0
0
Selenium Javatesting~8 mins

Custom annotations in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Custom annotations
Folder Structure
src/
├── main/
│   └── java/
│       └── com/example/project/
│           ├── annotations/       # Custom annotation definitions
│           ├── pages/             # Page Object classes
│           ├── utils/             # Utility classes (e.g., waits, helpers)
│           └── config/            # Configuration classes
└── test/
    └── java/
        └── com/example/project/
            ├── tests/             # Test classes
            └── listeners/         # TestNG listeners, e.g., for reporting
Test Framework Layers
  • Annotations Layer: Contains custom annotation interfaces (e.g., @Retry, @TestInfo) to add metadata or behavior to tests.
  • Page Object Layer: Classes representing web pages with locators and methods to interact with UI elements.
  • Test Layer: Test classes using TestNG with custom annotations to control test execution or add info.
  • Utilities Layer: Helper classes for common tasks like waits, logging, or annotation processing.
  • Configuration Layer: Classes managing environment settings, browser setup, and credentials.
Configuration Patterns
  • Environment Properties: Use config.properties files for different environments (dev, qa, prod) loaded via a config class.
  • Browser Setup: Configure browser type and options in a factory class, selectable via system properties or config files.
  • Credentials Management: Store sensitive data securely (e.g., encrypted files or environment variables) accessed by config classes.
  • Annotation Parameters: Custom annotations can accept parameters (e.g., retry count) to configure test behavior dynamically.
Test Reporting and CI/CD Integration
  • Use TestNG listeners to capture test results and generate detailed HTML reports.
  • Custom annotations can trigger listeners or retry logic to improve test reliability and reporting clarity.
  • Integrate reports with CI/CD pipelines (e.g., Jenkins, GitHub Actions) to show pass/fail status and logs after each build.
  • Use logs and screenshots on test failure, triggered by annotation-driven retry or hooks.
Best Practices for Custom Annotations in Selenium Java Framework
  • Keep Annotations Simple: Define clear, single-purpose annotations to avoid complexity.
  • Use Retention and Target Properly: Set retention policy to runtime and target to methods or classes as needed.
  • Process Annotations via Reflection: Use Java reflection to read annotation values and apply logic (e.g., retries, test metadata).
  • Integrate with TestNG: Combine custom annotations with TestNG listeners or transformers for seamless behavior changes.
  • Document Annotations: Provide usage examples and expected effects to help team members understand and use them correctly.
Self Check

Where in this framework structure would you add a new custom annotation called @FlakyTest to mark tests that should be retried automatically?

Key Result
Use custom annotations in a dedicated package to add metadata and control test behavior, processed via reflection and integrated with TestNG listeners.