0
0
Selenium Javatesting~8 mins

Utility classes in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Utility classes
Folder Structure
src/
├── main/
│   └── java/
│       └── com/example/project/
│           ├── pages/           # Page Object classes
│           ├── utils/           # Utility classes (e.g., WebDriverUtils, WaitUtils)
│           └── config/          # Configuration classes
└── test/
    └── java/
        └── com/example/project/
            ├── tests/           # Test classes
            └── data/            # Test data classes or files
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown.
  • Page Objects: Represent web pages with methods to interact with UI elements.
  • Utility Classes: Provide reusable helper methods like waits, screenshots, logging, and common WebDriver actions.
  • Tests: Contain test cases using page objects and utilities.
  • Configuration: Manage environment settings, browser types, and credentials.
Configuration Patterns
  • Properties Files: Store environment URLs, browser names, timeouts.
  • Config Classes: Load properties and provide access methods.
  • Environment Profiles: Use different property files for dev, test, prod.
  • Browser Setup: Utility class reads config and initializes correct WebDriver.
  • Credentials: Store securely outside code (e.g., encrypted files or environment variables).
Test Reporting and CI/CD Integration
  • TestNG Reports: Use built-in TestNG HTML reports for test results.
  • Logging: Utility classes handle logging for debugging and traceability.
  • Screenshot Utility: Capture screenshots on test failure for reports.
  • CI/CD: Integrate with Jenkins or GitHub Actions to run tests automatically on code changes.
  • Report Aggregation: Use plugins like Allure or ExtentReports for enhanced reports.
Best Practices for Utility Classes
  1. Single Responsibility: Each utility class should have one clear purpose (e.g., WaitUtils only handles waits).
  2. Reusability: Write generic methods that can be used across different tests and pages.
  3. Static Methods: Use static methods for stateless utilities to avoid unnecessary object creation.
  4. Exception Handling: Handle exceptions inside utilities to keep test code clean.
  5. Documentation: Comment utility methods clearly to explain their use.
Self Check

Where in this folder structure would you add a new utility class for handling explicit waits?

Key Result
Utility classes provide reusable helper methods to keep test code clean and maintainable.