Skip to content

Commit 62e5d2c

Browse files
Added example code for data table verification (#99)
* added data table page and a test to verify row count * fixed the row count test assertions * fixed the all records printing test * added testng xml * added datatable testng to main testng xml
1 parent afed654 commit 62e5d2c

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.mfaisalkhatri.pages.theinternet;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.microsoft.playwright.Locator;
7+
import com.microsoft.playwright.Page;
8+
import com.microsoft.playwright.options.AriaRole;
9+
10+
public class DataTablePage {
11+
12+
private final Page page;
13+
14+
public DataTablePage (final Page page) {
15+
this.page = page;
16+
}
17+
18+
public Locator tableOne () {
19+
return this.page.getByRole (AriaRole.TABLE)
20+
.first ();
21+
}
22+
23+
public Locator tableTwo () {
24+
return this.page.getByRole (AriaRole.TABLE)
25+
.nth (1);
26+
}
27+
28+
public Locator getTotalRowsInTable (final Locator table) {
29+
return table.locator ("tbody")
30+
.getByRole (AriaRole.ROW);
31+
}
32+
33+
public List<String> getAllRecordsFromTable (final Locator table) {
34+
return table.locator ("tbody")
35+
.getByRole (AriaRole.ROW)
36+
.allInnerTexts ();
37+
}
38+
39+
public Locator getColumnHeadersOfTable (final Locator table) {
40+
return table.getByRole (AriaRole.COLUMNHEADER);
41+
}
42+
43+
public Locator getCell (final Locator table, final int rowNumber, final int columnNumber) {
44+
return table.locator ("tbody")
45+
.getByRole (AriaRole.ROW)
46+
.nth (rowNumber)
47+
.locator ("td")
48+
.nth (columnNumber);
49+
}
50+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.github.mfaisalkhatri.tests;
2+
3+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
4+
5+
import com.microsoft.playwright.Browser;
6+
import com.microsoft.playwright.BrowserType;
7+
import com.microsoft.playwright.Page;
8+
import com.microsoft.playwright.Playwright;
9+
import com.microsoft.playwright.options.AriaRole;
10+
import io.github.mfaisalkhatri.pages.theinternet.DataTablePage;
11+
import org.testng.annotations.AfterTest;
12+
import org.testng.annotations.BeforeTest;
13+
import org.testng.annotations.Test;
14+
15+
public class DataTableTests {
16+
17+
private Playwright playwright;
18+
private Page page;
19+
private DataTablePage dataTablePage;
20+
21+
@BeforeTest
22+
public void setup () {
23+
this.playwright = Playwright.create ();
24+
final Browser browser = this.playwright.chromium ()
25+
.launch (new BrowserType.LaunchOptions ().setHeadless (false)
26+
.setChannel ("chrome"));
27+
this.page = browser.newPage ();
28+
this.dataTablePage = new DataTablePage (this.page);
29+
}
30+
31+
@Test
32+
public void testRowNumbers () {
33+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
34+
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (4);
35+
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (4);
36+
}
37+
38+
@Test
39+
public void testColumnNumbers () {
40+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
41+
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableOne ())).hasCount (6);
42+
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableTwo ())).hasCount (6);
43+
44+
}
45+
46+
@Test
47+
public void testPrintTableRecords () {
48+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
49+
System.out.println ("Printing records of Table 1");
50+
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableOne ())
51+
.forEach (System.out::println);
52+
System.out.println ("Printing records of Table 2");
53+
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableTwo ())
54+
.forEach (System.out::println);
55+
}
56+
57+
@Test
58+
public void testTableOneColumnHeaders () {
59+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
60+
final String[] expectedColumnHeaders = { "Last Name", "First Name", "Email", "Due", "Web Site", "Action" };
61+
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableOne ())).hasText (
62+
expectedColumnHeaders);
63+
}
64+
65+
@Test
66+
public void testTableData () {
67+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
68+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 0)).hasText ("Smith");
69+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 1)).hasText ("John");
70+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 2)).hasText ("jsmith@gmail.com");
71+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 3)).hasText ("$50.00");
72+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 4)).hasText (
73+
"http://www.jsmith.com");
74+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 5).getByRole (AriaRole.LINK).first ()).hasAttribute ("href", "#edit");
75+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableOne (), 0, 5).getByRole (AriaRole.LINK).nth(1)).hasAttribute ("href", "#delete");
76+
77+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableTwo (), 3, 1)).hasText ("Tim");
78+
assertThat (this.dataTablePage.getCell (this.dataTablePage.tableTwo (), 3, 1)).hasText ("Tim");
79+
}
80+
81+
@AfterTest
82+
public void tearDown () {
83+
this.page.close ();
84+
this.playwright.close ();
85+
}
86+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Working with Data Tables using Playwright Java">
4+
<test name="Data table tests">
5+
<classes>
6+
<class name="io.github.mfaisalkhatri.tests.DataTableTests">
7+
<methods>
8+
<include name="testRowNumbers"/>
9+
<include name="testColumnNumbers"/>
10+
<include name="testPrintTableRecords"/>
11+
<include name="testTableOneColumnHeaders"/>
12+
<include name="testTableData"/>
13+
</methods>
14+
</class>
15+
</classes>
16+
</test>
17+
</suite>

test-suites/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<suite-file path="testng-playwrightdemotests.xml"/>
1515
<suite-file path="testng-radiobuttons.xml"/>
1616
<suite-file path="testng-elementstatetest.xml"/>
17+
<suite-file path="testng-datatabletest.xml"/>
1718
</suite-files>
1819
</suite>

0 commit comments

Comments
 (0)