I've recently started using Selenium2 with the Page Object Pattern in conjunction with Page Factory. I have the WebElements declared with @FindBy Annotations that are initialized by the PageFactory when the class is initialized. However I would like to use the @FindBy Annotation with a locators.properties file. Unfortunately I don't seem to be able to do this as the Annotation is restricted to only allowing Constant Expressions. This seems to be a limitation in Java Annotations in general but I'm just wondering if anybody has found a workaround for this. I would prefer to load the locators from an external source but then I would lose the benefits of using PageFactory.
public class LoginPage {
protected WebDriver driver;
@FindBy(id = "username")
private WebElement usernameField;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "button_login")
private WebElement loginButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
I would like to implement something similar to this but I can't because the Annotation will not allow this:
public class LoginPage {
protected WebDriver driver;
Properties locators = new Properties();
@FindBy(id = locators.getProperty("login.usernameField"))
private WebElement usernameField;
@FindBy(id = locators.getProperty("login.passwordField"))
private WebElement passwordField;
@FindBy(id = locators.getProperty("login.loginButton"))
private WebElement loginButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
// Load the locators.properties File here
PageFactory.initElements(driver, this);
}
}