简体   繁体   中英

Assert that element from List<webelement> contains some text

I'm not sure how can I assert that I have a list of webelements where each webelement has the text inside , And I want to verify that on this list of webelements exists some text.

I not sure how this should looks assertion in this case.

Simple get stream, map to string (text) and check if any matches:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .anyMatch(text -> "inside".equals(text));

To be safe filter out null's and trim:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .filter(Objects::nonNull)
  .map(String::trim)
  .anyMatch(text -> "inside".equals(text));

You can do it like this:

public boolean isElementsContainsProvidedText(String text){

    List<WebElement> elements = driver.findElements(By.xpath("your_xpath_for_list"));

    return elements.stream().anyMatch(e->e.gettext().trim().equals(text));     

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM