Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.mfaisalkhatri.pages.theinternet;

import java.util.ArrayList;
import java.util.List;

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

public class DataTablePage {

private final Page page;

public DataTablePage (final Page page) {
this.page = page;
}

public Locator tableOne () {
return this.page.getByRole (AriaRole.TABLE)
.first ();
}

public Locator tableTwo () {
return this.page.getByRole (AriaRole.TABLE)
.nth (1);
}

public Locator getTotalRowsInTable (final Locator table) {
return table.locator ("tbody")
.getByRole (AriaRole.ROW);
}

public List<String> getAllRecordsFromTable (final Locator table) {
return table.locator ("tbody")
.getByRole (AriaRole.ROW)
.allInnerTexts ();
}

public Locator getColumnHeadersOfTable (final Locator table) {
return table.getByRole (AriaRole.COLUMNHEADER);
}

public Locator getCell (final Locator table, final int rowNumber, final int columnNumber) {
return table.locator ("tbody")
.getByRole (AriaRole.ROW)
.nth (rowNumber)
.locator ("td")
.nth (columnNumber);
}
}
86 changes: 86 additions & 0 deletions src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.mfaisalkhatri.tests;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.AriaRole;
import io.github.mfaisalkhatri.pages.theinternet.DataTablePage;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DataTableTests {

private Playwright playwright;
private Page page;
private DataTablePage dataTablePage;

@BeforeTest
public void setup () {
this.playwright = Playwright.create ();
final Browser browser = this.playwright.chromium ()
.launch (new BrowserType.LaunchOptions ().setHeadless (false)
.setChannel ("chrome"));
this.page = browser.newPage ();
this.dataTablePage = new DataTablePage (this.page);
}

@Test
public void testRowNumbers () {
this.page.navigate ("https://the-internet.herokuapp.com/tables");
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (4);
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (4);
}

@Test
public void testColumnNumbers () {
this.page.navigate ("https://the-internet.herokuapp.com/tables");
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableOne ())).hasCount (6);
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableTwo ())).hasCount (6);

}

@Test
public void testPrintTableRecords () {
this.page.navigate ("https://the-internet.herokuapp.com/tables");
System.out.println ("Printing records of Table 1");
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableOne ())
.forEach (System.out::println);
System.out.println ("Printing records of Table 2");
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableTwo ())
.forEach (System.out::println);
}

@Test
public void testTableOneColumnHeaders () {
this.page.navigate ("https://the-internet.herokuapp.com/tables");
final String[] expectedColumnHeaders = { "Last Name", "First Name", "Email", "Due", "Web Site", "Action" };
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableOne ())).hasText (
expectedColumnHeaders);
}

@Test
public void testTableData () {
this.page.navigate ("https://the-internet.herokuapp.com/tables");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 0)).hasText ("Smith");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 1)).hasText ("John");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 2)).hasText ("jsmith@gmail.com");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 3)).hasText ("$50.00");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 4)).hasText (
"http://www.jsmith.com");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 5).getByRole (AriaRole.LINK).first ()).hasAttribute ("href", "#edit");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 5).getByRole (AriaRole.LINK).nth(1)).hasAttribute ("href", "#delete");

assertThat (this.dataTablePage.getCell (this.dataTablePage.tableTwo (), 3, 1)).hasText ("Tim");
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableTwo (), 3, 1)).hasText ("Tim");
}

@AfterTest
public void tearDown () {
this.page.close ();
this.playwright.close ();
}
}
17 changes: 17 additions & 0 deletions test-suites/testng-datatabletest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Working with Data Tables using Playwright Java">
<test name="Data table tests">
<classes>
<class name="io.github.mfaisalkhatri.tests.DataTableTests">
<methods>
<include name="testRowNumbers"/>
<include name="testColumnNumbers"/>
<include name="testPrintTableRecords"/>
<include name="testTableOneColumnHeaders"/>
<include name="testTableData"/>
</methods>
</class>
</classes>
</test>
</suite>
1 change: 1 addition & 0 deletions test-suites/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<suite-file path="testng-playwrightdemotests.xml"/>
<suite-file path="testng-radiobuttons.xml"/>
<suite-file path="testng-elementstatetest.xml"/>
<suite-file path="testng-datatabletest.xml"/>
</suite-files>
</suite>
Loading