Skip to content

Commit b686c8c

Browse files
authored
Merge pull request #924 from AzureAD/avdunn/selenium-fix
Selenium test fix
2 parents 928991b + 9ce5038 commit b686c8c

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

msal4j-sdk/src/integrationtest/java/com.microsoft.aad.msal4j/HttpClientIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
1616
import static org.junit.jupiter.api.Assertions.assertNotNull;
1717
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1819

1920
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2021
class HttpClientIT {
@@ -81,6 +82,8 @@ private void assertAcquireTokenCommon_WithTimeout(User user, int readTimeout)
8182
.build())
8283
.get());
8384

84-
assertEquals("com.microsoft.aad.msal4j.MsalClientException: java.net.SocketTimeoutException: Read timed out", ex.getMessage());
85+
//The timeout may occur during either the connection attempt (SocketTimeoutException) or the SSL handshake (SSLException)
86+
assertTrue(ex.getMessage().equals("com.microsoft.aad.msal4j.MsalClientException: java.net.SocketTimeoutException: Read timed out") ||
87+
ex.getMessage().equals("com.microsoft.aad.msal4j.MsalClientException: javax.net.ssl.SSLException: Read timed out"));
8588
}
8689
}

msal4j-sdk/src/integrationtest/java/infrastructure/SeleniumExtensions.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525

2626
public 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

Comments
 (0)