Skip to content

Commit 270e715

Browse files
committed
fixed the all records printing test
1 parent 7f3ebf0 commit 270e715

2 files changed

Lines changed: 26 additions & 44 deletions

File tree

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.mfaisalkhatri.pages.theinternet;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.microsoft.playwright.Locator;
47
import com.microsoft.playwright.Page;
58
import com.microsoft.playwright.options.AriaRole;
@@ -23,42 +26,17 @@ public Locator tableTwo () {
2326
}
2427

2528
public Locator getTotalRowsInTable (final Locator table) {
26-
return table.getByRole (AriaRole.ROW);
27-
}
28-
29-
// public int getTotalRowsInTable (final int tableNumber) {
30-
// final Locator table = switch (tableNumber) {
31-
// case 1 -> tableOne ();
32-
// case 2 -> tableTwo ();
33-
// default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber);
34-
// };
35-
//
36-
// }
37-
38-
public void tableRecords (final int tableNumber) {
39-
final Locator table = switch (tableNumber) {
40-
case 1 -> tableOne ();
41-
case 2 -> tableTwo ();
42-
default -> throw new IllegalStateException ("Invalid Table Number: " + tableNumber);
43-
};
44-
45-
final Locator rows = table.getByRole (AriaRole.ROW);
46-
for (int i = 1; i < rows.count (); i++) {
47-
System.out.println (rows.nth (i)
48-
.innerText ());
49-
}
29+
return table.locator ("tbody")
30+
.getByRole (AriaRole.ROW);
5031
}
5132

52-
public void getColumnsOfTableOne () {
53-
final Locator columnHeader = tableOne ().getByRole (AriaRole.COLUMNHEADER);
54-
for (int i = 0; i < columnHeader.count (); i++) {
55-
columnHeader.nth (i)
56-
.innerText ();
57-
}
33+
public List<String> getAllRecordsFromTable (final Locator table) {
34+
return table.locator ("tbody")
35+
.getByRole (AriaRole.ROW)
36+
.allInnerTexts ();
5837
}
5938

60-
private static Locator getRows (final Locator rows) {
61-
return rows;
39+
public Locator getColumnHeadersOfTable (final Locator table) {
40+
return table.getByRole (AriaRole.COLUMNHEADER);
6241
}
63-
6442
}

src/test/java/io/github/mfaisalkhatri/tests/DataTableTests.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ public void setup () {
3131
@Test
3232
public void testRowNumber () {
3333
this.page.navigate ("https://the-internet.herokuapp.com/tables");
34-
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (5);
35-
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (5);
34+
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableOne ())).hasCount (4);
35+
assertThat (this.dataTablePage.getTotalRowsInTable (this.dataTablePage.tableTwo ())).hasCount (4);
3636
}
3737

3838
@Test
3939
public void testPrintTableRecords () {
4040
this.page.navigate ("https://the-internet.herokuapp.com/tables");
4141
System.out.println ("Printing records of Table 1");
42-
this.dataTablePage.tableRecords (1);
42+
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableOne ())
43+
.forEach (System.out::println);
4344
System.out.println ("Printing records of Table 2");
44-
this.dataTablePage.tableRecords (2);
45+
this.dataTablePage.getAllRecordsFromTable (this.dataTablePage.tableTwo ())
46+
.forEach (System.out::println);
4547
}
4648

47-
// @Test
48-
// public void testTableOneColumnHeaders () {
49-
// this.page.navigate ("https://the-internet.herokuapp.com/tables");
50-
// assertEquals (this.dataTablePage.getColumnsOfTableOne (),
51-
// "Last Name\tFirst Name\tEmail\tDue\tWeb Site\tAction");
52-
//
53-
// }
49+
@Test
50+
public void testTableOneColumnHeaders () {
51+
this.page.navigate ("https://the-internet.herokuapp.com/tables");
52+
final String[] expectedColumnHeaders = { "Last Name", "First Name", "Email", "Due", "Web Site", "Action" };
53+
assertThat (this.dataTablePage.getColumnHeadersOfTable (this.dataTablePage.tableOne ())).hasText (
54+
expectedColumnHeaders);
55+
}
56+
57+
5458

5559
@AfterMethod
5660
public void tearDown () {

0 commit comments

Comments
 (0)