简体   繁体   中英

How to use property file value in PageFactory @FindBy annotation?

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

}

You can't at the moment.

You could always write a way to do it and offer it up as a patch that can be applied back into the Selenium codebase :)

it is possible and this link is indeed helpful Selenium Page Object Reuse ‌​se

I have managed to read locators from config, please take a look Selenium Page Object. How to read @FindBy locator from external source?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM