1

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?

Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
Tom
  • 35
  • 6
  • Also consider having a look at : https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Arnaud Jul 05 '19 at 10:11
  • 1
    `e.getText() != expectedString` <- don't do that. Use `.equals()` to compare strings. – JonK Jul 05 '19 at 10:11
  • Try this : if(! e.getText().contains(expectedString)) – frianH Jul 05 '19 at 10:32
  • thanks guys, I forgot about the equals / contains method. However the question here is more: is it possible to get the text value from a list of webelements at once, I mean without having to use for-loop? – Tom Jul 05 '19 at 10:52

1 Answers1

1

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();

    }
cheparsky
  • 514
  • 6
  • 20