Skip to content

Commit 2af1247

Browse files
committed
fixed the locator strategy and tests for form authentication tests
1 parent a1fb776 commit 2af1247

6 files changed

Lines changed: 55 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.microsoft.playwright.Locator;
44
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.AriaRole;
56

67
public class FormAuthenticationPage {
78

@@ -10,7 +11,7 @@ public class FormAuthenticationPage {
1011
public FormAuthenticationPage(final Page page) {
1112
this.page = page;
1213
}
13-
14+
1415
public String pageHeader() {
1516
return this.page.locator("h2").innerHTML();
1617
}
@@ -23,7 +24,7 @@ private Locator passwordField() {
2324
}
2425

2526
private Locator loginBtn() {
26-
return this.page.getByText("Login");
27+
return this.page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"));
2728
}
2829

2930
public SecureAreaPage performLogin(final String userName, final String password) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.microsoft.playwright.Locator;
44
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.AriaRole;
56

67
public class SecureAreaPage {
78

@@ -12,7 +13,7 @@ public SecureAreaPage(final Page page) {
1213
}
1314

1415
public String pageHeader() {
15-
return this.page.getByText("Secure Area").textContent();
16+
return this.page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("Secure Area").setExact(true)).textContent();
1617
}
1718

1819
public String successMessage() {
@@ -23,7 +24,7 @@ public String pageSubHeader () {
2324
return this.page.locator("h4.subheader").innerText();
2425
}
2526

26-
private Locator logoutBtn() {
27+
public Locator logoutBtn() {
2728
return this.page.locator("a.button");
2829
}
2930

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)