11package 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-
173import static com .azure .spring .test .EnvironmentVariable .AAD_B2C_CLIENT_ID ;
184import static com .azure .spring .test .EnvironmentVariable .AAD_B2C_CLIENT_SECRET ;
195import static com .azure .spring .test .EnvironmentVariable .AAD_B2C_PROFILE_EDIT ;
228import static com .azure .spring .test .EnvironmentVariable .AAD_B2C_TENANT ;
239import static com .azure .spring .test .EnvironmentVariable .AAD_B2C_USER_EMAIL ;
2410import 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}
0 commit comments