Locator Properties with Selenium2 @FindBy Annotation

176 views
Skip to first unread message

Aidan

unread,
Sep 30, 2010, 10:29:36 AM9/30/10
to Selenium Users
Hi,

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);
}
}

valm...@gmail.com

unread,
Aug 20, 2013, 12:18:32 PM8/20/13
to seleniu...@googlegroups.com
Hi Aidan,
I know you posted this a long time ago but I was wondering if you ever found a workaround for it as
I'm facing the same problem.
I tried the method below but it did't work.
 
 public final static String PERSONAL_ID = OR.getProperty("LOGIN_PERSONAL_ID");
@FindBy(id = PERSONAL_ID) private WebElement _personalIDField;
Thanks
ValW

robbiewinston

unread,
Aug 21, 2013, 5:22:30 AM8/21/13
to seleniu...@googlegroups.com
You don't have to use the Page Factory to implement the page object design pattern.

For example you can hand craft page objects as POJOs.\

eg.

public class LoginPage { 

        protected WebDriver driver; 

        Properties locators = new Properties(); 

private By usernameIdentifier = By.cssSelector("#" +
locators.getProperty("login.usernameField"));
 
        public LoginPage(WebDriver driver) { 
                this.driver = driver; 
               
        } 

public void setUsername(String username) {

this.driver.findElement(usernameIdentifier).sendKeys(username);


public String getUsername() {

return this.driver.findElement(usernameIdentifier).getText();
}
 
 
}

Reply all
Reply to author
Forward
0 new messages