0
0
Selenium Javatesting~8 mins

PageFactory initialization in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - PageFactory initialization
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── LoginPage.java
                ├── tests/
                │   └── LoginTest.java
                ├── utils/
                │   └── WebDriverManager.java
                └── config/
                    └── ConfigReader.java
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverManager.java).
  • Page Objects: Classes representing web pages using PageFactory for element initialization (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 reading configs, waits, or logging.
  • Configuration: Handles environment variables, browser settings, and credentials (e.g., ConfigReader.java).
Configuration Patterns
  • Use config.properties file to store environment URLs, browser types, and credentials.
  • ConfigReader.java reads properties and provides getters for test classes.
  • Pass browser type as a system property or environment variable to support multiple browsers.
  • Initialize WebDriver in WebDriverManager.java based on config settings.
  • Use @BeforeMethod and @AfterMethod in test classes to setup and quit WebDriver.
Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for test execution results.
  • Integrate with Allure or ExtentReports for detailed, user-friendly reports.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests on code commits.
  • Generate and archive reports as build artifacts for easy access.
  • Fail builds automatically if critical tests fail to ensure quality gates.
Best Practices for PageFactory Initialization
  • Use PageFactory.initElements(driver, this) inside page object constructors to initialize web elements.
  • Keep locators private and final to encapsulate page details.
  • Use meaningful and stable locators (id, name, CSS selectors) to reduce flakiness.
  • Separate page actions (methods) from test assertions for clarity and reuse.
  • Use explicit waits in page methods to handle dynamic elements instead of implicit waits.
Self Check Question

Where in this folder structure would you add a new page object class for the "Dashboard" page?

Key Result
Use PageFactory in page object classes to initialize web elements cleanly and maintainably.