0
0
Selenium Javatesting~8 mins

Base test class pattern in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Base test class pattern
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── base/
                │   └── BaseTest.java
                ├── pages/
                │   ├── LoginPage.java
                │   └── HomePage.java
                ├── tests/
                │   ├── LoginTest.java
                │   └── HomeTest.java
                └── utils/
                    ├── DriverFactory.java
                    └── ConfigReader.java
Test Framework Layers
  • Base Layer: BaseTest.java - Contains setup and teardown methods, WebDriver initialization, and common test configurations.
  • Driver Layer: DriverFactory.java - Manages WebDriver creation and browser selection.
  • Page Objects: Classes like LoginPage.java and HomePage.java encapsulate page elements and actions.
  • Test Layer: Test classes like LoginTest.java extend BaseTest and contain test methods.
  • Utilities: Helper classes such as ConfigReader.java for reading config files.
Configuration Patterns
  • Use ConfigReader to load environment variables (e.g., URLs, credentials) from config.properties.
  • Browser type is specified in config and passed to DriverFactory to initialize the correct WebDriver.
  • BaseTest reads config and sets up WebDriver before tests, and quits driver after tests.
  • Use TestNG XML files or system properties to switch environments or browsers at runtime.
Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for test execution results.
  • Integrate with tools like Allure or ExtentReports for detailed HTML reports.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests automatically on code commits.
  • Reports are archived and emailed or published to dashboards for team visibility.
Best Practices
  • Single Responsibility: BaseTest handles setup/teardown only, not test logic.
  • Reusability: Common code in BaseTest avoids duplication in test classes.
  • Maintainability: Changes to setup (e.g., browser config) happen in one place.
  • Explicit Waits: Use waits in page objects, not in BaseTest, to keep layers clean.
  • Parameterization: Allow browser and environment to be passed dynamically for flexible runs.
Self Check

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

Key Result
Base test class centralizes WebDriver setup and teardown for reusable, maintainable Selenium tests in Java.