Web-tables are the tables displayed on the pages, which has data in the form of rows and columns.
There are two types of web-tables, and the very first thing that we need to know before automating is, whether the web-table is static or dynamic.
- Static Web-tables: Number of rows and columns is defined.
- Dynamic Web-tables: Rows and column count is unknown and can be anything.
Once we know, that our web-table is dynamic or static. We can follow below steps for automation of dynamic table:
- use List <WebElement>
- Create an xpath for the table element till /tr and get a count of the same. (This will give us the row count)
- Similarly for columns the path should be till /th (From here we will get columns count)
- Once, we get the row and column count, simply apply loop with i rows and j columns and perform your thing.
- A sample code for the same would be:
for (int i =1; i<rows.size(); i++)
- List <WebElement rows = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute/"]/tbody/tr"));
- List <WebElement cols = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute/"]/tbody/tr/th"));
for (int j=0; j<cols.size(); j++)
here the xpath needs to //*[@id=/"table_id_or_available_attribute/"]/tbody/tr[i]/td[j]
If the table is static, we can follow the same process, however, the row/cols count step can be omitted. The row-count/col-count can be directly used in the above loop.
For better QA services we can make the XPath dynamic as mentioned inside the loop.