I'm rewriting some Selenium tests according to the page object design pattern taking advantage of Seleniums' annotations. My problem is that I have some html select
elements whose option
elements are loaded dynamically. These are not available at the same time the select
s are.
The original code looks like:
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
}
The new code turns into something like:
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;
}
}
If I want to implement the wait in the new code, I have to use an xpath for the option
that includes the xpath for the select
, thus losing the advantage of using annotations to simplify the code:
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;
}
}
Is there any annotation I can use to wait until the options are loaded, or am I using this wrong?