简体   繁体   English

如何在PageFactory @FindBy注释中使用属性文件值?

[英]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. 我最近开始将Selenium2与Page Object Pattern和Page Factory结合使用。 I have the WebElements declared with @FindBy Annotations that are initialized by the PageFactory when the class is initialized. 我有使用@FindBy Annotations声明的WebElements,它们在初始化类时由PageFactory初始化。 However I would like to use the @FindBy Annotation with a locators.properties file. 但是,我想将@FindBy Annotation与locators.properties文件一起使用。 Unfortunately I don't seem to be able to do this as the Annotation is restricted to only allowing Constant Expressions. 不幸的是我似乎无法做到这一点,因为Annotation仅限于允许Constant表达式。 This seems to be a limitation in Java Annotations in general but I'm just wondering if anybody has found a workaround for this. 这似乎是Java Annotations的一般限制,但我只是想知道是否有人为此找到了解决方法。 I would prefer to load the locators from an external source but then I would lose the benefits of using PageFactory. 我宁愿从外部源加载定位器,但我会失去使用PageFactory的好处。

public class LoginPage { 公共类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: 我想实现类似于此的东西,但我不能,因为Annotation不允许这样:

public class LoginPage { 公共类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 :) 您总是可以编写一种方法来将其作为补丁提供,然后将其应用到Selenium代码库中:)

it is possible and this link is indeed helpful Selenium Page Object Reuse ‌​se 这是可能的,这个链接确实有用Selenium Page Object Reuse se

I have managed to read locators from config, please take a look Selenium Page Object. 我已经设法从配置中读取定位器,请查看Selenium Page Object。 How to read @FindBy locator from external source? 如何从外部源读取@FindBy定位器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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