|
5 | 5 |
|
6 | 6 | import com.microsoft.aad.msal4j.TestConstants; |
7 | 7 | import com.microsoft.aad.msal4j.labapi2.LabUser; |
8 | | -import org.apache.commons.io.FileUtils; |
9 | 8 | import org.openqa.selenium.By; |
10 | | -import org.openqa.selenium.OutputType; |
11 | 9 | import org.openqa.selenium.StaleElementReferenceException; |
12 | | -import org.openqa.selenium.TakesScreenshot; |
13 | 10 | import org.openqa.selenium.TimeoutException; |
14 | 11 | import org.openqa.selenium.WebDriver; |
15 | 12 | import org.openqa.selenium.WebElement; |
16 | 13 | import org.openqa.selenium.chrome.ChromeDriver; |
17 | 14 | import org.openqa.selenium.chrome.ChromeOptions; |
18 | | -import org.openqa.selenium.support.ui.ExpectedCondition; |
| 15 | +import org.openqa.selenium.support.ui.ExpectedConditions; |
19 | 16 | import org.openqa.selenium.support.ui.WebDriverWait; |
20 | 17 | import org.slf4j.Logger; |
21 | 18 | import org.slf4j.LoggerFactory; |
22 | 19 |
|
23 | | -import java.io.File; |
24 | | -import java.util.concurrent.TimeUnit; |
| 20 | +import java.time.Duration; |
25 | 21 |
|
26 | 22 | public class SeleniumExtensions { |
27 | 23 |
|
28 | 24 | private static final Logger LOG = LoggerFactory.getLogger(SeleniumExtensions.class); |
29 | 25 |
|
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 |
| 26 | + private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(15); |
| 27 | + private static final Duration COMMON_ELEMENT_TIMEOUT = Duration.ofSeconds(5); |
33 | 28 |
|
34 | 29 | private SeleniumExtensions() { |
35 | 30 | } |
36 | 31 |
|
37 | 32 | public static WebDriver createDefaultWebDriver() { |
38 | 33 | ChromeOptions options = new ChromeOptions(); |
39 | | - |
40 | | - //No visual rendering, remove to see browser window when debugging |
41 | | -// options.addArguments("--headless"); |
42 | | - //Add to avoid issues if your real browser's history/cookies are affecting tests, should not be needed in ADO pipelines |
| 34 | + options.addArguments("--headless"); |
43 | 35 | options.addArguments("--incognito"); |
44 | 36 |
|
45 | 37 | System.setProperty("webdriver.chrome.driver", "C:/Windows/chromedriver.exe"); |
46 | | - ChromeDriver driver = new ChromeDriver(options); |
47 | | - driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); |
48 | | - |
49 | | - return driver; |
| 38 | + return new ChromeDriver(options); |
50 | 39 | } |
51 | 40 |
|
52 | | - public static WebElement waitForElementToBeVisibleAndEnable(WebDriver driver, By by, int timeOutInSeconds) { |
53 | | - WebDriverWait webDriverWait = new WebDriverWait(driver, timeOutInSeconds); |
54 | | - return webDriverWait.until(dr -> |
55 | | - { |
| 41 | + public static WebElement waitForElementToBeVisibleAndEnabled(WebDriver driver, By by, Duration timeout) { |
| 42 | + WebDriverWait wait = new WebDriverWait(driver, timeout.getSeconds()); |
| 43 | + return wait.until(dr -> { |
56 | 44 | try { |
57 | | - WebElement elementToBeDisplayed = driver.findElement(by); |
58 | | - if (elementToBeDisplayed.isDisplayed() && elementToBeDisplayed.isEnabled()) { |
59 | | - return elementToBeDisplayed; |
| 45 | + WebElement element = driver.findElement(by); |
| 46 | + if (element.isDisplayed() && element.isEnabled()) { |
| 47 | + return element; |
60 | 48 | } |
61 | 49 | return null; |
62 | 50 | } catch (StaleElementReferenceException e) { |
63 | | - LOG.info("Stale element waitForElementToBeVisibleAndEnable: " + e.getMessage()); |
| 51 | + LOG.debug("Stale element in waitForElementToBeVisibleAndEnabled: {}", e.getMessage()); |
64 | 52 | return null; |
65 | 53 | } |
66 | 54 | }); |
67 | 55 | } |
68 | 56 |
|
69 | | - public static WebElement waitForElementToBeVisibleAndEnable(WebDriver driver, By by) { |
70 | | - return waitForElementToBeVisibleAndEnable(driver, by, DEFAULT_TIMEOUT_IN_SEC); |
| 57 | + public static WebElement waitForElementToBeVisibleAndEnabled(WebDriver driver, By by) { |
| 58 | + return waitForElementToBeVisibleAndEnabled(driver, by, DEFAULT_TIMEOUT); |
71 | 59 | } |
72 | 60 |
|
73 | 61 | public static void performADOrCiamLogin(WebDriver driver, LabUser user) { |
74 | | - LOG.info("performADOrCiamLogin"); |
| 62 | + LOG.info("performADOrCiamLogin for user: {}", user.getUpn()); |
75 | 63 |
|
76 | 64 | UserInformationFields fields = new UserInformationFields(user); |
77 | 65 |
|
78 | | - LOG.info("Loggin in ... Entering username"); |
79 | | - driver.findElement(new By.ById(fields.getAadUserNameInputId())).sendKeys(user.getUpn()); |
| 66 | + LOG.info("Entering username"); |
| 67 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getAadUserNameInputId())) |
| 68 | + .sendKeys(user.getUpn()); |
80 | 69 |
|
81 | | - LOG.info("Loggin in ... Clicking <Next> after username"); |
82 | | - driver.findElement(new By.ById(fields.getAadSignInButtonId())).click(); |
| 70 | + LOG.info("Clicking Next after username"); |
| 71 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getAadSignInButtonId())) |
| 72 | + .click(); |
83 | 73 |
|
84 | | - LOG.info("Loggin in ... Entering password"); |
85 | | - By by = new By.ById(fields.getPasswordInputId()); |
86 | | - waitForElementToBeVisibleAndEnable(driver, by).sendKeys(user.getPassword()); |
| 74 | + LOG.info("Entering password"); |
| 75 | + System.out.println("Using password ID: " + fields.getPasswordInputId()); |
| 76 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getPasswordInputId())); |
| 77 | + System.out.println(By.id(fields.getPasswordInputId())); |
| 78 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getPasswordInputId())) |
| 79 | + .sendKeys(user.getPassword()); |
87 | 80 |
|
88 | | - LOG.info("Loggin in ... click submit"); |
89 | | - waitForElementToBeVisibleAndEnable(driver, new By.ById(fields.getPasswordSigInButtonId())). |
90 | | - click(); |
| 81 | + LOG.info("Clicking submit"); |
| 82 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getPasswordSigInButtonId())) |
| 83 | + .click(); |
91 | 84 |
|
92 | | - try { |
93 | | - checkAuthenticationCompletePage(driver); |
| 85 | + if (checkAuthenticationCompletePage(driver)) { |
94 | 86 | return; |
95 | | - } catch (TimeoutException ex) { |
96 | | - LOG.error("Timeout Exception while checking authentication complete page: " + ex.getMessage()); |
97 | 87 | } |
98 | 88 |
|
99 | | - LOG.info("Checking optional questions"); |
| 89 | + handleOptionalPrompts(driver); |
| 90 | + } |
100 | 91 |
|
| 92 | + private static boolean checkAuthenticationCompletePage(WebDriver driver) { |
101 | 93 | try { |
102 | | - LOG.info("Are you trying to sign in to ... ? checking"); |
103 | | - waitForElementToBeVisibleAndEnable(driver, new By.ById(SeleniumConstants.ARE_YOU_TRYING_TO_SIGN_IN_TO), COMMON_ELEMENT_TIMEOUT_IN_SEC). |
104 | | - click(); |
105 | | - LOG.info("Are you trying to sign in to ... ? click Continue"); |
106 | | - |
| 94 | + WebDriverWait wait = new WebDriverWait(driver, COMMON_ELEMENT_TIMEOUT.getSeconds()); |
| 95 | + wait.until(ExpectedConditions.textToBePresentInElementLocated( |
| 96 | + By.tagName("body"), "Authentication complete")); |
| 97 | + return true; |
107 | 98 | } catch (TimeoutException ex) { |
108 | | - LOG.error("Timeout Exception while checking sign in prompt: " + ex.getMessage()); |
| 99 | + LOG.debug("Authentication complete page not found: {}", ex.getMessage()); |
| 100 | + return false; |
109 | 101 | } |
| 102 | + } |
110 | 103 |
|
| 104 | + private static void handleOptionalPrompts(WebDriver driver) { |
| 105 | + // Handle "Are you trying to sign in to..." prompt |
111 | 106 | try { |
112 | | - LOG.info("Stay signed in? checking"); |
113 | | - waitForElementToBeVisibleAndEnable(driver, new By.ById(SeleniumConstants.STAY_SIGN_IN_NO_BUTTON_ID), COMMON_ELEMENT_TIMEOUT_IN_SEC). |
114 | | - click(); |
115 | | - LOG.info("Stay signed in? click NO"); |
| 107 | + LOG.info("Checking for 'Are you trying to sign in' prompt"); |
| 108 | + waitForElementToBeVisibleAndEnabled( |
| 109 | + driver, |
| 110 | + By.id(SeleniumConstants.ARE_YOU_TRYING_TO_SIGN_IN_TO), |
| 111 | + COMMON_ELEMENT_TIMEOUT) |
| 112 | + .click(); |
| 113 | + LOG.info("Clicked Continue on sign-in prompt"); |
116 | 114 | } catch (TimeoutException ex) { |
117 | | - LOG.error("Timeout Exception while checking stay signed in prompt: " + ex.getMessage()); |
| 115 | + LOG.debug("No 'Are you trying to sign in' prompt found"); |
118 | 116 | } |
119 | | - } |
120 | 117 |
|
121 | | - private static void checkAuthenticationCompletePage(WebDriver driver) { |
122 | | - new WebDriverWait(driver, COMMON_ELEMENT_TIMEOUT_IN_SEC).until((ExpectedCondition<Boolean>) d -> { |
123 | | - WebElement we = d.findElement(new By.ByTagName("body")); |
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; |
132 | | - } |
133 | | - return false; |
134 | | - }); |
| 118 | + // Handle "Stay signed in?" prompt |
| 119 | + try { |
| 120 | + LOG.info("Checking for 'Stay signed in' prompt"); |
| 121 | + waitForElementToBeVisibleAndEnabled( |
| 122 | + driver, |
| 123 | + By.id(SeleniumConstants.STAY_SIGN_IN_NO_BUTTON_ID), |
| 124 | + COMMON_ELEMENT_TIMEOUT) |
| 125 | + .click(); |
| 126 | + LOG.info("Clicked No on 'Stay signed in' prompt"); |
| 127 | + } catch (TimeoutException ex) { |
| 128 | + LOG.debug("No 'Stay signed in' prompt found"); |
| 129 | + } |
135 | 130 | } |
136 | 131 |
|
137 | 132 | public static void performADFSLogin(WebDriver driver, LabUser user) { |
138 | | - LOG.info("PerformADFSLogin"); |
| 133 | + LOG.info("performADFSLogin for user: {}", user.getUpn()); |
139 | 134 |
|
140 | 135 | UserInformationFields fields = new UserInformationFields(user); |
141 | 136 |
|
142 | | - LOG.info("Loggin in ... Entering username"); |
143 | | - driver.findElement(new By.ById(fields.getADFSUserNameInputId())).sendKeys(user.getUpn()); |
| 137 | + LOG.info("Entering username"); |
| 138 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getADFSUserNameInputId())) |
| 139 | + .sendKeys(user.getUpn()); |
144 | 140 |
|
145 | | - LOG.info("Loggin in ... Entering password"); |
146 | | - By by = new By.ById(fields.getPasswordInputId()); |
147 | | - waitForElementToBeVisibleAndEnable(driver, by).sendKeys(user.getPassword()); |
| 141 | + LOG.info("Entering password"); |
| 142 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getPasswordInputId())) |
| 143 | + .sendKeys(user.getPassword()); |
148 | 144 |
|
149 | | - LOG.info("Loggin in ... click submit"); |
150 | | - waitForElementToBeVisibleAndEnable(driver, new By.ById(fields.getPasswordSigInButtonId())). |
151 | | - click(); |
| 145 | + LOG.info("Clicking submit"); |
| 146 | + waitForElementToBeVisibleAndEnabled(driver, By.id(fields.getPasswordSigInButtonId())) |
| 147 | + .click(); |
152 | 148 | } |
153 | 149 |
|
154 | 150 | public static void performLocalLogin(WebDriver driver, LabUser user) { |
155 | | - LOG.info("PerformLocalLogin"); |
| 151 | + LOG.info("performLocalLogin"); |
156 | 152 |
|
157 | | - driver.findElement(new By.ById(SeleniumConstants.B2C_LOCAL_ACCOUNT_ID)).click(); |
| 153 | + waitForElementToBeVisibleAndEnabled(driver, By.id(SeleniumConstants.B2C_LOCAL_ACCOUNT_ID)) |
| 154 | + .click(); |
158 | 155 |
|
159 | | - LOG.info("Loggin in ... Entering username"); |
160 | | - driver.findElement(new By.ById(SeleniumConstants.B2C_LOCAL_USERNAME_ID)).sendKeys(TestConstants.B2C_UPN); |
| 156 | + LOG.info("Entering username"); |
| 157 | + waitForElementToBeVisibleAndEnabled(driver, By.id(SeleniumConstants.B2C_LOCAL_USERNAME_ID)) |
| 158 | + .sendKeys(TestConstants.B2C_UPN); |
161 | 159 |
|
162 | | - LOG.info("Loggin in ... Entering password"); |
163 | | - By by = new By.ById(SeleniumConstants.B2C_LOCAL_PASSWORD_ID); |
164 | | - waitForElementToBeVisibleAndEnable(driver, by).sendKeys(user.getPassword()); |
| 160 | + LOG.info("Entering password"); |
| 161 | + waitForElementToBeVisibleAndEnabled(driver, By.id(SeleniumConstants.B2C_LOCAL_PASSWORD_ID)) |
| 162 | + .sendKeys(user.getPassword()); |
165 | 163 |
|
166 | | - waitForElementToBeVisibleAndEnable(driver, new By.ById(SeleniumConstants.B2C_LOCAL_SIGN_IN_BUTTON_ID)). |
167 | | - click(); |
| 164 | + LOG.info("Clicking sign in"); |
| 165 | + waitForElementToBeVisibleAndEnabled(driver, By.id(SeleniumConstants.B2C_LOCAL_SIGN_IN_BUTTON_ID)) |
| 166 | + .click(); |
168 | 167 | } |
169 | 168 | } |
0 commit comments