Showing posts with label Handle Webtable in Playwright. Show all posts
Showing posts with label Handle Webtable in Playwright. Show all posts

How to Handle Dynamic Webtable in Playwright















How to Handle Dynamic Web Tables in Playwright Java – Complete Guide with Examples

Dynamic web tables are one of the most common and important elements in modern web applications. Whether you are testing an e-commerce admin panel, a banking dashboard, or an HR management system, you will frequently encounter tables that load data dynamically. If you are working with Playwright Java, learning how to handle dynamic web tables effectively is essential for building robust and reliable automation scripts.

In this detailed, SEO-optimized guide, you will learn:

  • What a dynamic web table is

  • How it differs from a static table

  • Real-world examples of dynamic tables

  • Challenges in handling dynamic web tables

  • Step-by-step approach to handle dynamic tables in Playwright Java

  • A complete working Java example

  • Best practices for automation

  • Interview questions and troubleshooting tips

This guide is written in a simple, human-friendly way to help beginners and experienced testers alike.


What Is a Dynamic Web Table?

A dynamic web table is a table on a web page whose rows and columns are generated or updated dynamically at runtime. The structure and data inside the table are not fixed and may change depending on:

  • Backend data from a database

  • User actions such as searching or filtering

  • Pagination

  • Sorting columns

  • AJAX/API calls

Unlike static tables (where the data and structure remain constant), dynamic tables update automatically when data changes.


Real-World Examples of Dynamic Web Tables

Dynamic web tables are commonly used in:

  • E-commerce order management systems

  • Employee management systems

  • Banking transaction history pages

  • Analytics dashboards

  • CRM systems

For example:

  • An admin panel showing customer orders

  • An HR portal displaying employee records

  • A stock trading platform showing live price updates

The number of rows increases or decreases depending on backend data.


Static Table vs Dynamic Table

FeatureStatic TableDynamic Table
Data changesNoYes
Rows/columnsFixedVariable
UpdatesManualAutomatic (AJAX/API)
Automation complexityLowHigh

Dynamic tables require smarter automation logic because you cannot rely on fixed row numbers or static locators.


Challenges in Handling Dynamic Web Tables

Handling dynamic web tables in automation can be tricky. Here are the main challenges:

1. Changing Number of Rows and Columns

Since data changes dynamically, you cannot hardcode:

  • Row numbers

  • Column indexes

  • Cell positions

Your script must dynamically count rows and columns.


2. Dynamic IDs or Locators

Some applications generate auto-incremented IDs like:

row_12345 row_12346

You cannot rely on fixed IDs in such cases.


3. Pagination

Many tables display only 10–20 records per page. You must:

  • Navigate through pages

  • Check if “Next” button exists

  • Continue searching for required data


4. Sorting and Filtering

After clicking a column header:

  • Data order changes

  • Rows get reloaded

  • Previous locators may become stale

Your script must re-fetch table data after sorting.


5. AJAX Loading

Some tables load data asynchronously. If you try to access rows immediately, they may not yet exist.

Playwright’s auto-waiting helps, but you still need proper synchronization.


How to Handle Dynamic Web Tables in Playwright Java

Handling a dynamic web table involves the following logical steps:

  1. Identify the table structure

  2. Count rows dynamically

  3. Count columns dynamically

  4. Loop through rows and columns

  5. Extract or validate data

  6. Perform actions on specific rows

  7. Handle pagination (if needed)


Step-by-Step Approach in Playwright Java

Step 1: Launch Browser and Navigate

Open the webpage containing the dynamic table.

Step 2: Locate the Table

Use a CSS selector or XPath to identify the <table> element.

Step 3: Separate Header and Body

Typically:

  • <thead> contains column headers

  • <tbody> contains actual data

Step 4: Count Rows and Columns

Use Playwright’s count() method dynamically.

Step 5: Iterate Through Rows and Cells

Use nested loops to:

  • Traverse rows

  • Access each column

  • Extract cell text


Example Website for Dynamic Table

We will use this demo website:

https://demo.guru99.com/test/web-table-element.php

This page contains a dynamic stock price table.


Maven Dependency for Playwright Java

Add this dependency to your pom.xml:

<dependencies> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> </dependency> </dependencies>


Complete Java Code Example

Below is a working Playwright Java example to handle a dynamic web table:

import com.microsoft.playwright.*; public class DynamicWebTableExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium() .launch(new BrowserType.LaunchOptions().setHeadless(false)); Page page = browser.newPage(); // Navigate to dynamic table page page.navigate("https://demo.guru99.com/test/web-table-element.php"); // Locate table Locator table = page.locator("table.dataTable"); // Count rows int rowCount = table.locator("tbody > tr").count(); System.out.println("Total Rows: " + rowCount); // Count columns int colCount = table.locator("thead > tr > th").count(); System.out.println("Total Columns: " + colCount); // Loop through rows and columns for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { String cellText = table .locator("tbody > tr").nth(i) .locator("td").nth(j) .innerText(); System.out.print(cellText + " | "); } System.out.println(); } browser.close(); } } }


Code Explanation

Let’s break down the important parts.


(a) Navigate to Website

page.navigate("URL");

Opens the dynamic table webpage.


(b) Locate Web Table

Locator table = page.locator("table.dataTable");

Identifies the table using CSS selector.


(c) Count Rows Dynamically

table.locator("tbody > tr").count();
  • Fetches all rows inside <tbody>

  • Excludes header row

This ensures flexibility if rows increase or decrease.


(d) Count Columns

table.locator("thead > tr > th").count();

Gets number of header columns dynamically.


(e) Nested Loop for Data Extraction

We use:

.nth(i) .nth(j)

This allows:

  • Iterating row by row

  • Accessing each column dynamically


Searching for Specific Data in Dynamic Table

Often, we need to find specific data like:

  • Employee Name = “John”

  • Stock price > 500

  • Status = “Active”

Example logic:

for (int i = 0; i < rowCount; i++) { String name = table.locator("tbody > tr").nth(i) .locator("td").nth(0).innerText(); if (name.equals("Some Company")) { System.out.println("Data Found!"); break; } }


Handling Pagination in Dynamic Tables

If table has multiple pages:

  1. Check if “Next” button exists

  2. Click next

  3. Repeat row scanning

  4. Stop when data found

Playwright allows checking button visibility:

if (page.locator("a.next").isVisible()) { page.locator("a.next").click(); }

Repeat until desired data is found.


Handling Sorting and Filtering

When you click a column header:

page.locator("th:has-text('Price')").click();

After sorting:

  • Recalculate row count

  • Re-fetch table rows

  • Re-validate data

Never reuse old row references after table refresh.


Adding Assertions in Dynamic Table

You can validate table data like this:

if (cellText.contains("Active")) { System.out.println("Status Verified"); }

Or integrate with JUnit:

Assertions.assertTrue(cellText.contains("Active"));


Best Practices for Handling Dynamic Web Tables

To build stable automation:

1. Avoid Hardcoding Row Numbers

Never assume row 5 contains specific data.


2. Always Count Rows Dynamically

Use count() instead of fixed indexes.


3. Use Smart Locators

Prefer:

  • CSS selectors

  • Text-based locators

  • Relative locators


4. Handle AJAX Loading

Wait for table to load:

page.waitForSelector("table.dataTable");


5. Re-fetch Data After Sorting

Dynamic tables reload content. Always re-locate rows.


Common Interview Questions

Q1: What is a dynamic web table?

A table whose data and structure change dynamically at runtime.


Q2: How do you count rows in Playwright?

Using:

locator.count();


Q3: How do you handle pagination?

Loop through pages until required data is found.


Q4: Why avoid hardcoded row indexes?

Because dynamic tables change based on data.


Real-World Automation Scenarios

Dynamic web table handling is essential in:

  • HR employee record validation

  • E-commerce order verification

  • Banking transaction checks

  • Reporting dashboards

Without dynamic handling logic, your automation will fail when data changes.


Common Mistakes to Avoid

  • Hardcoding row numbers

  • Not waiting for table load

  • Ignoring pagination

  • Reusing stale locators after sorting


Conclusion

Handling dynamic web tables in Playwright Java is a crucial skill for any automation engineer. Since dynamic tables update based on backend data, searching, filtering, or pagination, your automation must adapt accordingly.

In this comprehensive guide, we covered:

  • What a dynamic web table is

  • Challenges in automation

  • Step-by-step handling approach

  • Complete Java example

  • Pagination and sorting handling

  • Best practices and interview questions

By mastering dynamic table handling, you can automate complex real-world applications with confidence.

If you are learning Playwright Java, practice with different table structures and build reusable utility methods for dynamic table operations. This will make your automation framework more powerful and scalable.


Suggested Posts:

1. BrowserContext in Playwright
2. File Upload in Playwright
3. Comma Selectors in Playwright
4. Thread Local in Playwright
5. Handle GET API in Playwright