我面临一个问题,我想创建一个函数,等待列表中的文本“ Other”存在。我希望它等待30秒,然后每5秒它将再次进行呼叫并验证新列表。(最多5到6次,以验证文本是否在新列表中)。我已经通过将List转换为地图并检查所有匹配项(地图转换和检查正在工作)来完成验证。
问题出在等待机制上,我尝试创建复杂的流畅的等待,如果找到期望的文本,该等待也会收到布尔值,然后等待应该停止。但是我不知道如果在第一次或第二次尝试中找不到该文本时该返回什么,我希望它仍然等待并再次调用并再次验证。
我只希望在两种情况下停止等待:
是否可以设置等待Boolean == true的等待条件?我不确定条件Boolean>()是否正确,是否可以在wait.until中完成,如果可以工作
public void testrIntegrationfluent ()
{
WebElement element = BasePage.getWebElementByXPathWithWaitToClicable("//nz-select[@formcontrolname='selectedIntegrationTypes']/div");
element.click();
WebDriver driver2 = WebDriverMgr.getDriver();
Wait wait = new FluentWait(driver2)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new Function<WebDriver , Boolean>() {
public Boolean apply (WebDriver driver2) {
List<WebElement> dropdownOptions = driver2.findElements(By.xpath("//ul[contains(@class, 'ant-select-dropdown-menu')]/li"));
Boolean isExists = dropdownOptions.stream().map(WebElement::getText).anyMatch(text -> "Other".equals(text));
if (isExists.equals(true)) {
return isExists;
}
return ****** need to think what to put thatkeep waiting and perform call for list and validation reoccured ******
}
});
}
您可以使用声明的FluentWait
这样简化您的代码。您可以仅用此行替换包含wait.until(new Function<WebDriver , Boolean>()
功能的整个apply
块。您可以编写函数以直接查找带有文本li
的li
,而不是调用映射以获取所有Other
元素的文本并检查其他文本。
fluentWait.until (ExpectedConditions.presenceOfElement(By.xpath("//ul[contains(@class, 'ant-select-dropdown-menu')]/li[text()='Other'])));
此代码将等待,直到包含文本
li
的Other
出现。