Skip to content

Commit

Permalink
fixed the locator strategy and tests for form authentication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaisalkhatri committed Oct 5, 2023
1 parent a1fb776 commit 2af1247
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.AriaRole;

public class FormAuthenticationPage {

Expand All @@ -10,7 +11,7 @@ public class FormAuthenticationPage {
public FormAuthenticationPage(final Page page) {
this.page = page;
}

public String pageHeader() {
return this.page.locator("h2").innerHTML();
}
Expand All @@ -23,7 +24,7 @@ private Locator passwordField() {
}

private Locator loginBtn() {
return this.page.getByText("Login");
return this.page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"));
}

public SecureAreaPage performLogin(final String userName, final String password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.AriaRole;

public class SecureAreaPage {

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

public String pageHeader() {
return this.page.getByText("Secure Area").textContent();
return this.page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("Secure Area").setExact(true)).textContent();
}

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

private Locator logoutBtn() {
public Locator logoutBtn() {
return this.page.locator("a.button");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.mfaisalkhatri.tests;

import io.github.mfaisalkhatri.pages.theinternet.FormAuthenticationPage;
import io.github.mfaisalkhatri.pages.theinternet.MainPage;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class FormAuthenticationTests extends BaseTest {

@Test
public void testLoginFunctionality() {

final String userName = "tomsmith";
final String password = "SuperSecretPassword!";
final var page = this.browserManager.getPage();
page.navigate("https://the-internet.herokuapp.com/");

final var mainPage = new MainPage(page);
mainPage.clickLink("Form Authentication");

final var formAuthenticationPage = new FormAuthenticationPage(page);
assertEquals(formAuthenticationPage.pageHeader(), "Login Page");
final var securePage = formAuthenticationPage.performLogin(userName, password);
assertTrue(securePage.successMessage().contains("You logged into a secure area!"));
assertEquals(securePage.pageHeader(), " Secure Area");
assertEquals(securePage.pageSubHeader(), "Welcome to the Secure Area. When you are done click logout below.");

securePage.logoutBtn().isEnabled();
securePage.logoutFromWebsite();
}
}
2 changes: 1 addition & 1 deletion test-suites/testng-browsernavigationtests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<classes>
<class name="io.github.mfaisalkhatri.tests.BrowserNavigationTests">
<methods>
<exclude name="testBrowserNavigation"/>
<include name="testBrowserNavigation"/>
<include name="testTitleAndCurrentUrl"/>
</methods>
</class>
Expand Down
14 changes: 14 additions & 0 deletions test-suites/testng-formauthenticationtests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Browser Navigation test suite">
<test name="Browser navigation tests using Playwright on Chrome">
<parameter name="browser" value="chrome"/>
<classes>
<class name="io.github.mfaisalkhatri.tests.FormAuthenticationTests">
<methods>
<include name="testLoginFunctionality"/>
</methods>
</class>
</classes>
</test>
</suite> <!-- Suite -->
1 change: 1 addition & 0 deletions test-suites/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<suite-files>
<suite-file path="testng-browsertest.xml"/>
<suite-file path="testng-browsernavigationtests.xml"/>
<suite-file path="testng-formauthenticationtests.xml"/>
</suite-files>
</suite> <!-- Suite -->

0 comments on commit 2af1247

Please sign in to comment.