我正在根据页面对象设计模式利用Seleniums 的 annotations重写一些 Selenium 测试。我的问题是我有一些 htmlselect
元素,其option
元素是动态加载的。这些不能在select
s 的同时可用。
原始代码如下所示:
public void fillinForm() {
// Fill-in some fields ...
// Select dynamic loaded option
String optionXpath = "//*[@id='field']/option[text()='Software engineering']";
waitForElement(driver, By.xpath(optionXpath), SHORT_TIMEOUT_S);
driver.findElement(By.xpath(optionXpath)).click();
// Fill-in more fields, etc ...
}
// Selenium wait
public static void waitForElement(WebDriver driver, By by, int timeout) {
// implementation
}
新代码变成了这样的东西:
public void setUp() {
page = PageFactory.initElements(driver, Page.class);
}
public void fillinForm() {
page.setField("Software engineering");
}
public class Page {
private webElement field;
public Page setField(String byText) {
field.click();
String optionXpath = String.format("./option[text()='%s']", byText);
field.findElement(By.xpath(optionXpath)).click();
return this;
}
}
如果我想在新代码中实现等待,我必须使用option
包含 的 xpath 的 xpath select
,从而失去使用注释来简化代码的优势:
public void fillinForm() {
page.setField("Software engineering");
}
public class Page {
private webElement field;
public Page setField(String byText) {
field.click();
// Note that I'm now explicitly writing "field", exactly what I wanted
// to save using annotations and the PageFactory
String optionXpath = String.format("//select[@id='%s']/option[text()='%s']",
"field", byText);
field.findElement(By.xpath(optionXpath)).click();
return this;
}
}
是否有任何注释可以用来等到加载选项,还是我使用错了?