Skip to content

Commit d8d7c92

Browse files
Merge pull request #53 from mfaisalkhatri/issue-6
Added Form Authentication tests as example code for typing in the field
2 parents e5ba9f5 + 2af1247 commit d8d7c92

8 files changed

Lines changed: 123 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class ChallengingDomPage {
66

7-
final Page page;
7+
private final Page page;
88

99
public ChallengingDomPage(final Page page) {
1010
this.page = page;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.mfaisalkhatri.pages.theinternet;
2+
3+
import com.microsoft.playwright.Locator;
4+
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.AriaRole;
6+
7+
public class FormAuthenticationPage {
8+
9+
private final Page page;
10+
11+
public FormAuthenticationPage(final Page page) {
12+
this.page = page;
13+
}
14+
15+
public String pageHeader() {
16+
return this.page.locator("h2").innerHTML();
17+
}
18+
private Locator userNameField() {
19+
return this.page.getByLabel("Username");
20+
}
21+
22+
private Locator passwordField() {
23+
return this.page.getByLabel("Password");
24+
}
25+
26+
private Locator loginBtn() {
27+
return this.page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"));
28+
}
29+
30+
public SecureAreaPage performLogin(final String userName, final String password) {
31+
userNameField().clear();
32+
userNameField().fill(userName);
33+
passwordField().clear();
34+
passwordField().fill(password);
35+
loginBtn().click();
36+
return new SecureAreaPage(this.page);
37+
}
38+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class MainPage {
66

7-
final Page page;
7+
private final Page page;
88

99
public MainPage(final Page page) {
1010
this.page = page;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.mfaisalkhatri.pages.theinternet;
2+
3+
import com.microsoft.playwright.Locator;
4+
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.AriaRole;
6+
7+
public class SecureAreaPage {
8+
9+
private final Page page;
10+
11+
public SecureAreaPage(final Page page) {
12+
this.page = page;
13+
}
14+
15+
public String pageHeader() {
16+
return this.page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("Secure Area").setExact(true)).textContent();
17+
}
18+
19+
public String successMessage() {
20+
return this.page.locator("#flash").textContent();
21+
}
22+
23+
public String pageSubHeader () {
24+
return this.page.locator("h4.subheader").innerText();
25+
}
26+
27+
public Locator logoutBtn() {
28+
return this.page.locator("a.button");
29+
}
30+
31+
public void logoutFromWebsite() {
32+
logoutBtn().click();
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.mfaisalkhatri.tests;
2+
3+
import io.github.mfaisalkhatri.pages.theinternet.FormAuthenticationPage;
4+
import io.github.mfaisalkhatri.pages.theinternet.MainPage;
5+
import org.testng.annotations.Test;
6+
7+
import static org.testng.Assert.assertEquals;
8+
import static org.testng.Assert.assertTrue;
9+
10+
public class FormAuthenticationTests extends BaseTest {
11+
12+
@Test
13+
public void testLoginFunctionality() {
14+
15+
final String userName = "tomsmith";
16+
final String password = "SuperSecretPassword!";
17+
final var page = this.browserManager.getPage();
18+
page.navigate("https://the-internet.herokuapp.com/");
19+
20+
final var mainPage = new MainPage(page);
21+
mainPage.clickLink("Form Authentication");
22+
23+
final var formAuthenticationPage = new FormAuthenticationPage(page);
24+
assertEquals(formAuthenticationPage.pageHeader(), "Login Page");
25+
final var securePage = formAuthenticationPage.performLogin(userName, password);
26+
assertTrue(securePage.successMessage().contains("You logged into a secure area!"));
27+
assertEquals(securePage.pageHeader(), " Secure Area");
28+
assertEquals(securePage.pageSubHeader(), "Welcome to the Secure Area. When you are done click logout below.");
29+
30+
securePage.logoutBtn().isEnabled();
31+
securePage.logoutFromWebsite();
32+
}
33+
}

test-suites/testng-browsernavigationtests.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<classes>
77
<class name="io.github.mfaisalkhatri.tests.BrowserNavigationTests">
88
<methods>
9-
<exclude name="testBrowserNavigation"/>
9+
<include name="testBrowserNavigation"/>
1010
<include name="testTitleAndCurrentUrl"/>
1111
</methods>
1212
</class>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Browser Navigation test suite">
4+
<test name="Browser navigation tests using Playwright on Chrome">
5+
<parameter name="browser" value="chrome"/>
6+
<classes>
7+
<class name="io.github.mfaisalkhatri.tests.FormAuthenticationTests">
8+
<methods>
9+
<include name="testLoginFunctionality"/>
10+
</methods>
11+
</class>
12+
</classes>
13+
</test>
14+
</suite> <!-- Suite -->

test-suites/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<suite-files>
55
<suite-file path="testng-browsertest.xml"/>
66
<suite-file path="testng-browsernavigationtests.xml"/>
7+
<suite-file path="testng-formauthenticationtests.xml"/>
78
</suite-files>
89
</suite> <!-- Suite -->

0 commit comments

Comments
 (0)