Site icon Vinsguru

Selenium WebDriver – How To Query HTML Web Table

Overview:

As an automation engineer , often, we might have to find / interact with some web elements of a HTML Web Table based on certain conditions. There are various approaches to do that.  We will see how to use Java Streams to play with HTML tables.

Sample HTML Table:

I create a Simple HTML page with below table.

 

Our requirement is to select check boxes based on some conditions like – DOB should match ’01/01/1970′ or Country should be ‘USA’ etc – basically any given condition at run time.

Filter Rows Using Java Streams:

WebDriver driver = new ChromeDriver();
driver.get("sample.html");
WebElement table = driver.findElement(By.tagName("table"));
List<String> columnNames = table.findElements(By.tagName("th"))   // get table headers
                                        .stream()
                                        .map(WebElement::getText)        // get the text
                                        .map(String::trim)               // trim - no space
                                        .collect(Collectors.toList());   // collect to a list
// columnNames prints [Row Index, Name, DOB, City, Country, Checkbox]
Map<String, Integer> columnMap = IntStream.range(0, columnNames.size())
                                               .boxed()
                                               .collect(Collectors.toMap(columnNames::get, 
                                                                         Function.identity()));

//columnMap {Row Index=0, Name=1, DOB=2, City=3, Country=4, Checkbox=5}
table.findElements(By.tagName("tr"))  //get all rows
             .stream()
             .skip(1)                 // skip first row as we do not need header
             .map(tr -> tr.findElements(By.tagName("td")))   // get all cells for each rows
             .filter(tds -> tds.get(columnMap.get("DOB")).getText().equals("01/01/1960"))   // find the row which has DOB as 01/01/1960
             .map(tds -> tds.get(columnMap.get("Checkbox")))   // get cell which contains checkbox
             .map(td -> td.findElement(By.tagName("input")))   // get checkbox
             .forEach(WebElement::click);                      // click checkbox
private void filterRows(Predicate<List<WebElement>> compositeCheck){
        
        table.findElements(By.tagName("tr")) 
            .stream()
            .skip(1)        
            .map(tr -> tr.findElements(By.tagName("td"))) 
            .filter(compositeCheck)  // passed as argument
            .map(tds -> tds.get(columnMap.get("Checkbox"))) 
            .map(td -> td.findElement(By.tagName("input")))
            .forEach(WebElement::click);
        
}
Predicate<List<WebElement>> dobCheck = (tds) -> tds.get(columnMap.get("DOB")).getText().equals("01/01/1960");   // check if DOB is '01/01/1960'
Predicate<List<WebElement>> countryCheck = (tds) -> tds.get(columnMap.get("Country")).getText().equals("USA");  // check Country is 'USA'
Predicate<List<WebElement>> cityCheck = (tds) -> tds.get(columnMap.get("City")).getText().equals("Oran");       // check City is 'Oran'
dobCheck.or(cityCheck)

dobCheck.and(countryCheck)

dobCheck.or(countryCheck).or(cityCheck)
private Predicate<List<WebElement>> getPredicate(String col, String text){
        return (tds) -> tds.get(columnMap.get(col)).getText().equals(text);
}

getPredicate("DOB", "01/01/1960").and(getPredicate("City", "Oran")) 

 

Happy Testing & Subscribe 🙂

 

 

Share This:

Exit mobile version