Skip to content

Java Selenium framework with POM, TestNG & ExtentReports. Java 21 + Maven + WebDriverManager for cross-browser testing.

Notifications You must be signed in to change notification settings

Purush1701/java-selenium-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Selenium Framework with Page Object Model

Selenium Java Maven TestNG

A streamlined Java-based Selenium automation framework built with Maven, TestNG, and ExtentReports. Features industry-standard test cases using SauceDemo - a fully-featured e-commerce application designed for testing.

✨ Features

  • Page Object Model (POM) - Maintainable and reusable page objects
  • Maven - Dependency management and build automation
  • TestNG - Test execution and reporting framework
  • ExtentReports - Beautiful HTML reports with screenshots (auto-opens after tests)
  • WebDriverManager - Automatic driver management
  • Firefox Default - Stable headless execution out of the box
  • Simplified Architecture - Clean, focused test suite (31 core tests)
  • Auto-Report Opening - Reports automatically open in browser after execution
  • Smart Cleanup - Automatic old report cleanup on each run
  • Custom Maven Wrapper - Easy-to-use commands for all operations
  • Configuration Management - Centralized configuration
  • Java 21 - Latest LTS Java version

πŸ“ Project Structure

Java Selenium Framework/
β”œβ”€β”€ pom.xml                           # Maven configuration with auto-cleanup
β”œβ”€β”€ README.md                         # This documentation
β”œβ”€β”€ mvn-wrapper.sh                    # Custom Maven wrapper (auto-opens reports)
β”œβ”€β”€ .gitignore                        # Git ignore rules
β”œβ”€β”€ src/main/java/com/framework/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ WebDriverConfig.java     # WebDriver setup & management (Firefox default)
β”‚   β”‚   β”œβ”€β”€ ConfigReader.java        # Configuration file reader
β”‚   β”‚   └── EnvironmentConfig.java   # Environment variable support
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ BasePage.java            # Base page class with common methods
β”‚   β”‚   β”œβ”€β”€ LoginPage.java           # SauceDemo login page
β”‚   β”‚   β”œβ”€β”€ ProductsPage.java        # Products listing page
β”‚   β”‚   └── CartPage.java            # Shopping cart page
β”‚   β”œβ”€β”€ reports/
β”‚   β”‚   β”œβ”€β”€ ExtentReportManager.java # HTML report management
β”‚   β”‚   └── ScreenshotUtils.java     # Screenshot utilities
β”‚   └── utils/
β”‚       β”œβ”€β”€ WebDriverUtils.java      # Common WebDriver operations
β”‚       └── ExcelUtils.java          # Excel file operations
└── src/test/
    β”œβ”€β”€ java/com/framework/tests/
    β”‚   β”œβ”€β”€ LoginTest.java           # Login tests (6 tests)
    β”‚   β”œβ”€β”€ ProductTest.java         # Product tests (8 tests)
    β”‚   └── CartTest.java            # Cart tests (17 tests)
    └── resources/
        β”œβ”€β”€ config/
        β”‚   └── config.properties    # Configuration settings
        β”œβ”€β”€ testdata/
        β”‚   └── testdata.json        # Test data
        └── testng.xml               # TestNG suite (sequential execution)

πŸ“‹ Prerequisites

  • Java 21 (LTS - Latest Long Term Support)
  • Maven 3.6+
  • Firefox browser (default) or Chrome/Edge
  • macOS/Linux/Windows

Setup Instructions

  1. Clone or download the project

  2. Navigate to project directory

    cd "Java Selenium Framework"
  3. Set up environment variables (Important!)

    # Copy the example environment file
    cp .env.example .env
    
    # Edit .env with your credentials (optional for demo)
    # The default values work for SauceDemo
  4. Install dependencies

    mvn clean install

πŸš€ Running Tests

⚑ Quick Start (Recommended)

Use the custom Maven wrapper script - it auto-cleans old reports and opens results:

# Run all tests with Firefox (default) + auto-open report
./mvn-wrapper.sh test

# Run with Chrome + auto-open report
./mvn-wrapper.sh test-chrome

# Run in headless mode + auto-open report
./mvn-wrapper.sh test-headless

# Just open the latest report (without running tests)
./mvn-wrapper.sh report

# Show all available commands
./mvn-wrapper.sh help

πŸ› οΈ Maven Wrapper Commands

Command Description
./mvn-wrapper.sh test Run all tests (Firefox) + open report
./mvn-wrapper.sh test-chrome Run with Chrome + open report
./mvn-wrapper.sh test-firefox Run with Firefox + open report
./mvn-wrapper.sh test-headless Run in headless mode + open report
./mvn-wrapper.sh test-only Run tests WITHOUT opening report
./mvn-wrapper.sh report Open the latest test report
./mvn-wrapper.sh clean Clean project and old reports
./mvn-wrapper.sh compile Compile project only
./mvn-wrapper.sh install Install all dependencies

πŸ”§ Direct Maven Commands

Run all tests

mvn clean test

Run specific test class

# Run login tests (6 tests)
mvn test -Dtest=LoginTest

# Run product tests (8 tests)
mvn test -Dtest=ProductTest

# Run cart tests (17 tests)
mvn test -Dtest=CartTest

Run specific test method

# Run specific login test
mvn test -Dtest=LoginTest#testSuccessfulLogin

# Run specific product test
mvn test -Dtest=ProductTest#testSortProductsLowToHigh

# Run specific cart test
mvn test -Dtest=CartTest#testAddItemToCart

🌐 Browser-Specific Execution

# Firefox (default, headless)
mvn test

# Chrome
mvn test -Dbrowser=chrome

# Edge
mvn test -Dbrowser=edge

# Safari
mvn test -Dbrowser=safari

πŸ“Š View Test Reports

Reports automatically open when using mvn-wrapper.sh commands.

Manual opening:

# Open latest report (after cleaning, only one exists)
open test-output/reports/TestReport_*.html

# Or use the wrapper
./mvn-wrapper.sh report

πŸ”§ Development Commands

Clean and compile

mvn clean compile

Install dependencies

mvn clean install

Run with debug information

mvn test -X

πŸ“‹ Environment Setup

Set Java path (if needed)

export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH"
mvn test

πŸ“ View Results

Check test reports

# View ExtentReports
open test-output/reports/TestReport_*.html

# View TestNG reports
open target/surefire-reports/index.html

# List all reports
ls -la test-output/reports/

🎯 Quick Start Examples

# Quick test run
mvn test -Dtest=SimpleTestRunner

# Full test suite with Chrome
mvn test -Dbrowser=chrome

# Headless testing
mvn test -Dbrowser=chrome -Dheadless=true

# Parallel execution
mvn test -Dparallel=methods -DthreadCount=2

Test Application

This framework tests SauceDemo (https://www.saucedemo.com), a fully-featured e-commerce application designed for test automation practice.

Available Test Users

  • standard_user - Standard user with full access
  • locked_out_user - User that has been locked out
  • problem_user - User experiencing problems (images, sorting)
  • performance_glitch_user - User with performance issues
  • Password for all users: secret_sauce

βœ… Test Coverage

Total: 31 Comprehensive Tests across 3 core areas

πŸ” Login Tests (LoginTest.java) - 6 Tests

  • βœ… Successful login with valid credentials
  • βœ… Login failure with invalid username
  • βœ… Login failure with invalid password
  • βœ… Login failure with empty credentials
  • βœ… Login failure with empty password
  • βœ… Locked out user validation

πŸ›οΈ Product Tests (ProductTest.java) - 8 Tests

  • βœ… Verify all products are displayed
  • βœ… Sort products A to Z
  • βœ… Sort products Z to A
  • βœ… Sort products by price (low to high)
  • βœ… Sort products by price (high to low)
  • βœ… Verify specific product details
  • βœ… Add single product to cart
  • βœ… Add multiple products to cart

πŸ›’ Cart Tests (CartTest.java) - 17 Tests

  • βœ… Access cart page
  • βœ… Verify empty cart
  • βœ… Verify items appear in cart after adding
  • βœ… Verify multiple items in cart
  • βœ… Remove items from cart
  • βœ… Continue shopping functionality
  • βœ… Verify item quantities
  • βœ… Verify item prices in cart
  • βœ… Add/remove multiple items workflow
  • βœ… Cart badge count validation
  • βœ… Cart state persistence
  • βœ… Item details in cart
  • βœ… Remove last item from cart
  • βœ… Multiple additions/removals
  • βœ… Cart navigation
  • βœ… Product to cart integration
  • βœ… Complete cart workflow

Configuration

πŸ” Environment Variables (Recommended for Sensitive Data)

The framework supports .env files for managing sensitive data like passwords, API keys, and credentials:

# Create your .env file
cp .env.example .env

# Edit with your values
nano .env

Available in .env:

  • User credentials (passwords, API keys)
  • Database credentials
  • Remote execution configuration (BrowserStack, Sauce Labs)
  • Email/Slack notifications
  • Cloud service credentials (AWS, etc.)

See Environment Setup Guide for complete documentation.

βš™οΈ Configuration Properties

Edit src/test/resources/config/config.properties for non-sensitive settings:

Priority: .env values override config.properties values.

Test Reports

After running tests, reports will be generated in:

  • ExtentReports: test-output/reports/TestReport_[timestamp].html
  • Screenshots: test-output/screenshots/
  • TestNG Reports: test-output/

πŸ”¨ Adding New Tests

  1. Create a new page class extending BasePage (PageFactory handled by BasePage)
  2. Add page elements using @FindBy annotations
  3. Create test methods in test classes
  4. Update testng.xml to include new test classes

Example Page Class

public class NewPage extends BasePage {
    @FindBy(id = "elementId")
    private WebElement element;
    
    public NewPage(WebDriver driver) {
        super(driver); // BasePage handles PageFactory initialization
    }
    
    public void performAction() {
        clickElement(element);
    }
}

Example Test Method

@Test(description = "Test description")
public void testMethod() {
    try {
        WebDriver driver = WebDriverConfig.getDriver();
        // Test steps
        ExtentReportManager.logInfo("Test step executed");
        Assert.assertTrue(condition, "Assertion message");
        ExtentReportManager.logPass("Test passed");
    } catch (Exception e) {
        ExtentReportManager.logFail("Test failed: " + e.getMessage());
        throw e;
    }
}

@AfterMethod
public void tearDown() {
    WebDriverConfig.quitDriver(); // Always use quitDriver()
}

Data-Driven Testing

Excel Data

Use ExcelUtils class to read/write Excel files:

List<Map<String, String>> testData = ExcelUtils.readExcelData("testdata.xlsx", "Sheet1");

JSON Data

Place JSON test data files in src/test/resources/testdata/ directory.

Best Practices

  1. Use Page Object Model - Keep page logic separate from test logic
  2. Use explicit waits - Avoid Thread.sleep()
  3. Take screenshots on test failures
  4. Use meaningful test names and descriptions
  5. Keep tests independent - Each test should be able to run standalone
  6. Use configuration files - Avoid hardcoding values
  7. Clean up resources - Always quit WebDriver in @AfterMethod

Troubleshooting

Common Issues

  1. WebDriver not found

    • Ensure browser is installed
    • Check WebDriverManager setup
  2. Tests failing

    • Check element locators
    • Verify application is accessible
    • Check timeout settings
  3. Reports not generating

    • Ensure test-output directory exists
    • Check ExtentReportManager initialization

Contributing

  1. Follow existing code structure
  2. Add proper documentation
  3. Include test cases for new features
  4. Update README if needed

πŸ“š Complete Command Reference

πŸ› οΈ Maven Wrapper Commands (Recommended)

Command Description
./mvn-wrapper.sh test Run all tests (Firefox) + auto-open report
./mvn-wrapper.sh test-chrome Run with Chrome + auto-open report
./mvn-wrapper.sh test-firefox Run with Firefox + auto-open report
./mvn-wrapper.sh test-headless Run in headless mode + auto-open report
./mvn-wrapper.sh test-only Run tests WITHOUT opening report
./mvn-wrapper.sh report Open the latest test report only
./mvn-wrapper.sh clean Clean project and old reports
./mvn-wrapper.sh compile Compile project only
./mvn-wrapper.sh install Install all dependencies
./mvn-wrapper.sh help Show all available commands

πŸ”§ Direct Maven Commands

Command Description
mvn clean test Clean and run all tests
mvn test Run all tests
mvn test -Dtest=LoginTest Run specific test class
mvn test -Dtest=LoginTest#testSuccessfulLogin Run specific test method
mvn test -Dbrowser=chrome Run with Chrome browser
mvn test -Dbrowser=firefox Run with Firefox browser (default)
mvn test -Dbrowser=edge Run with Edge browser
mvn clean install Install dependencies
mvn clean compile Clean and compile only
mvn test -X Run with debug information

🌍 Environment Variables

Variable Description Example
JAVA_HOME Java installation path /opt/homebrew/opt/openjdk@21
PATH Include Java bin directory export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"
MAVEN_OPTS Maven JVM options -Xmx1024m

Report Locations

Report Type Location Description
ExtentReports test-output/reports/ Beautiful HTML reports with screenshots
TestNG Reports target/surefire-reports/ Standard TestNG test results
Screenshots test-output/screenshots/ Test failure screenshots
Logs logs/ Application and test logs

Configuration Properties

Property Description Default
app.url Application base URL https://www.google.com
browser.name Default browser chrome
app.timeout Global timeout 30
app.implicit.wait Implicit wait time 10
browser.headless Headless mode false
browser.maximize Maximize window true

πŸš€ Quick Start Guide

  1. Navigate to project directory

    cd "Java Selenium Framework"
  2. Install dependencies (first time only)

    ./mvn-wrapper.sh install
  3. Run all tests (auto-opens report)

    ./mvn-wrapper.sh test

That's it! The framework will:

  • βœ… Clean old reports
  • βœ… Run all 31 tests with Firefox (headless)
  • βœ… Generate HTML report with screenshots
  • βœ… Automatically open the report in your browser

🎯 Common Use Cases

Development Workflow

# Run specific test class during development
mvn test -Dtest=LoginTest

# Run specific test method
mvn test -Dtest=LoginTest#testSuccessfulLogin

# Run without auto-opening report
./mvn-wrapper.sh test-only

# Open latest report manually
./mvn-wrapper.sh report

CI/CD Pipeline

# Run tests in headless mode (default)
./mvn-wrapper.sh test-headless

# Or use Maven directly
mvn clean test

Cross-Browser Testing

# Firefox (default, headless)
./mvn-wrapper.sh test

# Chrome
./mvn-wrapper.sh test-chrome

# Different browser with Maven
mvn test -Dbrowser=edge
mvn test -Dbrowser=safari

License

This project is for educational and testing purposes.

About

Java Selenium framework with POM, TestNG & ExtentReports. Java 21 + Maven + WebDriverManager for cross-browser testing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published