我想扩展硒并实现如下所示:
public class IsExistsWebElement extends WebElement {
public boolean isExists() {
try {
this.getText();
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
然后像这样使用它(使用页面工厂):
public class HomePage{
@FindBy(class = "button")
private IsExistsWebElement button;
public HomePage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public boolean isButtonExists() {
return this.button.isExists();
}
}
实现这样的最佳方法是什么?
答案 0 :(得分:0)
如果您要寻找自定义实现,则必须在硒之上创建一个新框架,并且必须为大多数事情编写自己的实现。
例如RemoteWebElement
类实现了WebElement
接口。但是编写下面的代码并像访问WebElement那样具有访问权限并不容易
例如IsExistsWebElement element = driver.findElement(By.id("a"));
class IsExistsWebElement extends RemoteWebElement {
public boolean isExists() {
try {
this.getText();
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
如果您正在寻找一种基于硒的新框架,并且能够负担得起时间(可能需要3、6、9 ...个月,取决于资源)和成本,那么就乐于接受它。
OR
然后在页面对象模型中寻找以通用方式管理等待
例如
public BasePage(WebDriver driver) {
this.driver = driver;
wait = new WebDriverWait(driver, TIMEOUT, POLLING);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT), this);
}
public void waitForElementReady(WebElement element) {
try {
wait.until(ExpectedConditions.visibilityOf(element));
} catch (TimeoutException exception) {
System.out.println("Element didn't find in given time");
}
}