2525
2626public class SeleniumExtensions {
2727
28- private final static Logger LOG = LoggerFactory .getLogger (SeleniumExtensions .class );
28+ private static final Logger LOG = LoggerFactory .getLogger (SeleniumExtensions .class );
29+
30+ //These timeout values define how long Selenium will wait for elements to be visible and enabled
31+ private static final int DEFAULT_TIMEOUT_IN_SEC = 15 ;
32+ private static final int COMMON_ELEMENT_TIMEOUT_IN_SEC = 5 ; //Used for most elements in a sign-in flow
2933
3034 private SeleniumExtensions () {
3135 }
@@ -47,7 +51,7 @@ public static WebDriver createDefaultWebDriver() {
4751
4852 public static WebElement waitForElementToBeVisibleAndEnable (WebDriver driver , By by , int timeOutInSeconds ) {
4953 WebDriverWait webDriverWait = new WebDriverWait (driver , timeOutInSeconds );
50- return webDriverWait .until (( dr ) ->
54+ return webDriverWait .until (dr ->
5155 {
5256 try {
5357 WebElement elementToBeDisplayed = driver .findElement (by );
@@ -56,14 +60,13 @@ public static WebElement waitForElementToBeVisibleAndEnable(WebDriver driver, By
5660 }
5761 return null ;
5862 } catch (StaleElementReferenceException e ) {
63+ LOG .info ("Stale element waitForElementToBeVisibleAndEnable: " + e .getMessage ());
5964 return null ;
6065 }
6166 });
6267 }
6368
6469 public static WebElement waitForElementToBeVisibleAndEnable (WebDriver driver , By by ) {
65- int DEFAULT_TIMEOUT_IN_SEC = 15 ;
66-
6770 return waitForElementToBeVisibleAndEnable (driver , by , DEFAULT_TIMEOUT_IN_SEC );
6871 }
6972
@@ -90,39 +93,44 @@ public static void performADOrCiamLogin(WebDriver driver, User user) {
9093 checkAuthenticationCompletePage (driver );
9194 return ;
9295 } catch (TimeoutException ex ) {
93- LOG .error (ex .getMessage ());
96+ LOG .error ("Timeout Exception while checking authentication complete page: " + ex .getMessage ());
9497 }
9598
9699 LOG .info ("Checking optional questions" );
97100
98101 try {
99102 LOG .info ("Are you trying to sign in to ... ? checking" );
100- waitForElementToBeVisibleAndEnable (driver , new By .ById (SeleniumConstants .ARE_YOU_TRYING_TO_SIGN_IN_TO ), 3 ).
103+ waitForElementToBeVisibleAndEnable (driver , new By .ById (SeleniumConstants .ARE_YOU_TRYING_TO_SIGN_IN_TO ), COMMON_ELEMENT_TIMEOUT_IN_SEC ).
101104 click ();
102105 LOG .info ("Are you trying to sign in to ... ? click Continue" );
103106
104107 } catch (TimeoutException ex ) {
105- LOG .error (ex .getMessage ());
108+ LOG .error ("Timeout Exception while checking sign in prompt: " + ex .getMessage ());
106109 }
107110
108111 try {
109112 LOG .info ("Stay signed in? checking" );
110- waitForElementToBeVisibleAndEnable (driver , new By .ById (SeleniumConstants .STAY_SIGN_IN_NO_BUTTON_ID ), 3 ).
113+ waitForElementToBeVisibleAndEnable (driver , new By .ById (SeleniumConstants .STAY_SIGN_IN_NO_BUTTON_ID ), COMMON_ELEMENT_TIMEOUT_IN_SEC ).
111114 click ();
112115 LOG .info ("Stay signed in? click NO" );
113116 } catch (TimeoutException ex ) {
114- LOG .error (ex .getMessage ());
117+ LOG .error ("Timeout Exception while checking stay signed in prompt: " + ex .getMessage ());
115118 }
116119 }
117120
118121 private static void checkAuthenticationCompletePage (WebDriver driver ) {
119- (new WebDriverWait (driver , 5 )).until ((ExpectedCondition <Boolean >) d -> {
120- boolean condition = false ;
122+ new WebDriverWait (driver , COMMON_ELEMENT_TIMEOUT_IN_SEC ).until ((ExpectedCondition <Boolean >) d -> {
121123 WebElement we = d .findElement (new By .ByTagName ("body" ));
122- if (we != null && we .getText ().contains ("Authentication complete" )) {
123- condition = true ;
124+ try {
125+ if (we != null && we .getText ().contains ("Authentication complete" ))
126+ //The authentication is complete and the WebDriverWait can end
127+ return true ;
128+ } catch (StaleElementReferenceException e ) {
129+ //It is possible for this method to begin executing before the redirect happens, in which case the WebElement
130+ // will reference something on the previous page and cause a StaleElementReferenceException
131+ return false ;
124132 }
125- return condition ;
133+ return false ;
126134 });
127135 }
128136
0 commit comments