-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathSeleniumExtensions.java
More file actions
99 lines (79 loc) · 3.74 KB
/
SeleniumExtensions.java
File metadata and controls
99 lines (79 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package infrastructure;
import com.microsoft.aad.msal4j.labapi.UserConfig;
import infrastructure.pageobjects.ADFSLoginPage;
import infrastructure.pageobjects.AzureADLoginPage;
import infrastructure.pageobjects.B2CLocalLoginPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
public class SeleniumExtensions {
private static final Logger LOG = LoggerFactory.getLogger(SeleniumExtensions.class);
private SeleniumExtensions() {
}
public static WebDriver createDefaultWebDriver() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
options.addArguments("--incognito");
return new ChromeDriver(options);
}
public static WebElement waitForElementToBeVisibleAndEnabled(WebDriver driver, By by, Duration timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
return wait.until(ExpectedConditions.elementToBeClickable(by));
}
public static void performADOrCiamLogin(WebDriver driver, UserConfig user) {
LOG.info("performADOrCiamLogin for user: {}", user.getUpn());
AzureADLoginPage loginPage = new AzureADLoginPage(driver);
loginPage.login(user.getUpn(), user.getPassword());
}
public static void performADFSLogin(WebDriver driver, UserConfig user) {
LOG.info("performADFSLogin for user: {}", user.getUpn());
ADFSLoginPage loginPage = new ADFSLoginPage(driver);
loginPage.login(user.getUpn(), user.getPassword());
}
public static void performLocalLogin(WebDriver driver, UserConfig user) {
LOG.info("performLocalLogin");
B2CLocalLoginPage loginPage = new B2CLocalLoginPage(driver);
loginPage.login(user.getUpn(), user.getPassword());
}
/**
* Perform device code flow authentication.
* Navigates to the verification URI, enters the device code, and completes Azure AD login.
*
* @param driver The WebDriver instance
* @param verificationUri The URI to navigate to for device code entry
* @param userCode The device code to enter
* @param user The lab user credentials for login
*/
public static void performDeviceCodeLogin(WebDriver driver, String verificationUri, String userCode, UserConfig user) {
LOG.info("performDeviceCodeLogin for user: {}", user.getUpn());
try {
// Navigate to device code verification page
LOG.info("Navigating to verification URI");
driver.navigate().to(verificationUri);
// Enter device code
LOG.info("Entering device code");
By deviceCodeInputField = By.id("otc");
waitForElementToBeVisibleAndEnabled(driver, deviceCodeInputField, Duration.ofSeconds(15))
.sendKeys(userCode);
// Click continue button
LOG.info("Clicking continue button");
By continueButton = By.id("idSIButton9");
waitForElementToBeVisibleAndEnabled(driver, continueButton, Duration.ofSeconds(15))
.click();
// Perform standard Azure AD login
performADOrCiamLogin(driver, user);
} catch (Exception e) {
LOG.error("Device code flow automation failed: {}", e.getMessage());
throw new RuntimeException("Device code flow automation failed", e);
}
}
}