[英]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.我不确定如何断言我有一个 webelements 列表,其中每个 webelement inside
都有文本,我想验证这个 webelements 列表中是否存在一些文本。
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.