Get values from each table rows

Viewed 157

I use this code to validate table with single row:

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']/div/div"));
        for (WebElement e : tableRows) {
            if (e.getAttribute("col-id").equalsIgnoreCase("projectLookupCodes")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("loadContainerSequence")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("67", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("packagedItemCount")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("23", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("locationName")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("Location", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("licensePlateLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("LP-12160", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("shipmentLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("520031", e.getText());
            }
        }

HTML code:

https://pastebin.com/TCRVLvpf

How I can verify each table row? Currently it's searching for first row and so on. How this code can be extended to search for several table rows values.

2 Answers

As I see you are very close to the solution.
All what you need is to retrieve all the rows from the table and for each row find all it's cells.
Now you can iterate over each cell in the internal loop and validate its` value.

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']//div[@role='row']"));
    for(WebElement row :tableRows){
        List<WebElement> rowCells = row.findElements(By.xpath(".//div"));
        for (WebElement e : rowCells) {
            if (e.getAttribute("col-id").equalsIgnoreCase("projectLookupCodes")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("loadContainerSequence")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("67", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("packagedItemCount")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("23", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("locationName")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("Location", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("licensePlateLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("LP-12160", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("shipmentLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("520031", e.getText());
            }
        
    }

See if this works:-

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']/div"));
        String projectLookupCodes = ".//div[@col-id='projectLookupCodes']";
        String loadContainerSequence = ".//div[@col-id='loadContainerSequence']";
        String packagedItemCount = ".//div[@col-id='packagedItemCount']";
        String locationName = ".//div[@col-id='locationName']";
        String licensePlateLookupCode = ".//div[@col-id='licensePlateLookupCode']";
        String shipmentLookupCode = ".//div[@col-id='shipmentLookupCode']";
        
        for(WebElement e: tableRows) {
            if(e.findElement(By.xpath(projectLookupCodes)).isDisplayed()) {
                System.out.println("ProjectLookupCodes: "+e.findElement(By.xpath(projectLookupCodes)).getText());
            }
            if(e.findElement(By.xpath(loadContainerSequence)).isDisplayed()) {
                System.out.println("loadContainerSequence: "+e.findElement(By.xpath(loadContainerSequence)).getText());
            }
            if(e.findElement(By.xpath(packagedItemCount)).isDisplayed()) {
                System.out.println("packagedItemCount: "+e.findElement(By.xpath(packagedItemCount)).getText());
            }
            if(e.findElement(By.xpath(locationName)).isDisplayed()) {
                System.out.println("locationName: "+e.findElement(By.xpath(locationName)).getText());
            }
            if(e.findElement(By.xpath(licensePlateLookupCode)).isDisplayed()) {
                System.out.println("licensePlateLookupCode: "+e.findElement(By.xpath(licensePlateLookupCode)).getText());
            }
            if(e.findElement(By.xpath(shipmentLookupCode)).isDisplayed()) {
                System.out.println("shipmentLookupCode: "+e.findElement(By.xpath(shipmentLookupCode)).getText());
            }
            System.out.println("=============================");
        }
Related