如果该按钮不存在,则测试将挂起超过 5 秒。
DefaultElementLocator
中的 findElement()
方法被调用约 63 次!
block 嵌套越深,等待时间越长。
是否可以在 htmlElements 中以这种方式使用 block ? 我做错了什么?
@Test
public void myTestFunc() {
WebElement element = myPage.getMyForm()
.getSubForm()
.getButton()
.getWrappedElement();
try {
(new WebDriverWait(driver, 5))
.until(ExpectedConditions.visibilityOf(element));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class MyPage {
@FindBy(className = "...")
private MyForm myForm;
public MyPage(WebDriver driver){
PageFactory.initElements(new HtmlElementDecorator(driver), this);
}
public MyForm getMyForm() {
return myForm;
}
}
public class MyForm extends HtmlElement {
@FindBy(className = "...")
private MySubForm mySubForm;
public MySubForm getMySubForm() {
return mySubForm;
}
}
public class MySubForm extends HtmlElement {
@FindBy(className = "...")
private MyButtonWrap button;
public MyButtonWrap getButton() {
return button;
}
}
public class MyButtonWrap extends Button {
public MyButtonWrap(WebElement wrappedElement) {
super(wrappedElement);
}
// ...
}
最佳答案
我认为问题与默认设置为 5 秒的隐式等待有关。请参阅this issue了解更多详情。
我认为正在发生的是,当您尝试获取wrappedElement时:
myPage.getMyForm().getSubForm().getButton().getWrappedElement();
它隐式等待每个 @FindBy
5 秒。
尝试放置打印语句并查看时间花在哪里,例如:
public void myTestFunc() {
System.out.println("start");
element = myPage.getMyForm().getSubForm().getButton().getWrappedElement();
System.out.println("got element");
try {
(new WebDriverWait(driver, 5)).until(visibilityOf(element));
System.out.println("Finished waiting successfully");
} catch (Exception ex) {
ex.printStackTrace();
}
}
关于java - 等待 HtmlElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046427/