0
0
Selenium Javatesting~8 mins

@FindBy annotations in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @FindBy annotations
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── LoginPage.java
                ├── tests/
                │   └── LoginTest.java
                ├── utils/
                │   └── WebDriverManager.java
                └── config/
                    └── TestConfig.java
  
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverManager.java).
  • Page Objects: Classes representing web pages using @FindBy annotations to locate elements (e.g., LoginPage.java).
  • Tests: Test classes that use page objects to perform actions and assertions (e.g., LoginTest.java).
  • Utilities: Helper classes for common functions like waits, screenshots, or logging.
  • Configuration: Holds environment settings, browser types, and credentials (e.g., TestConfig.java).
Configuration Patterns

Use a TestConfig.java or config.properties file to manage:

  • Environment URLs (dev, test, prod)
  • Browser types (Chrome, Firefox, Edge)
  • User credentials (stored securely or mocked)

Load these settings at runtime to keep tests flexible and avoid hardcoding.

Test Reporting and CI/CD Integration
  • Use TestNG or JUnit reports for test execution results.
  • Integrate with CI/CD tools like Jenkins or GitHub Actions to run tests automatically on code changes.
  • Generate HTML reports with screenshots on failure for easy debugging.
Best Practices for @FindBy Annotations
  • Use @FindBy to locate elements clearly and avoid using brittle locators like absolute XPaths.
  • Keep locators private and expose only meaningful methods in page objects.
  • Use explicit waits in page object methods to handle dynamic page content.
  • Group related elements logically in page objects for readability and maintenance.
  • Initialize page objects with PageFactory.initElements(driver, this) to enable @FindBy processing.
Self Check

Where would you add a new @FindBy annotated element for a "Remember Me" checkbox on the login page?

Key Result
Use @FindBy annotations in page objects to locate web elements cleanly and maintainably.