[英]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.