0
0
Selenium Javatesting~8 mins

Screenshot attachment on failure in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Screenshot attachment on failure
Folder Structure
src/
├── main/
│   └── java/
│       └── com/example/project/
│           └── pages/           # Page Object classes
├── test/
│   └── java/
│       ├── com/example/project/tests/   # Test classes
│       ├── com/example/project/utils/   # Utility classes (e.g., ScreenshotHelper)
│       └── com/example/project/listeners/ # TestNG listeners for failure handling
└── resources/
    └── config.properties       # Configuration file
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory class).
  • Page Objects: Classes representing web pages with locators and actions.
  • Tests: TestNG test classes containing test methods.
  • Utilities: Helper classes like ScreenshotHelper to capture screenshots on failure.
  • Listeners: TestNG listeners (e.g., ITestListener) to detect test failures and trigger screenshot capture.
  • Configuration: Properties files and classes to manage environment settings and browser choices.
Configuration Patterns
  • Use a config.properties file to store environment URLs, browser types, and credentials.
  • Load configuration in a utility class (e.g., ConfigManager) to provide values to tests and driver setup.
  • Allow browser choice via system properties or config file for flexibility.
  • Configure TestNG XML to include listeners for automatic screenshot capture on failure.
Test Reporting and CI/CD Integration
  • Integrate TestNG reports with screenshot attachments for failed tests.
  • Use TestNG ITestListener to capture screenshots and save them with timestamped filenames.
  • Include screenshot paths in TestNG reports or custom HTML reports.
  • Configure CI/CD pipelines (e.g., Jenkins) to archive test reports and screenshots as build artifacts.
  • Optionally, send failure reports with screenshots via email or Slack notifications.
Best Practices
  • Use TestNG Listeners: Implement ITestListener to centralize failure detection and screenshot capture.
  • Keep Screenshot Logic Separate: Put screenshot code in utility/helper classes to keep tests clean.
  • Use Descriptive Filenames: Include test name and timestamp in screenshot filenames for easy identification.
  • Store Screenshots in Organized Folders: Separate screenshots by test run date or test class for clarity.
  • Integrate with Reports: Attach screenshots to reports so failures can be quickly diagnosed.
Self Check

Where in this framework structure would you add the code to capture a screenshot when a test fails?

Key Result
Use TestNG listeners to capture and attach screenshots automatically on test failure in Selenium Java frameworks.