0

I am wondering whether @FindBy annotation accepts xpath from properties or object repository file? and also please let me know , what is the practical approach ? a. Using Page object model having all CSS selectors and Methods to perform actions in page object. b. Having CSS selectors as key value pair in properties file and refer them in page object.

Langauge used is java

Thanks in Advance.!

2 Answers 2

0

I am wondering whether @FindBy annotation accepts xpath from properties or object repository file?

No, As far as I know, java annotations only accepts constant values like string literals etc. So If you use @FindBy annotation, you need to put selector as a string literal like:

@FindBy(css = "tbody[id$='some_css_in_here']")

About second question

Adding all selectors in properties file seemed much confusing to me since, after some time, too much selectors will be displayed in single properties file and you may have a hard time to remember which css selector belongs to which page (you can add comments to the properties file, but it is an additional work)

Although, putting css selectors into the page objects is much more readable for me, since during project, it will be easier to identify which css selectors belong to which page.

Like: Css selectors related with login page, will be located in LoginPage.java page object class.

Regards..

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.