Skip to content

Commit f439011

Browse files
Add azure-spring-boot-test-selenium-common module and refactor AAD integration tests and B2C integration tests. (Azure#18561)
1 parent 0f84386 commit f439011

File tree

20 files changed

+304
-249
lines changed

20 files changed

+304
-249
lines changed

eng/versioning/version_client.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ com.azure:azure-security-keyvault-jca;1.0.0-beta.2;1.0.0-beta.3
142142
com.azure.spring:azure-spring-data-gremlin;2.3.1-beta.1;2.3.1-beta.1
143143
com.azure.spring:azure-spring-boot-starter-data-gremlin;3.0.0-beta.1;3.0.0-beta.1
144144
com.azure.spring:azure-spring-boot-test-aad;1.0.0;1.0.0
145+
com.azure.spring:azure-spring-boot-test-aad-b2c;1.0.0;1.0.0
146+
com.azure.spring:azure-spring-boot-test-selenium-common;1.0.0;1.0.0
145147
com.azure.spring:azure-spring-boot-test-aad-obo;1.0.0;1.0.0
146148
com.azure.spring:azure-spring-boot-test-aad-resource-server;1.0.0;1.0.0
147149
com.azure.spring:azure-spring-boot-test-aad-resource-server-by-filter;1.0.0;1.0.0

sdk/spring/azure-spring-boot-test-aad-b2c/pom.xml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,10 @@
4747
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
4848
</dependency>
4949

50-
<dependency>
51-
<groupId>org.seleniumhq.selenium</groupId>
52-
<artifactId>selenium-java</artifactId>
53-
</dependency>
54-
55-
<!-- test dependencies. -->
5650
<dependency>
5751
<groupId>com.azure.spring</groupId>
58-
<artifactId>azure-spring-boot-test-core</artifactId>
59-
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-boot-test-core;current} -->
52+
<artifactId>azure-spring-boot-test-selenium-common</artifactId>
53+
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-boot-test-selenium-common;current} -->
6054
<scope>test</scope>
6155
</dependency>
6256
</dependencies>

sdk/spring/azure-spring-boot-test-aad-b2c/src/test/java/com/azure/test/aad/b2c/selenium/AADB2CIT.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
package com.azure.test.aad.b2c.selenium;
55

6+
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_PROFILE_EDIT;
7+
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_SIGN_UP_OR_SIGN_IN;
8+
import static com.azure.test.aad.b2c.selenium.AADB2CSeleniumITHelper.createDefaultProperteis;
9+
610
import com.azure.spring.autoconfigure.b2c.AADB2COidcLoginConfigurer;
7-
import java.util.Collections;
811
import org.junit.After;
912
import org.junit.Assert;
13+
import org.junit.Before;
1014
import org.junit.Test;
1115
import org.springframework.boot.autoconfigure.SpringBootApplication;
1216
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@@ -19,55 +23,52 @@
1923
import org.springframework.ui.Model;
2024
import org.springframework.web.bind.annotation.GetMapping;
2125

22-
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_PROFILE_EDIT;
23-
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_SIGN_UP_OR_SIGN_IN;
24-
2526
public class AADB2CIT {
2627

2728
private final String JOB_TITLE_A_WORKER = "a worker";
2829
private final String JOB_TITLE_WORKER = "worker";
2930
private AADB2CSeleniumITHelper aadB2CSeleniumITHelper;
3031

32+
@Before
33+
public void initAndSignIn() {
34+
aadB2CSeleniumITHelper = new AADB2CSeleniumITHelper(DumbApp.class, createDefaultProperteis());
35+
aadB2CSeleniumITHelper.logIn();
36+
}
37+
3138
@Test
32-
public void testSignIn() throws InterruptedException {
33-
aadB2CSeleniumITHelper = new AADB2CSeleniumITHelper(DumbApp.class, Collections.emptyMap());
34-
aadB2CSeleniumITHelper.signIn(AAD_B2C_SIGN_UP_OR_SIGN_IN);
39+
public void testSignIn() {
3540
String name = aadB2CSeleniumITHelper.getName();
3641
String userFlowName = aadB2CSeleniumITHelper.getUserFlowName();
37-
3842
Assert.assertNotNull(name);
3943
Assert.assertNotNull(userFlowName);
4044
Assert.assertEquals(AAD_B2C_SIGN_UP_OR_SIGN_IN, userFlowName);
4145
}
4246

4347
@Test
44-
public void testProfileEdit() throws InterruptedException {
45-
aadB2CSeleniumITHelper = new AADB2CSeleniumITHelper(DumbApp.class, Collections.emptyMap());
46-
aadB2CSeleniumITHelper.signIn(AAD_B2C_SIGN_UP_OR_SIGN_IN);
48+
public void testProfileEdit() {
49+
aadB2CSeleniumITHelper.profileEditJobTitle(JOB_TITLE_A_WORKER);
4750
String currentJobTitle = aadB2CSeleniumITHelper.getJobTitle();
4851
String newJobTitle = JOB_TITLE_A_WORKER.equals(currentJobTitle) ? JOB_TITLE_WORKER : JOB_TITLE_A_WORKER;
4952
aadB2CSeleniumITHelper.profileEditJobTitle(newJobTitle);
5053
String name = aadB2CSeleniumITHelper.getName();
5154
String jobTitle = aadB2CSeleniumITHelper.getJobTitle();
5255
String userFlowName = aadB2CSeleniumITHelper.getUserFlowName();
53-
5456
Assert.assertNotNull(name);
5557
Assert.assertNotNull(jobTitle);
5658
Assert.assertEquals(newJobTitle, jobTitle);
5759
Assert.assertEquals(AAD_B2C_PROFILE_EDIT, userFlowName);
5860
}
5961

6062
@Test
61-
public void testLogOut() throws InterruptedException {
62-
aadB2CSeleniumITHelper = new AADB2CSeleniumITHelper(DumbApp.class, Collections.emptyMap());
63-
aadB2CSeleniumITHelper.signIn(AAD_B2C_SIGN_UP_OR_SIGN_IN);
64-
String signInButtonText = aadB2CSeleniumITHelper.logoutAndGetSignInButtonText();
63+
public void testLogOut() {
64+
aadB2CSeleniumITHelper.logout();
65+
String signInButtonText = aadB2CSeleniumITHelper.getSignInButtonText();
6566
Assert.assertEquals("Sign in", signInButtonText);
6667
}
6768

6869
@After
69-
public void quitDriver() {
70-
aadB2CSeleniumITHelper.quitDriver();
70+
public void destroy() {
71+
aadB2CSeleniumITHelper.destroy();
7172
}
7273

7374
@EnableWebSecurity
Lines changed: 42 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package com.azure.test.aad.b2c.selenium;
22

3-
import com.azure.spring.test.AppRunner;
4-
import java.io.File;
5-
import java.io.IOException;
6-
import java.util.HashMap;
7-
import java.util.Map;
8-
import java.util.regex.Pattern;
9-
import org.openqa.selenium.By;
10-
import org.openqa.selenium.WebDriver;
11-
import org.openqa.selenium.chrome.ChromeDriver;
12-
import org.openqa.selenium.chrome.ChromeDriverService;
13-
import org.openqa.selenium.chrome.ChromeOptions;
14-
import org.openqa.selenium.support.ui.ExpectedConditions;
15-
import org.openqa.selenium.support.ui.WebDriverWait;
16-
173
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_CLIENT_ID;
184
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_CLIENT_SECRET;
195
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_PROFILE_EDIT;
@@ -22,115 +8,63 @@
228
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_TENANT;
239
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_USER_EMAIL;
2410
import static com.azure.spring.test.EnvironmentVariable.AAD_B2C_USER_PASSWORD;
11+
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
2512

26-
public class AADB2CSeleniumITHelper {
27-
28-
private final String userEmail;
29-
private final String userPassword;
30-
private final AppRunner app;
31-
private WebDriver driver;
32-
private WebDriverWait wait;
33-
private static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<>();
34-
35-
static {
36-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.tenant", AAD_B2C_TENANT);
37-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.client-id", AAD_B2C_CLIENT_ID);
38-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.client-secret", AAD_B2C_CLIENT_SECRET);
39-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.reply-url", AAD_B2C_REPLY_URL);
40-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.user-flows.sign-up-or-sign-in", AAD_B2C_SIGN_UP_OR_SIGN_IN);
41-
DEFAULT_PROPERTIES.put("azure.activedirectory.b2c.user-flows.profile-edit", AAD_B2C_PROFILE_EDIT);
13+
import com.azure.test.aad.common.SeleniumITHelper;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import org.openqa.selenium.By;
17+
import org.openqa.selenium.Keys;
18+
import org.openqa.selenium.support.ui.ExpectedConditions;
4219

43-
final String directory = "src/test/resources/driver/";
44-
final String chromedriverLinux = "chromedriver_linux64";
45-
final String chromedriverWin32 = "chromedriver_win32.exe";
46-
final String chromedriverMac = "chromedriver_mac64";
47-
String osName = System.getProperty("os.name").toLowerCase();
48-
Process process = null;
49-
try {
50-
File dir = new File(directory);
51-
if (Pattern.matches("linux.*", osName)) {
52-
process = Runtime.getRuntime().exec("chmod +x " + chromedriverLinux, null, dir);
53-
process.waitFor();
54-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverLinux);
55-
} else if (Pattern.matches("windows.*", osName)) {
56-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverWin32);
57-
} else if (Pattern.matches("mac.*", osName)) {
58-
process = Runtime.getRuntime().exec("chmod +x " + chromedriverMac, null, dir);
59-
process.waitFor();
60-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverMac);
61-
} else {
62-
throw new IllegalStateException("Unrecognized osName. osName = " + System.getProperty("os.name"));
63-
}
64-
} catch (InterruptedException | IOException e) {
65-
throw new RuntimeException(e);
66-
} finally {
67-
if (process != null) {
68-
process.destroy();
69-
}
70-
}
20+
public class AADB2CSeleniumITHelper extends SeleniumITHelper {
21+
22+
private String userEmail;
23+
private String userPassword;
24+
25+
public static Map<String, String> createDefaultProperteis() {
26+
Map<String, String> defaultProperteis = new HashMap<>();
27+
defaultProperteis.put("azure.activedirectory.b2c.tenant", AAD_B2C_TENANT);
28+
defaultProperteis.put("azure.activedirectory.b2c.client-id", AAD_B2C_CLIENT_ID);
29+
defaultProperteis.put("azure.activedirectory.b2c.client-secret", AAD_B2C_CLIENT_SECRET);
30+
defaultProperteis.put("azure.activedirectory.b2c.reply-url", AAD_B2C_REPLY_URL);
31+
defaultProperteis
32+
.put("azure.activedirectory.b2c.user-flows.profile-edit", AAD_B2C_PROFILE_EDIT);
33+
defaultProperteis
34+
.put("azure.activedirectory.b2c.user-flows.sign-up-or-sign-in", AAD_B2C_SIGN_UP_OR_SIGN_IN);
35+
return defaultProperteis;
7136
}
7237

73-
public AADB2CSeleniumITHelper(Class<?> appClass, Map<String, String> properties) throws InterruptedException {
38+
public AADB2CSeleniumITHelper(Class<?> appClass, Map<String, String> properties) {
39+
super(appClass, properties);
7440
userEmail = AAD_B2C_USER_EMAIL;
7541
userPassword = AAD_B2C_USER_PASSWORD;
76-
app = new AppRunner(appClass);
77-
DEFAULT_PROPERTIES.forEach(app::property);
78-
properties.forEach(app::property);
79-
this.app.start();
80-
setDriver();
81-
}
82-
83-
public void quitDriver() {
84-
try {
85-
this.driver.quit();
86-
} catch (Exception e) {
87-
this.driver = null;
88-
}
8942
}
9043

91-
private void setDriver() {
92-
if (driver == null) {
93-
ChromeOptions options = new ChromeOptions();
94-
options.addArguments("--headless");
95-
options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage");
96-
this.driver = new ChromeDriver(options);
97-
wait = new WebDriverWait(driver, 5);
98-
}
99-
}
100-
101-
public void signIn(String userFlowName) throws InterruptedException {
44+
public void logIn() {
10245
driver.get(app.root());
103-
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='submit']")));
104-
driver.findElement(By.id("email")).sendKeys(userEmail);
105-
driver.findElement(By.id("password")).sendKeys(userPassword);
106-
driver.findElement(By.cssSelector("button[type='submit']")).click();
46+
wait.until(presenceOfElementLocated(By.id("email"))).sendKeys(userEmail);
47+
wait.until(presenceOfElementLocated(By.id("password"))).sendKeys(userPassword);
48+
wait.until(presenceOfElementLocated(By.cssSelector("button[type='submit']"))).sendKeys(Keys.ENTER);
10749
manualRedirection();
10850
}
10951

110-
public void profileEditJobTitle(String newJobTitle) throws InterruptedException {
111-
driver.findElement(By.id("profileEdit")).click();
52+
public void profileEditJobTitle(String newJobTitle) {
53+
wait.until(presenceOfElementLocated(By.id("profileEdit"))).click();
11254
changeJobTile(newJobTitle);
113-
driver.findElement(By.cssSelector("button[type='submit']")).click();
55+
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='submit']"))).click();
11456
manualRedirection();
11557
}
11658

117-
public String logoutAndGetSignInButtonText() throws InterruptedException {
118-
wait.until(ExpectedConditions.elementToBeClickable(By.id("logout")));
119-
driver.findElement(By.id("logout")).click();
120-
121-
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='submit']")));
122-
driver.findElement(By.cssSelector("button[type='submit']")).submit();
59+
public void logout() {
60+
wait.until(presenceOfElementLocated(By.id("logout"))).click();
61+
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='submit']"))).submit();
12362
manualRedirection();
12463
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(
125-
"a[href='/oauth2/authorization/" + AAD_B2C_SIGN_UP_OR_SIGN_IN + "']")));
126-
driver.findElement(
127-
By.cssSelector(
128-
"a[href='/oauth2/authorization/" + AAD_B2C_SIGN_UP_OR_SIGN_IN + "']")).click();
129-
wait.until(ExpectedConditions.elementToBeClickable(By.id("next")));
130-
return driver.findElement(By.cssSelector("button[type='submit']")).getText();
64+
"a[href='/oauth2/authorization/" + AAD_B2C_SIGN_UP_OR_SIGN_IN + "']"))).click();
13165
}
13266

133-
private void manualRedirection() throws InterruptedException {
67+
private void manualRedirection() {
13468
wait.until(ExpectedConditions.urlMatches("^http://localhost"));
13569
String currentUrl = driver.getCurrentUrl();
13670
String newCurrentUrl = currentUrl.replaceFirst("http://localhost:8080/", app.root());
@@ -139,9 +73,8 @@ private void manualRedirection() throws InterruptedException {
13973

14074
public void changeJobTile(String newValue) {
14175
String elementId = "jobTitle";
142-
wait.until(ExpectedConditions.elementToBeClickable(By.id(elementId)));
143-
driver.findElement(By.id(elementId)).clear();
144-
driver.findElement(By.id(elementId)).sendKeys(newValue);
76+
wait.until(presenceOfElementLocated(By.id(elementId))).clear();
77+
wait.until(presenceOfElementLocated(By.id(elementId))).sendKeys(newValue);
14578
}
14679

14780
public String getJobTitle() {
@@ -152,7 +85,6 @@ public String getJobTitle() {
15285
}
15386

15487
public String getName() {
155-
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("tbody")));
15688
return driver.findElement(By.cssSelector("tbody"))
15789
.findElement(By.xpath("tr[2]"))
15890
.findElement(By.xpath("th[2]"))
@@ -166,4 +98,7 @@ public String getUserFlowName() {
16698
.getText();
16799
}
168100

101+
public String getSignInButtonText() {
102+
return wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='submit']"))).getText();
103+
}
169104
}

sdk/spring/azure-spring-boot-test-aad/pom.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@
3131
<artifactId>spring-boot-starter-oauth2-client</artifactId>
3232
</dependency>
3333

34-
<!-- test dependencies. -->
35-
<dependency>
36-
<groupId>com.azure.spring</groupId>
37-
<artifactId>azure-spring-boot-test-core</artifactId>
38-
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-boot-test-core;current} -->
39-
<scope>test</scope>
40-
</dependency>
4134
<dependency>
4235
<groupId>org.springframework.boot</groupId>
4336
<artifactId>spring-boot-starter-test</artifactId>
4437
<scope>test</scope>
4538
</dependency>
39+
4640
<dependency>
47-
<groupId>org.seleniumhq.selenium</groupId>
48-
<artifactId>selenium-java</artifactId>
41+
<groupId>com.azure.spring</groupId>
42+
<artifactId>azure-spring-boot-test-selenium-common</artifactId>
43+
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-boot-test-selenium-common;current} -->
4944
<scope>test</scope>
5045
</dependency>
5146
</dependencies>

0 commit comments

Comments
 (0)