25
25
26
26
public class SeleniumExtensions {
27
27
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
29
33
30
34
private SeleniumExtensions () {
31
35
}
@@ -47,7 +51,7 @@ public static WebDriver createDefaultWebDriver() {
47
51
48
52
public static WebElement waitForElementToBeVisibleAndEnable (WebDriver driver , By by , int timeOutInSeconds ) {
49
53
WebDriverWait webDriverWait = new WebDriverWait (driver , timeOutInSeconds );
50
- return webDriverWait .until (( dr ) ->
54
+ return webDriverWait .until (dr ->
51
55
{
52
56
try {
53
57
WebElement elementToBeDisplayed = driver .findElement (by );
@@ -56,14 +60,13 @@ public static WebElement waitForElementToBeVisibleAndEnable(WebDriver driver, By
56
60
}
57
61
return null ;
58
62
} catch (StaleElementReferenceException e ) {
63
+ LOG .info ("Stale element waitForElementToBeVisibleAndEnable: " + e .getMessage ());
59
64
return null ;
60
65
}
61
66
});
62
67
}
63
68
64
69
public static WebElement waitForElementToBeVisibleAndEnable (WebDriver driver , By by ) {
65
- int DEFAULT_TIMEOUT_IN_SEC = 15 ;
66
-
67
70
return waitForElementToBeVisibleAndEnable (driver , by , DEFAULT_TIMEOUT_IN_SEC );
68
71
}
69
72
@@ -90,39 +93,44 @@ public static void performADOrCiamLogin(WebDriver driver, User user) {
90
93
checkAuthenticationCompletePage (driver );
91
94
return ;
92
95
} catch (TimeoutException ex ) {
93
- LOG .error (ex .getMessage ());
96
+ LOG .error ("Timeout Exception while checking authentication complete page: " + ex .getMessage ());
94
97
}
95
98
96
99
LOG .info ("Checking optional questions" );
97
100
98
101
try {
99
102
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 ).
101
104
click ();
102
105
LOG .info ("Are you trying to sign in to ... ? click Continue" );
103
106
104
107
} catch (TimeoutException ex ) {
105
- LOG .error (ex .getMessage ());
108
+ LOG .error ("Timeout Exception while checking sign in prompt: " + ex .getMessage ());
106
109
}
107
110
108
111
try {
109
112
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 ).
111
114
click ();
112
115
LOG .info ("Stay signed in? click NO" );
113
116
} catch (TimeoutException ex ) {
114
- LOG .error (ex .getMessage ());
117
+ LOG .error ("Timeout Exception while checking stay signed in prompt: " + ex .getMessage ());
115
118
}
116
119
}
117
120
118
121
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 -> {
121
123
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 ;
124
132
}
125
- return condition ;
133
+ return false ;
126
134
});
127
135
}
128
136
0 commit comments