0
0
Selenium Javatesting~8 mins

Page class design in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Page class design
Folder Structure
src/
  test/
    java/
      com/
        example/
          tests/
            LoginTest.java
            CheckoutTest.java
          pages/
            LoginPage.java
            HomePage.java
            CheckoutPage.java
          utils/
            DriverFactory.java
            WaitUtils.java
          config/
            ConfigReader.java
            TestData.java
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., DriverFactory.java).
  • Page Objects: Classes representing web pages (e.g., LoginPage.java) with locators and methods to interact with page elements.
  • Tests: Test classes that use page objects to perform test scenarios (e.g., LoginTest.java).
  • Utilities: Helper classes for waits, logging, or common actions (e.g., WaitUtils.java).
  • Configuration: Classes or files to manage environment settings, test data, and credentials (e.g., ConfigReader.java).
Configuration Patterns
  • Environment Properties: Use property files or environment variables to store URLs, browser types, and credentials.
  • ConfigReader.java: Reads configuration values at runtime to allow easy switching between environments (dev, test, prod).
  • DriverFactory.java: Uses config to initialize the correct browser driver (Chrome, Firefox, etc.).
  • Test Data: Store test data separately in files or classes (e.g., TestData.java) to keep tests clean and maintainable.
Test Reporting and CI/CD Integration
  • TestNG Reports: Use TestNG framework for running tests and generating HTML reports.
  • Logging: Add logs in page methods and tests for easier debugging.
  • CI/CD: Integrate with Jenkins or GitHub Actions to run tests automatically on code changes.
  • Failure Screenshots: Capture screenshots on test failure for better analysis.
Best Practices for Page Class Design
  • Single Responsibility: Each page class should represent one page and contain only its elements and actions.
  • Encapsulation: Keep locators private and expose only meaningful methods to interact with the page.
  • Readable Methods: Use clear method names like login(String user, String pass) instead of exposing low-level clicks.
  • Reusability: Avoid duplicating code by creating common methods in base page classes or utilities.
  • Explicit Waits: Use waits inside page methods to handle dynamic page elements reliably.
Self Check

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

Answer: Add ProfilePage.java inside the src/test/java/com/example/pages/ folder.

Key Result
Use page classes to encapsulate web page elements and actions for clean, maintainable Selenium tests in Java.