Skip to content

Commit 56642f8

Browse files
committed
fixed the form authnetication tests
1 parent 846c6d9 commit 56642f8

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

src/main/java/io/github/mfaisalkhatri/drivers/DriverManager.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,7 @@ private static void setupBrowserTimeouts () {
8282
LOG.info ("Setting Browser Timeouts....");
8383
getDriver ().manage ()
8484
.timeouts ()
85-
.implicitlyWait (Duration.ofSeconds (20));
86-
getDriver ().manage ()
87-
.timeouts ()
88-
.pageLoadTimeout (Duration.ofSeconds (20));
89-
getDriver ().manage ()
90-
.timeouts ()
91-
.scriptTimeout (Duration.ofSeconds (20));
85+
.implicitlyWait (Duration.ofSeconds (30));
9286
}
9387

9488
private static void setupChromeDriver () {

src/test/java/io/github/mfaisalkhatri/pages/theinternet/FormAuthenticationPage.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,32 @@
1515

1616
package io.github.mfaisalkhatri.pages.theinternet;
1717

18+
import static io.github.mfaisalkhatri.drivers.DriverManager.getDriver;
19+
20+
import java.time.Duration;
21+
1822
import org.openqa.selenium.By;
1923
import org.openqa.selenium.WebElement;
20-
21-
import static io.github.mfaisalkhatri.drivers.DriverManager.getDriver;
24+
import org.openqa.selenium.support.ui.ExpectedConditions;
25+
import org.openqa.selenium.support.ui.WebDriverWait;
2226

2327
/**
2428
* Created By Faisal Khatri on 24-12-2021
2529
*/
2630
public class FormAuthenticationPage {
2731

32+
private WebDriverWait wait;
33+
34+
public FormAuthenticationPage () {
35+
this.wait = new WebDriverWait (getDriver (), Duration.ofSeconds (30));
36+
}
37+
2838
public String getFlashMessage () {
29-
return getDriver ().findElement (By.id ("flash"))
30-
.getText ();
39+
return wait.until (ExpectedConditions.presenceOfElementLocated (By.id ("flash"))).getText ();
3140
}
3241

3342
public SecurePage login (final String userName, final String password) {
43+
getDriver ().navigate ().refresh ();
3444
userNameField ().click ();
3545
userNameField ().clear ();
3646
userNameField ().sendKeys (userName);

src/test/java/io/github/mfaisalkhatri/pages/theinternet/SecurePage.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@
1717

1818
import org.openqa.selenium.By;
1919
import org.openqa.selenium.WebElement;
20+
import org.openqa.selenium.support.ui.ExpectedConditions;
21+
import org.openqa.selenium.support.ui.WebDriverWait;
2022

2123
import static io.github.mfaisalkhatri.drivers.DriverManager.getDriver;
2224

25+
import java.time.Duration;
26+
2327
/**
2428
* Created By Faisal Khatri on 24-12-2021
2529
*/
2630
public class SecurePage {
2731

32+
private WebDriverWait wait;
33+
34+
public SecurePage () {
35+
this.wait = new WebDriverWait (getDriver (), Duration.ofSeconds (30));
36+
}
37+
2838
public String getFlashMessage () {
29-
return getDriver ().findElement (By.id ("flash"))
30-
.getText ();
39+
return wait.until (ExpectedConditions.presenceOfElementLocated (By.id ("flash"))).getText ();
3140
}
3241

3342
public String getHeaderText () {

src/test/java/io/github/mfaisalkhatri/tests/theinternet/FormAuthenticationTests.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ public void testSetup () {
5252

5353
@Test
5454
public void blankUserAndPasswordTest () {
55+
5556
this.formAuthenticationPage.login (" ", " ");
5657
assertTrue (this.formAuthenticationPage.getFlashMessage ()
5758
.contains ("Your username is invalid!"));
5859
}
5960

6061
@Test
6162
public void invalidLoginCredentialsTest () {
63+
6264
this.formAuthenticationPage.login (USERNAME, "InvalidPass");
6365
assertTrue (this.formAuthenticationPage.getFlashMessage ()
6466
.contains ("Your password is invalid!"));
@@ -69,20 +71,22 @@ public Iterator<Object[]> loginData () {
6971
final List<Object[]> testData = new ArrayList<> ();
7072
testData.add (new Object[] { " ", PASSWORD, false });
7173
testData.add (new Object[] { USERNAME, " ", false });
72-
testData.add (new Object[] { " ", " ", false });
7374
testData.add (new Object[] { USERNAME, "invalid", false });
7475
testData.add (new Object[] { USERNAME, PASSWORD, true });
7576
return testData.iterator ();
7677
}
7778

7879
@Test (dataProvider = "loginData")
7980
public void loginTests (final String userName, final String password, final boolean isValid) {
81+
8082
this.securePage = this.formAuthenticationPage.login (userName, password);
8183

8284
if (!isValid) {
85+
System.out.println (formAuthenticationPage.getFlashMessage ());
8386
assertTrue (this.formAuthenticationPage.getFlashMessage ()
84-
.contains (" is invalid!"));
87+
.contains ("is invalid!"));
8588
} else {
89+
System.out.println (securePage.getFlashMessage ());
8690
assertTrue (this.securePage.getFlashMessage ()
8791
.contains ("You logged into a secure area!"));
8892
assertEquals (this.securePage.getHeaderText (), "Secure Area");

test-suite/testng-theinternet.xml

+12-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</class>
5959
</classes>
6060
</test>
61-
<test name="Form Authentication Tests">
61+
<test name="Form Authentication Tests - Negative scenarios">
6262
<parameter name="browser" value="chrome"/>
6363
<classes>
6464
<class name="io.github.mfaisalkhatri.tests.theinternet.FormAuthenticationTests">
@@ -67,6 +67,15 @@
6767
<include name="passwordNotValidTest"/>
6868
<include name="invalidLoginCredentialsTest"/>
6969
<include name="blankUserAndPasswordTest"/>
70+
</methods>
71+
</class>
72+
</classes>
73+
</test>
74+
<test name="Form Authentication Tests - DataProvider">
75+
<parameter name="browser" value="chrome"/>
76+
<classes>
77+
<class name="io.github.mfaisalkhatri.tests.theinternet.FormAuthenticationTests">
78+
<methods>
7079
<include name="loginTests"/>
7180
<include name="loginWithCorrectCredentials"/>
7281
</methods>
@@ -113,7 +122,8 @@
113122
</class>
114123
</classes>
115124
</test>
116-
<test name="iFrame Tests" enabled="false"> <!--Disabled as the demo web page has errors and does not allow to type in the frame-->
125+
<test name="iFrame Tests"
126+
enabled="false"> <!--Disabled as the demo web page has errors and does not allow to type in the frame-->
117127
<parameter name="browser" value="chrome"/>
118128
<classes>
119129
<class name="io.github.mfaisalkhatri.tests.theinternet.IFrameTests">

0 commit comments

Comments
 (0)