From d393c8a85de39e8e397d14b55349c73e6f02e1f4 Mon Sep 17 00:00:00 2001 From: mfaisalkhatri Date: Wed, 10 Jun 2026 11:05:06 +0530 Subject: [PATCH 1/5] added data table page and a test to verify row count --- .../pages/theinternet/DataTablePage.java | 33 ++++++++++++++ .../mfaisalkhatri/tests/DataTableTests.java | 43 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java create mode 100644 src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java diff --git a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java new file mode 100644 index 0000000..23c84fb --- /dev/null +++ b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java @@ -0,0 +1,33 @@ +package io.github.mfaisalkhatri.pages.theinternet; + +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 int getTotalRowsInTable (final int tableNumber) { + final Locator table = switch (tableNumber) { + case 1 -> tableOne (); + case 2 -> tableTwo (); + default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber); + }; + return table.getByRole(AriaRole.ROW) + .count (); + } + +} diff --git a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java new file mode 100644 index 0000000..6149939 --- /dev/null +++ b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java @@ -0,0 +1,43 @@ +package io.github.mfaisalkhatri.tests; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static org.testng.Assert.assertEquals; + +import com.microsoft.playwright.Browser; +import com.microsoft.playwright.BrowserType; +import com.microsoft.playwright.Page; +import com.microsoft.playwright.Playwright; +import io.github.mfaisalkhatri.pages.theinternet.DataTablePage; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class DataTableTests { + + private Playwright playwright; + private Page page; + private DataTablePage dataTablePage; + + @BeforeMethod + 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 testRowNumber() { + this.page.navigate("https://the-internet.herokuapp.com/tables"); + assertEquals(this.dataTablePage.getTotalRowsInTable (1),5); + assertEquals(this.dataTablePage.getTotalRowsInTable (2),5); + } + + @AfterMethod + public void tearDown () { + this.page.close (); + this.playwright.close (); + } + +} From 7f3ebf016199286a0b7f4341fe7a7a8422a937ce Mon Sep 17 00:00:00 2001 From: mfaisalkhatri Date: Fri, 12 Jun 2026 18:12:36 +0530 Subject: [PATCH 2/5] fixed the row count test assertions --- .../pages/theinternet/DataTablePage.java | 41 ++++++++++++++++--- .../mfaisalkhatri/tests/DataTableTests.java | 30 +++++++++++--- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java index 23c84fb..6299b66 100644 --- a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java +++ b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java @@ -13,21 +13,52 @@ public DataTablePage (final Page page) { } public Locator tableOne () { - return this.page.getByRole (AriaRole.TABLE).first (); + return this.page.getByRole (AriaRole.TABLE) + .first (); } public Locator tableTwo () { - return this.page.getByRole (AriaRole.TABLE).nth (1); + return this.page.getByRole (AriaRole.TABLE) + .nth (1); } - public int getTotalRowsInTable (final int tableNumber) { + public Locator getTotalRowsInTable (final Locator table) { + return table.getByRole (AriaRole.ROW); + } + + // public int getTotalRowsInTable (final int tableNumber) { + // final Locator table = switch (tableNumber) { + // case 1 -> tableOne (); + // case 2 -> tableTwo (); + // default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber); + // }; + // + // } + + public void tableRecords (final int tableNumber) { final Locator table = switch (tableNumber) { case 1 -> tableOne (); case 2 -> tableTwo (); default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber); }; - return table.getByRole(AriaRole.ROW) - .count (); + + final Locator rows = table.getByRole (AriaRole.ROW); + for (int i = 1; i < rows.count (); i++) { + System.out.println (rows.nth (i) + .innerText ()); + } + } + + public void getColumnsOfTableOne () { + final Locator columnHeader = tableOne ().getByRole (AriaRole.COLUMNHEADER); + for (int i = 0; i < columnHeader.count (); i++) { + columnHeader.nth (i) + .innerText (); + } + } + + private static Locator getRows (final Locator rows) { + return rows; } } diff --git a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java index 6149939..58dddaa 100644 --- a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java +++ b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java @@ -14,8 +14,8 @@ public class DataTableTests { - private Playwright playwright; - private Page page; + private Playwright playwright; + private Page page; private DataTablePage dataTablePage; @BeforeMethod @@ -27,13 +27,31 @@ public void setup () { this.page = browser.newPage (); this.dataTablePage = new DataTablePage (this.page); } + @Test - public void testRowNumber() { - this.page.navigate("https://the-internet.herokuapp.com/tables"); - assertEquals(this.dataTablePage.getTotalRowsInTable (1),5); - assertEquals(this.dataTablePage.getTotalRowsInTable (2),5); + public void testRowNumber () { + this.page.navigate ("https://the-internet.herokuapp.com/tables"); + assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (5); + assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (5); } + @Test + public void testPrintTableRecords () { + this.page.navigate ("https://the-internet.herokuapp.com/tables"); + System.out.println ("Printing records of Table 1"); + this.dataTablePage.tableRecords (1); + System.out.println ("Printing records of Table 2"); + this.dataTablePage.tableRecords (2); + } + +// @Test +// public void testTableOneColumnHeaders () { +// this.page.navigate ("https://the-internet.herokuapp.com/tables"); +// assertEquals (this.dataTablePage.getColumnsOfTableOne (), +// "Last Name\tFirst Name\tEmail\tDue\tWeb Site\tAction"); +// +// } + @AfterMethod public void tearDown () { this.page.close (); From 270e7152d85fb10f786e13b477e0df2a17bf8c56 Mon Sep 17 00:00:00 2001 From: mfaisalkhatri Date: Fri, 12 Jun 2026 18:51:14 +0530 Subject: [PATCH 3/5] fixed the all records printing test --- .../pages/theinternet/DataTablePage.java | 44 +++++-------------- .../mfaisalkhatri/tests/DataTableTests.java | 26 ++++++----- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java index 6299b66..a2e2bc1 100644 --- a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java +++ b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java @@ -1,5 +1,8 @@ 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; @@ -23,42 +26,17 @@ public Locator tableTwo () { } public Locator getTotalRowsInTable (final Locator table) { - return table.getByRole (AriaRole.ROW); - } - - // public int getTotalRowsInTable (final int tableNumber) { - // final Locator table = switch (tableNumber) { - // case 1 -> tableOne (); - // case 2 -> tableTwo (); - // default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber); - // }; - // - // } - - public void tableRecords (final int tableNumber) { - final Locator table = switch (tableNumber) { - case 1 -> tableOne (); - case 2 -> tableTwo (); - default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber); - }; - - final Locator rows = table.getByRole (AriaRole.ROW); - for (int i = 1; i < rows.count (); i++) { - System.out.println (rows.nth (i) - .innerText ()); - } + return table.locator ("tbody") + .getByRole (AriaRole.ROW); } - public void getColumnsOfTableOne () { - final Locator columnHeader = tableOne ().getByRole (AriaRole.COLUMNHEADER); - for (int i = 0; i < columnHeader.count (); i++) { - columnHeader.nth (i) - .innerText (); - } + public List getAllRecordsFromTable (final Locator table) { + return table.locator ("tbody") + .getByRole (AriaRole.ROW) + .allInnerTexts (); } - private static Locator getRows (final Locator rows) { - return rows; + public Locator getColumnHeadersOfTable (final Locator table) { + return table.getByRole (AriaRole.COLUMNHEADER); } - } diff --git a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java index 58dddaa..96964e8 100644 --- a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java +++ b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java @@ -31,26 +31,30 @@ public void setup () { @Test public void testRowNumber () { this.page.navigate ("https://the-internet.herokuapp.com/tables"); - assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (5); - assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (5); + assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (4); + assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (4); } @Test public void testPrintTableRecords () { this.page.navigate ("https://the-internet.herokuapp.com/tables"); System.out.println ("Printing records of Table 1"); - this.dataTablePage.tableRecords (1); + this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableOne ()) + .forEach (System.out::println); System.out.println ("Printing records of Table 2"); - this.dataTablePage.tableRecords (2); + this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableTwo ()) + .forEach (System.out::println); } -// @Test -// public void testTableOneColumnHeaders () { -// this.page.navigate ("https://the-internet.herokuapp.com/tables"); -// assertEquals (this.dataTablePage.getColumnsOfTableOne (), -// "Last Name\tFirst Name\tEmail\tDue\tWeb Site\tAction"); -// -// } + @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); + } + + @AfterMethod public void tearDown () { From 7e7daddc9bc0140d9ed684f6087f6f89d38f32d7 Mon Sep 17 00:00:00 2001 From: mfaisalkhatri Date: Fri, 12 Jun 2026 19:57:00 +0530 Subject: [PATCH 4/5] added testng xml --- .../pages/theinternet/DataTablePage.java | 8 ++++ .../mfaisalkhatri/tests/DataTableTests.java | 37 +++++++++++++++---- test-suites/testng-datatabletest.xml | 17 +++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 test-suites/testng-datatabletest.xml diff --git a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java index a2e2bc1..3d8d01b 100644 --- a/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java +++ b/src/test/java/io/github/mfaisalkhatri/pages/theinternet/DataTablePage.java @@ -39,4 +39,12 @@ public List getAllRecordsFromTable (final Locator table) { 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); + } } diff --git a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java index 96964e8..bb68a60 100644 --- a/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java +++ b/src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java @@ -1,15 +1,15 @@ package io.github.mfaisalkhatri.tests; import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; -import static org.testng.Assert.assertEquals; 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.AfterMethod; -import org.testng.annotations.BeforeMethod; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class DataTableTests { @@ -18,7 +18,7 @@ public class DataTableTests { private Page page; private DataTablePage dataTablePage; - @BeforeMethod + @BeforeTest public void setup () { this.playwright = Playwright.create (); final Browser browser = this.playwright.chromium () @@ -29,12 +29,20 @@ public void setup () { } @Test - public void testRowNumber () { + 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"); @@ -54,12 +62,25 @@ public void testTableOneColumnHeaders () { 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"); + } - @AfterMethod + @AfterTest public void tearDown () { this.page.close (); this.playwright.close (); } - -} +} \ No newline at end of file diff --git a/test-suites/testng-datatabletest.xml b/test-suites/testng-datatabletest.xml new file mode 100644 index 0000000..5b99a06 --- /dev/null +++ b/test-suites/testng-datatabletest.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file From 456c2ea92956c15adf0633eca28d107c95d5cecb Mon Sep 17 00:00:00 2001 From: mfaisalkhatri Date: Fri, 12 Jun 2026 19:57:36 +0530 Subject: [PATCH 5/5] added datatable testng to main testng xml --- test-suites/testng.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/test-suites/testng.xml b/test-suites/testng.xml index 319519a..119304a 100644 --- a/test-suites/testng.xml +++ b/test-suites/testng.xml @@ -14,5 +14,6 @@ + \ No newline at end of file