A robust and maintainable Java test automation framework built with Playwright and TestNG
A robust and maintainable Java test automation framework built with Playwright and TestNG. This framework follows the Page Object Model (POM) design pattern and provides a comprehensive set of utilities for web application testing with modern browser automation capabilities.
- Features
- Project Structure
- Prerequisites
- Installation
- Quick Start
- Usage
- Examples
- Page Object Model
- Configuration
- Reports
- Logging
- Advanced Features
- Troubleshooting
- Framework Architecture
- Contributing
- License
- Page Object Model: Organized page classes for better maintainability
- Browser Support: Chromium configured out of the box (default); Firefox and WebKit can be enabled via configuration
- Multi-Environment Support: Built-in support for multiple environments (dev, qa, prod) with easy switching
- Headless Mode: Run tests without opening browser windows
- Parallel Execution: Run tests in parallel with configurable thread count for faster execution
- Rich Reporting: Interactive Allure reports with screenshots and detailed test execution information
- Auto-Waiting: Playwright's built-in auto-waiting eliminates flaky tests
- Logging: Comprehensive logging with SLF4J and Logback
- Configuration Management: Flexible YAML-based configuration for different environments
- Data-Driven Testing: Support for YAML, JSON, and CSV test data
- Screenshot Capture: Automatic screenshots on test failures with Allure integration
- Test Retry Mechanism: Configurable automatic retry for failed tests
- CI/CD Integration: GitHub Actions workflow for automated testing and reporting
playwright-java-accelerator/
│
├── .github/
│ └── workflows/
│ └── playwright.yml # GitHub Actions CI/CD workflow
│
├── src/
│ └── test/
│ ├── java/
│ │ └── io/
│ │ └── github/
│ │ └── mypixelquest/
│ │ └── pja/
│ │ ├── core/
│ │ │ └── PlaywrightTest.java
│ │ ├── config/
│ │ │ └── ConfigModel.java
│ │ ├── testdata/
│ │ │ ├── TestDataManager.java
│ │ │ └── TestDataGenerator.java
│ │ ├── listeners/
│ │ │ ├── ScreenshotListener.java
│ │ │ ├── RetryAnalyzer.java
│ │ │ ├── RetryListener.java
│ │ │ └── TestConfigurationListener.java
│ │ ├── pages/
│ │ │ ├── BasePage.java
│ │ │ └── PlaywrightDocsPage.java
│ │ ├── examples/
│ │ │ ├── PlaywrightDocsNavigationTest.java
│ │ │ ├── TestDataManagementTest.java
│ │ │ ├── ParallelExecutionTest.java
│ │ │ ├── DataDrivenTest.java
│ │ │ ├── ScreenshotAndReportingTest.java
│ │ │ ├── WaitStrategiesTest.java
│ │ │ ├── MultiBrowserTest.java
│ │ │ ├── RetryMechanismTest.java
│ │ │ └── FormInteractionTest.java
│ │ └── util/
│ │ └── ConfigReader.java
│ │
│ └── resources/
│ ├── config/
│ │ ├── dev.yaml
│ │ ├── qa.yaml
│ │ └── prod.yaml
│ ├── data/
│ │ ├── test-data.json
│ │ ├── test-data.csv
│ │ └── playwright-test-data.yaml
│ ├── logback.xml
│ └── suites/
│ ├── example-suite.xml
│ └── testng.xml
│
├── .gitignore
├── LICENSE
├── pom.xml
└── README.md
Before installing the framework, ensure you have:
- Java 17+ (check with
java -version) - Maven 3.6+ (check with
mvn -version) - Chromium browser (installed automatically by Playwright when you run
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install").- You can optionally configure Firefox or WebKit via the YAML config, but the default setup uses Chromium only.
After installation, you can verify your setup:
# Check Java version
java -version # Should be 17+
# Check Maven version
mvn -version # Should be 3.6+
# Verify Playwright browsers are installed
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --help"-
Clone the repository:
git clone https://github.com/yashwant-das/playwright-java-accelerator.git cd playwright-java-accelerator -
Install Playwright browser binaries:
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"This will download browser binaries for Chromium, Firefox, and WebKit.
-
Verify installation:
mvn clean compile
Get up and running in minutes:
# 1. Clone and setup
git clone https://github.com/yashwant-das/playwright-java-accelerator.git
cd playwright-java-accelerator
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# 2. Run your first test
mvn test -Dtest=PlaywrightDocsNavigationTest#testHomePageNavigation
# 3. View the Allure report
mvn allure:serveThat's it! The framework will automatically handle browser setup, create necessary directories, and generate reports.
Basic Test Execution:
# Run all tests
mvn test
# Run specific test file
mvn test -Dtest=PlaywrightDocsNavigationTest
# Run specific test class
mvn test -Dtest=PlaywrightDocsNavigationTest
# Run specific test method
mvn test -Dtest=PlaywrightDocsNavigationTest#testHomePageNavigationBrowser Selection:
# Run tests in Chromium (default)
mvn test -Dbrowser.type=chromium
# Run tests in Firefox
mvn test -Dbrowser.type=firefox
# Run tests in WebKit
mvn test -Dbrowser.type=webkitExecution Modes:
# Run tests in headless mode (faster, no UI)
mvn test -Dbrowser.headless=true
# Run tests in headed mode (see browser)
mvn test -Dbrowser.headless=false
# Run tests with specific environment
mvn test -Denvironment=qaTest Suite Execution:
# Run main test suite (all tests)
mvn test
# Run example suite (playwright.dev tests only)
mvn test -DsuiteXmlFile=src/test/resources/suites/example-suite.xmlTest Filtering:
# Run tests with specific groups
mvn test -Dgroups=smoke
# Run specific test class
mvn test -Dtest=ParallelExecutionTestOverride configuration at runtime:
# Override browser settings
mvn test -Dbrowser.type=firefox -Dbrowser.headless=true
# Override parallel execution
mvn test -DtestExecution.parallel=false
mvn test -DtestExecution.threadCount=4
# Override retry settings
mvn test -Dretry.enabled=true
mvn test -Dretry.maxRetries=3
mvn test -Dretry.delayBetweenRetries=2000
# Override environment
mvn test -Denvironment=qaThe PlaywrightDocsNavigationTest class demonstrates various interactions with the Playwright documentation website:
- Homepage Navigation Test: Verifies basic navigation and "Get Started" functionality
- Java Documentation Test: Tests navigation to Java-specific documentation
- Search Functionality Test: Validates the search feature and modal dialog
- Tools Navigation Test: Tests navigation between different Playwright tools (Codegen, Trace Viewer)
Run these tests:
mvn test -Dtest=PlaywrightDocsNavigationTestOr using the example suite:
mvn test -DsuiteXmlFile=src/test/resources/suites/example-suite.xmlThe framework includes comprehensive showcase tests demonstrating various capabilities:
- ParallelExecutionTest: Demonstrates parallel test execution with thread-safe browser handling
- DataDrivenTest: Shows data-driven testing with YAML, JSON, and CSV data sources
- ScreenshotAndReportingTest: Demonstrates screenshot capture and Allure reporting integration
- WaitStrategiesTest: Shows different wait strategies and Playwright's auto-waiting capabilities
- MultiBrowserTest: Demonstrates cross-browser testing (Chromium, Firefox, WebKit)
- RetryMechanismTest: Shows test retry mechanism for handling flaky tests
- FormInteractionTest: Demonstrates form filling, validation, and submission
- TestDataManagementTest: Demonstrates loading and using test data from multiple sources
Run all showcase tests:
mvn test -Dtest=*TestRun specific showcase test:
mvn test -Dtest=ParallelExecutionTest
mvn test -Dtest=DataDrivenTest
mvn test -Dtest=ScreenshotAndReportingTest
mvn test -Dtest=WaitStrategiesTest
mvn test -Dtest=MultiBrowserTest
mvn test -Dtest=RetryMechanismTest
mvn test -Dtest=FormInteractionTest
mvn test -Dtest=TestDataManagementTestThe framework follows the Page Object Model (POM) design pattern for better maintainability and code reusability.
All page objects extend BasePage, which provides common functionality:
public abstract class BasePage {
protected final Page page;
public BasePage(Page page) {
this.page = page;
}
// Common methods available to all page objects
public String getTitle() { /* ... */ }
public String getCurrentUrl() { /* ... */ }
public BasePage waitForPageLoad() { /* ... */ }
}public class PlaywrightDocsPage extends BasePage {
private final Locator getStartedButton;
private final Locator searchButton;
private final Locator javaLink;
public PlaywrightDocsPage(Page page) {
super(page);
this.getStartedButton = page.getByRole(AriaRole.LINK,
new Page.GetByRoleOptions().setName("Get started"));
this.searchButton = page.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Search (Command+K)"));
this.javaLink = page.locator("a[href='/java/']");
}
@Step("Navigate to homepage")
public PlaywrightDocsPage navigate() {
page.navigate("https://playwright.dev/java/");
return this;
}
@Step("Click Get Started button")
public PlaywrightDocsPage clickGetStarted() {
getStartedButton.click();
page.waitForURL("**/docs/intro**");
return this;
}
@Step("Open search dialog")
public PlaywrightDocsPage openSearch() {
searchButton.click();
return this;
}
}@Epic("Playwright Website Tests")
@Feature("Basic Website Navigation")
public class PlaywrightDocsNavigationTest extends PlaywrightTest {
@Test(description = "Verify homepage navigation")
@Severity(SeverityLevel.BLOCKER)
@Story("Homepage Navigation")
public void testHomePageNavigation() {
getCurrentPage().ifPresent(page -> {
PlaywrightDocsPage docsPage = new PlaywrightDocsPage(page);
docsPage.navigate()
.clickGetStarted();
assertThat(docsPage.getCurrentUrl())
.contains("/docs/intro");
});
}
}The framework uses YAML configuration files located in src/test/resources/config/ as the single source of truth for all configuration settings.
dev.yaml- Development environment configurationqa.yaml- QA environment configurationprod.yaml- Production environment configuration
environment:
name: qa
baseUrl: https://playwright.dev/
browser:
type: chromium # chromium, firefox, or webkit
headless: true
slowMo: 0 # milliseconds to wait between actions
timeout: 30000 # default timeout in milliseconds
screenshot:
takeOnFailure: true
fullPage: true
testExecution:
parallel: true # enable/disable parallel execution
threadCount: 3 # number of parallel threads to use
retry:
enabled: true # enable/disable test retries
maxRetries: 2 # maximum number of retry attempts
delayBetweenRetries: 1000 # delay in milliseconds between retriesThe framework uses a configuration management system that:
- Centralizes all settings in YAML configuration files
- Supports environment-specific configurations
- Allows runtime overrides via system properties
- Uses TestNG listeners for dynamic configuration
- Provides automatic test retry capabilities
Configure retry behavior through YAML:
retry:
enabled: true # Enable/disable retry mechanism
maxRetries: 2 # Maximum retry attempts (0-N)
delayBetweenRetries: 1000 # Milliseconds to wait between retriesOr override at runtime:
mvn test -Dretry.enabled=true -Dretry.maxRetries=3Configure parallel execution through YAML:
testExecution:
parallel: true
threadCount: 3Or override at runtime:
mvn test -DtestExecution.parallel=false
mvn test -DtestExecution.threadCount=4The framework generates comprehensive test reports using Allure:
Interactive Allure reports provide enhanced visualization with automatically populated sections:
Generate and View Allure Reports:
# Generate Allure report
mvn allure:report
# Serve interactive report
mvn allure:serveAllure Features:
- Interactive test result dashboard
- Environment Section: Automatically populated with browser, OS, Java version, framework details
- Executors Section: Shows executor information (automatically generated)
- Trend Section: Test execution trends and history across multiple runs (automatically tracked)
- Screenshots and error logs attached to failed tests
- Test categorization and filtering
- Timeline view of test execution
- Step-by-step test execution details
CI/CD Test Results:
The test results are automatically published to GitHub Pages after each CI/CD run. You can view them at: https://yashwant-das.github.io/playwright-java-accelerator/
The report includes:
- Test execution history
- Test case details
- Screenshots of failed tests
- Test duration and status
- Environment information
- Test categories and tags
Screenshots are automatically captured and attached to reports:
- On Failure: All failed tests automatically capture screenshots
- Integration: Screenshots are embedded in Allure reports
- Storage:
target/screenshots/with timestamps and test names - Error Logs: Detailed error information saved alongside screenshots
The framework provides comprehensive logging with automatic timestamping:
- Test Execution Logs:
logs/playwright-tests.log - Archived Logs:
logs/archived/playwright-tests.YYYY-MM-DD.i.log - Console Logging: Real-time test execution information
- Log Levels: DEBUG, INFO, WARNING, ERROR with proper formatting
- Log files are automatically rotated when:
- Size reaches 10MB
- A new day starts
- Retention policy:
- Keeps logs for 30 days
- Total size cap of 100MB for all archived logs
Default log levels are configured as follows:
- Framework code (io.github.mypixelquest.pja): DEBUG
- TestNG: INFO
- Playwright: INFO
- Root logger: INFO
You can modify logging behavior by editing src/test/resources/logback.xml:
- Change log levels
- Modify rotation policies
- Add new appenders
- Customize log patterns
The framework supports parallel test execution through TestNG:
# Configure parallel execution in YAML
testExecution:
parallel: true
threadCount: 3
# Or override at runtime
mvn test -DtestExecution.parallel=true -DtestExecution.threadCount=4Benefits:
- Faster execution with multiple threads
- Independent browser sessions for each thread
- Automatic test distribution across threads
- Thread-safe browser handling
The framework provides comprehensive test data management capabilities:
- YAML:
playwright-test-data.yaml - JSON:
test-data.json - CSV:
test-data.csv
users:
admin:
username: admin@playwright-test.com
password: Test@1234
role: ADMIN
permissions:
- read
- write
- delete
browsers:
chromium:
name: "Chromium"
type: "chromium"
headless: true
viewport:
width: 1920
height: 1080@Test
public void testWithYamlData() {
TestDataManager dataManager = new TestDataManager("qa");
Map<String, Object> data = dataManager.loadYamlData("playwright-test-data.yaml");
String username = (String) dataManager.getValue(data, "users.admin.username");
String password = (String) dataManager.getValue(data, "users.admin.password");
// Use data in test
}@Test
public void testWithDynamicData() {
String email = TestDataGenerator.generateEmail();
String password = TestDataGenerator.generatePassword(12, true);
String phone = TestDataGenerator.generatePhoneNumber();
// Use generated data in test
}The framework includes two TestNG suite files:
- Runs all example tests in the framework
- Demonstrates all framework capabilities
- Uses package scanning to include all test classes
- Configured with all framework listeners
- Focused suite for playwright.dev documentation testing
- Includes only
PlaywrightDocsNavigationTestclass - Demonstrates how to create targeted test suites
- Useful for running specific test scenarios
Usage:
# Run main suite (all tests)
mvn test
# Run example suite (playwright.dev tests only)
mvn test -DsuiteXmlFile=src/test/resources/suites/example-suite.xmlBrowser Not Installed:
- Ensure the browser is installed before running tests
- Install Playwright browsers:
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
Java Version Issues:
- Verify Java version:
java -version(should be 17+) - Update JAVA_HOME if needed
Maven Dependency Issues:
- Clean and rebuild:
mvn clean install - Verify Maven version:
mvn -version(should be 3.6+)
Configuration Errors:
- Verify
src/test/resources/config/*.yamlfiles exist and are valid YAML - Check
src/test/resources/data/*files are valid
Import Errors:
- Verify all dependencies are installed:
mvn dependency:resolve - Check Java version compatibility
If tests fail, check:
- Log files in
logs/directory for detailed error messages - Screenshots in
target/screenshots/for visual debugging - Allure reports for test execution details
- GitHub Actions logs for CI/CD issues
The framework provides clear error messages to help diagnose issues quickly.
PlaywrightTest (core/PlaywrightTest.java):
- Base test class with browser setup/teardown
- Provides
getCurrentPage()method for test access - Handles browser lifecycle management
ConfigReader (util/ConfigReader.java):
- Singleton pattern for configuration loading
- Loads YAML configuration files
- Provides typed access to configuration settings
TestDataManager (testdata/TestDataManager.java):
- Centralized test data loading from YAML, JSON, CSV
- Path-based value access using dot notation
- Environment-specific data support
ScreenshotListener (listeners/ScreenshotListener.java):
- Automatic screenshot capture on test failure
- Allure report integration
- Full-page screenshot support
RetryAnalyzer (listeners/RetryAnalyzer.java):
- Configurable test retry mechanism
- Tracks retry attempts per test
- Configurable delay between retries
BasePage (pages/BasePage.java):
- Common page object methods
- Playwright page wrapper
- Screenshot integration for debugging
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT License - feel free to use this as a template for your own test automation framework.
Built with ❤️ using Java, Playwright, TestNG, and Allure
