Yes, Selenium's @FindBy
annotation can accept locators from object repository files. You can store the locators in properties files and reference them in your Page Object Model (POM) classes using @FindBy
annotation.
Here's an example of how you can use @FindBy
annotation with locators from a properties file:
Suppose you have a properties file called locators.properties
with the following entries:
login.username = //input[@id='username']
login.password = //input[@id='password']
login.submitButton = //button[@id='submit']
Now you can create a Page Object class for your login page as follows:
public class LoginPage {
private WebDriver driver;
@FindBy(xpath = "${login.username}")
private WebElement usernameField;
@FindBy(xpath = "${login.password}")
private WebElement passwordField;
@FindBy(xpath = "${login.submitButton}")
private WebElement submitButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void login(String username, String password) {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
submitButton.click();
}
}
In this example, we are using the ${}
syntax to reference the locators stored in the locators.properties
file. The PageFactory.initElements()
method initializes the @FindBy
annotations with the actual locators from the properties file.
As for the practical approach, both options you mentioned are valid. You can choose to store the locators in properties files or directly in your POM classes, depending on your preference and project requirements. However, it's generally recommended to use POM classes to keep your code organized and maintainable.