使用 Selenium 的一些默认功能怎么样:
/***
* An expectation for checking WebElement with given locator has text with a value as a part of it
* @param locator - used to find the element
* @param pattern - used as expected text matcher pattern
* @param timeout
* @return Boolean true when element has text value containing @value
*/
public boolean textMatches(By locator, Pattern pattern, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textMatches(locator, pattern));
} catch (Exception e) {
return false;
}
}
/***
* An expectation for checking WebElement with given locator has specific text
* @param locator - used to find the element
* @param value - used as expected text
* @param timeout
* @return Boolean true when element has text value equal to @value
*/
public boolean textToBe(By locator, String value, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textToBe(locator, value));
} catch (Exception e) {
return false;
}
}
/***
* An expectation for checking if the given text is present in the specified element.
* @param element - the WebElement
* @param text - to be present in the element
* @param timeout
* @return true once the element contains the given text
*/
public boolean textToBePresentInElement(WebElement element, String text, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textToBePresentInElement(element, text));
} catch (Exception e) {
return false;
}
}
/***
* An expectation for checking if the given text is present in the element that matches the given locator.
* @param locator - used to find the element
* @param text - to be present in the element found by the locator
* @param timeout
* @return true once the first element located by locator contains the given text
*/
public boolean textToBePresentInElementLocated(By locator, String text, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textToBePresentInElementLocated(locator, text));
} catch (Exception e) {
return false;
}
}
/***
* An expectation for checking if the given text is present in the specified elements value attribute.
* @param locator - used to find the element
* @param text - to be present in the value attribute of the element found by the locator
* @param timeout
* @return true once the value attribute of the first element located by locator contains the given text
*/
public boolean textToBePresentInElementValue(By locator, String text, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textToBePresentInElementValue(locator, text));
} catch (Exception e) {
return false;
}
}
/***
* An expectation for checking if the given text is present in the specified elements value attribute.
* @param element - the WebElement
* @param text - to be present in the element's value attribute
* @param timeout
* @return true once the element's value attribute contains the given text
*/
public boolean textToBePresentInElementValue(WebElement element, String text, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.textToBePresentInElementValue(element, text));
} catch (Exception e) {
return false;
}
}
与
private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
return new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
}
或者使用其他选项:
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public boolean apply(WebDriver driver) {
return driver.findElement(By.id("foo")).getText().length > 0;
}
});