【问题标题】:How to use property file value in PageFactory @FindBy annotation?如何在 PageFactory @FindBy 注释中使用属性文件值?
【发布时间】:2012-10-30 08:53:15
【问题描述】:

我最近开始使用 Selenium2 和页面对象模式 与 Page Factory 结合使用。我有声明的 WebElements @FindBy 由 PageFactory 初始化的注解,当 类被初始化。但是我想使用@FindBy 带有 locators.properties 文件的注释。可惜我好像没有 能够做到这一点,因为注释仅限于允许 常量表达式。这似乎是Java中的一个限制 一般的注释,但我只是想知道是否有人找到了 解决方法。我宁愿从 外部来源,但我会失去使用的好处 页面工厂。

公共类登录页面{

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

}

我想实现类似的东西,但我不能 因为注释不允许这样做:

公共类登录页面{

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

}

【问题讨论】:

  • 我认为不可能。提供给任何注释的值是在编译时而不是在运行时计算的。但是,如果您的意图是将定位器存储在属性文件中,那么您可以使用 By 子句...这是一个示例 C# 代码: public By UserName() { return By.Id(locators.getProperty("login.usernameField") ); } 以后你可以使用 Driver.FindElement(UserName()).SendKeys("")..
  • 看看这个,你可能会有一些想法stackoverflow.com/questions/3981498/selenium-page-object-reuse

标签: java webdriver


【解决方案1】:

你现在不能。

你总是可以写一个方法来做这件事,并将它作为一个补丁提供,可以应用回 Selenium 代码库:)

【讨论】:

    【解决方案2】:

    这是可能的,这个链接确实很有帮助Selenium Page Object Reuse‌​se

    我已经设法从配置中读取定位器,请看一下 Selenium Page Object. How to read @FindBy locator from external source?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 2013-05-06
      • 2016-09-08
      • 2016-02-08
      • 2018-07-20
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多