繁体   English   中英

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

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

我最近开始将Selenium2与Page Object Pattern和Page Factory结合使用。 我有使用@FindBy Annotations声明的WebElements,它们在初始化类时由PageFactory初始化。 但是,我想将@FindBy Annotation与locators.properties文件一起使用。 不幸的是我似乎无法做到这一点,因为Annotation仅限于允许Constant表达式。 这似乎是Java Annotations的一般限制,但我只是想知道是否有人为此找到了解决方法。 我宁愿从外部源加载定位器,但我会失去使用PageFactory的好处。

公共类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); 
    } 

}

我想实现类似于此的东西,但我不能,因为Annotation不允许这样:

公共类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); 
    } 

}

你现在不能。

您总是可以编写一种方法来将其作为补丁提供,然后将其应用到Selenium代码库中:)

这是可能的,这个链接确实有用Selenium Page Object Reuse se

我已经设法从配置中读取定位器,请查看Selenium Page Object。 如何从外部源读取@FindBy定位器?

暂无
暂无

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

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