0
0
Selenium Javatesting~8 mins

Action methods per page in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Action methods per page
Folder Structure
src
└── test
    └── java
        └── com
            └── example
                ├── pages
                │   ├── LoginPage.java
                │   ├── HomePage.java
                │   └── ProfilePage.java
                ├── tests
                │   ├── LoginTest.java
                │   └── ProfileTest.java
                ├── utils
                │   ├── DriverFactory.java
                │   └── WaitUtils.java
                └── config
                    └── ConfigReader.java
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., DriverFactory.java).
  • Page Objects Layer: Contains page classes (e.g., LoginPage.java) with locators and action methods that perform user interactions like clicking buttons or entering text.
  • Test Layer: Contains test classes (e.g., LoginTest.java) that call page object action methods to perform test scenarios.
  • Utilities Layer: Helper classes for waits, logging, or common utilities (e.g., WaitUtils.java).
  • Configuration Layer: Reads environment settings, URLs, credentials (e.g., ConfigReader.java).
Configuration Patterns

Use a config.properties file to store environment URLs, browser types, and credentials. Load these in ConfigReader.java. Example:

env.url=https://example.com
browser=chrome
username=testuser
password=secret

DriverFactory reads the browser property to start the correct WebDriver.

Tests use ConfigReader to get URLs and credentials, keeping sensitive data out of code.

Test Reporting and CI/CD Integration
  • Use TestNG for running tests and generating HTML reports.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests on code changes.
  • Reports show which tests passed or failed, with screenshots on failure.
  • Logs from action methods help debug test failures.
Best Practices for Action Methods per Page
  • One Action Method per User Interaction: Each method should do one clear action, like clickLoginButton() or enterUsername(String username).
  • Use Clear Method Names: Names should describe the action so tests read like a story.
  • Keep Locators Private: Locators stay inside the page class; tests never access them directly.
  • Return Page Objects When Navigating: If an action leads to a new page, return that page object from the method.
  • Use Explicit Waits Inside Actions: Wait for elements to be ready before interacting to avoid flaky tests.
Self Check

Where in this folder structure would you add a new action method clickLogoutButton() for the logout feature?

Key Result
Organize user interactions as clear action methods inside page classes to keep tests readable and maintainable.