Search code examples
javaseleniumhtml-tabletablecolumn

How to verify the whole column has the same values


I need to verify that a column in web table contains the exact same value for all rows.

I've done some coding but I'm looking for info about how to improve the performance:

  • I've tried a for loop, using getText method for each row and comparing the value to an expected string:
      List<WebElement> rows = driver.findElements(By.xpath(xxxx))

      for(WebElement e : rows) {
          if(e.getText() != expectedString) {
              throwError
          }
      }
  • I've also tried Java stream and lambda expressions, however at the end the Collector part is taking a lot of time to process:
        List<String> fetchedValues = rows
           .stream()
           .filter(e -> e.getText() != expectedString)
           .collect(Collectors.toList());

I would like to get info if at least one of the rows does not contain expected String.

Is there any way e.g. with Java to read all values in a given column and load it to ArrayList at once?


Solution

  • You can try forEach method described here

    Here you have an example:

        @Test
        public void sTest () {
            WebDriverManager.chromedriver().setup();
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.techbeamers.com/handling-html-tables-selenium-webdriver/");
            List<WebElement> el = driver.findElements(By.xpath("//*[@id='post-3500']/div[1]/div/p[3]/a"));
            el.forEach((e)-> {
                if(!e.getText().equals(expectedString)) throwError
            });
            driver.quit();
    
        }