@FindBy Annotation in Selenium

Learn via video courses
Topics Covered

Overview

The @FindBy annotation is used in Selenium to locate and identify web elements on a web page. It is typically used with the Page Object Model (POM) design pattern. Testers can easily access and interact with specific elements during test automation by specifying different attributes, such as id, name, class name, tag name, etc., within the @FindBy annotation. This annotation helps improve code readability, reusability, and maintainability by centralizing element locators within the page object classes.

What is Page Factory in Selenium?

Page Factory is a design pattern in Selenium, a popular framework for web application testing. It enhances the maintainability and readability of test automation code by providing a structured way to define and initialize web elements on a web page.

With Page Factory, we can leverage annotations and the concept of object-oriented programming to simplify the process of locating and interacting with web elements. It allows us to declare the web elements as fields within a page object class and initialize them automatically rather than manually using findElement whenever we want to interact with an element.

What is an Object Repository in Selenium?

In Selenium, an object repository is a central location or a collection that stores information about the web elements used in an application under test. It serves as a repository or storage for these elements' identification properties and locators.

The purpose of an object repository is to separate the object identification details from the test automation code. Instead of hard-coding the element locators directly in the test scripts, the object repository provides a more organized and maintainable approach to storing and managing the element information.

@FindBy Annotation in PageFactory Class

The @FindBy annotation in the PageFactory class is a powerful tool in Selenium for locating and initializing web elements within page object classes. It enhances the readability and maintainability of test automation code by providing a declarative way to define and access elements on a web page.

@FindBy annotation in pagefactory class

When using the @FindBy in Selenium in the PageFactory class, we can specify the locator strategy and value to uniquely identify the web element. The annotation is typically applied to class fields within a page object class, associating the element with a logical or variable name.

For example:

Code Explanation In the sample code above, the @FindBy annotation defines three web elements within the LoginPage class. The usernameField, passwordField, and loginButton fields are associated with the corresponding elements on the web page using different locator strategies (id and css).

  • To initialize the elements annotated with @FindBy, we must invoke the PageFactory.initElements() method from the PageFactory class. For example:
  • The initElements() method will initialize the web elements in the LoginPage object, establishing a connection between the elements declared in the class and the elements on the web page.
  • Once the elements are initialized, we can interact with them in our test automation code using the defined variables, such as loginPage.usernameField, loginPage.passwordField, or loginPage.loginButton.

Look at the code below which summarizes the entire process:

@FindBy annotation in pagefactory class code explaination

Difference between @FindBy And FindElement() Method in Selenium

Aspect@FindBy AnnotationfindElement() Method
Usage and SyntaxTo declare and initialize web elements within a page object class.Used to locate a single web element based on a locator strategy and value.
Example Syntax@FindBy(id = "myElement") private WebElement myElement;WebElement element = driver.findElement(By.id("myElement"));
FlexibilitySupports multiple locator strategies (e.g., id, name, class, xpath, css, etc.).Supports a wider range of locator strategies, including advanced techniques.
Code OrganizationPromotes code organization and readability by declaring elements as class fields with annotations.Allows ad-hoc element identification directly in test code without explicit declaration.

Conclusion

  • The @FindBy annotation is used to locate and initialize web elements within page object classes in Selenium.
  • It provides a declarative approach to define elements using different locator strategies such as id, name, class name, etc.
  • The annotation enhances code readability, reusability, and maintainability by centralizing element locators within the page object classes.
  • Page Factory is a design pattern in Selenium that leverages the @FindBy annotation to simplify the process of locating and interacting with web elements.
  • With Page Factory, web elements are declared fields within a page object class and are automatically initialized, reducing the need for manual element lookup using findElement().
  • Object repository in Selenium is a centralized storage or collection that holds information about the web elements used in an application under test.
  • The purpose of the object repository is to separate the element identification details from the test automation code, improving organization and maintenance.