Skip to content

A robust, enterprise-level test automation framework using Selenium WebDriver, Cucumber BDD, TestNG, and Gradle

Notifications You must be signed in to change notification settings

nagendra-qa/selenium-cucumber-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Selenium + Java + Cucumber + TestNG Framework

A robust, enterprise-level test automation framework using Selenium WebDriver, Cucumber BDD, TestNG, and Gradle.

πŸ—οΈ Framework Architecture

selenium-cucumber-framework/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/com/automation/
β”‚   β”‚   β”‚   β”œβ”€β”€ pages/           # Page Object Model classes
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ BasePage.java (100+ reusable methods)
β”‚   β”‚   β”‚   β”‚   └── LoginPage.java
β”‚   β”‚   β”‚   └── utils/           # Utility classes
β”‚   β”‚   β”‚       β”œβ”€β”€ ConfigReader.java
β”‚   β”‚   β”‚       β”œβ”€β”€ DriverManager.java
β”‚   β”‚   β”‚       β”œβ”€β”€ ScreenshotUtil.java
β”‚   β”‚   β”‚       └── RetryAnalyzer.java
β”‚   β”‚   └── resources/
β”‚   β”‚       └── config.properties
β”‚   └── test/
β”‚       β”œβ”€β”€ java/com/automation/
β”‚       β”‚   β”œβ”€β”€ runners/         # TestNG Cucumber Runners
β”‚       β”‚   β”‚   β”œβ”€β”€ TestRunner.java
β”‚       β”‚   β”‚   └── SmokeTestRunner.java
β”‚       β”‚   β”œβ”€β”€ stepdefinitions/ # Cucumber Step Definitions
β”‚       β”‚   β”‚   └── LoginSteps.java
β”‚       β”‚   └── hooks/           # Cucumber Hooks
β”‚       β”‚       └── Hooks.java
β”‚       └── resources/
β”‚           β”œβ”€β”€ features/        # Cucumber Feature files
β”‚           β”‚   └── Login.feature
β”‚           β”œβ”€β”€ testdata/        # Test data files
β”‚           └── testng.xml       # TestNG suite configuration
β”œβ”€β”€ build.gradle
β”œβ”€β”€ settings.gradle
β”œβ”€β”€ gradle.properties
└── README.md

✨ Key Features

🎯 Core Features

  • βœ… Page Object Model (POM) design pattern
  • βœ… Cucumber BDD for behavior-driven testing
  • βœ… TestNG for test execution and management
  • βœ… Gradle build automation
  • βœ… Enterprise-level BasePage with 100+ reusable methods
  • βœ… Retry Analyzer for automatic failed test retry
  • βœ… Screenshot on Failure capability
  • βœ… Allure Reports - Beautiful, interactive HTML reports
  • βœ… Multiple Reporting (Cucumber HTML, JSON, XML, Allure)
  • βœ… Parallel Execution support
  • βœ… Cross-browser testing support
  • βœ… WebDriverManager for automatic driver management

πŸ› οΈ BasePage Capabilities (100+ Methods)

The framework includes a comprehensive BasePage with advanced methods organized in categories:

Click Methods (7 types)

  • Standard click, JavaScript click, Actions click
  • Double-click, Right-click, Click and hold

Input/SendKeys Methods (6 types)

  • SendKeys with/without clear, JavaScript input
  • Slow typing, Field clearing

Dropdown Handling (7 types)

  • Select by text/value/index
  • Get all options, Multi-select support
  • Deselect operations

Advanced Wait Strategies (10 types)

  • Explicit waits, Fluent waits
  • Custom waits, AJAX/jQuery waits
  • Page load waits, Element visibility waits

Mouse Actions (5 types)

  • Hover, Hover and click
  • Drag & Drop, Drag by offset

Scroll Operations (6 types)

  • Scroll to element (smooth/regular)
  • Scroll to top/bottom
  • Scroll by pixels

Alert Handling (5 methods)

  • Accept, Dismiss, Get text
  • Send text, Check presence

Frame/Window Management (9 methods)

  • Switch frames (by index/name/element)
  • Switch windows, Close windows
  • Navigate between windows

JavaScript Executor (5 methods)

  • Execute scripts, Highlight elements
  • Set/Remove attributes

Navigation Methods (6 methods)

  • Navigate to URL, Refresh, Back, Forward
  • Get current URL and title

Screenshot Methods (2 types)

  • Full page screenshots
  • Element-specific screenshots

List/Table Operations (3 methods)

  • Get all elements text
  • Click element by text
  • Find element by text

Keyboard Actions (7 methods)

  • Press Enter/Tab/specific keys
  • Keyboard shortcuts
  • Copy/Paste operations

πŸ“‹ Prerequisites

  • Java JDK: 17 or higher
  • Gradle: 7.x or higher (or use Gradle wrapper)
  • IDE: IntelliJ IDEA / Eclipse / VS Code
  • Browsers: Chrome (default), Firefox, Edge, or Safari

πŸš€ Setup Instructions

1. Navigate to Framework Directory

cd /Users/doddakulanagendrababu/Documents/selenium-cucumber-framework

2. Install Dependencies

./gradlew clean build

Or on Windows:

gradlew.bat clean build

3. Configure Test Settings

Edit src/main/resources/config.properties:

# Application URL
app.url=https://doddakula-nagendrababu-ai-testcase.vercel.app/login.html

# Browser Configuration
browser=chrome
headless=false
browser.window.maximize=true

# Timeouts
implicit.wait=10
explicit.wait=20
page.load.timeout=30

# Test Credentials
test.username=testuser@example.com
test.password=Test@123

# Retry Configuration
retry.count=2

# Screenshot Configuration
screenshot.on.failure=true
screenshot.path=test-output/screenshots/

▢️ Running Tests

Run All Regression Tests

./gradlew clean test

Run via TestNG XML

./gradlew clean test -Dsuite=src/test/resources/testng.xml

Run Smoke Tests Only

./gradlew clean test -Dcucumber.filter.tags="@Smoke"

Run with Specific Tags

# Run Login tests
./gradlew clean test -Dcucumber.filter.tags="@Login"

# Run Positive tests
./gradlew clean test -Dcucumber.filter.tags="@Positive"

# Run Negative tests
./gradlew clean test -Dcucumber.filter.tags="@Negative"

# Run multiple tags (AND condition)
./gradlew clean test -Dcucumber.filter.tags="@Smoke and @Login"

# Run multiple tags (OR condition)
./gradlew clean test -Dcucumber.filter.tags="@Smoke or @Regression"

Run in Headless Mode

Update config.properties:

headless=true

Run with Different Browser

Update config.properties:

browser=firefox  # Options: chrome, firefox, edge, safari

Run Specific Runner

# Run TestRunner
./gradlew clean test --tests "com.automation.runners.TestRunner"

# Run SmokeTestRunner
./gradlew clean test --tests "com.automation.runners.SmokeTestRunner"

πŸ“Š Test Reports

After test execution, reports are generated in multiple formats:

Allure Report (Recommended) πŸ“Š

# Generate and view Allure report
allure serve build/allure-results

Location: build/allure-report/ Features: Interactive, beautiful HTML reports with screenshots, trends, and detailed analytics

πŸ“– See ALLURE_SETUP.md for detailed Allure installation and usage guide

Cucumber HTML Report

target/cucumber-reports/cucumber.html

Cucumber JSON Report

target/cucumber-reports/cucumber.json

Cucumber XML Report (for CI/CD)

target/cucumber-reports/cucumber.xml

TestNG Report

test-output/index.html

Screenshots (on failure)

test-output/screenshots/

View Reports

# View Cucumber report
open target/cucumber-reports/cucumber.html

# View Allure report
allure serve build/allure-results

πŸ§ͺ Writing New Tests

Step 1: Create Feature File

Create a new .feature file in src/test/resources/features/:

@NewFeature @Regression
Feature: New Feature Description
  As a user
  I want to test new functionality
  So that I can ensure it works correctly

  Background:
    Given User is on the application

  @Smoke @Positive
  Scenario: Valid scenario description
    When User performs an action
    And User performs another action
    Then User should see expected result

  @Negative
  Scenario: Invalid scenario description
    When User performs invalid action
    Then User should see error message

Step 2: Create Page Object

Create a new page class in src/main/java/com/automation/pages/:

package com.automation.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class NewPage extends BasePage {

    // Locators
    @FindBy(id = "elementId")
    private WebElement element;

    @FindBy(xpath = "//button[@type='submit']")
    private WebElement submitButton;

    // Constructor
    public NewPage(WebDriver driver) {
        super(driver);
    }

    // Action Methods
    public void clickElement() {
        click(element);
    }

    public void enterText(String text) {
        sendKeys(element, text);
    }

    public String getElementText() {
        return getText(element);
    }

    public boolean isElementDisplayed() {
        return isDisplayed(element);
    }
}

Step 3: Create Step Definitions

Create step definitions in src/test/java/com/automation/stepdefinitions/:

package com.automation.stepdefinitions;

import com.automation.pages.NewPage;
import com.automation.utils.DriverManager;
import io.cucumber.java.en.*;
import org.testng.Assert;

public class NewSteps {
    private NewPage newPage;

    @Given("User is on the application")
    public void user_is_on_the_application() {
        DriverManager.getDriver().get("URL");
        newPage = new NewPage(DriverManager.getDriver());
    }

    @When("User performs an action")
    public void user_performs_an_action() {
        newPage.clickElement();
    }

    @Then("User should see expected result")
    public void user_should_see_expected_result() {
        Assert.assertTrue(newPage.isElementDisplayed(), "Element not displayed");
    }
}

πŸ”§ Configuration Options

config.properties Options

Property Description Default Example
app.url Application URL - https://example.com
browser Browser name chrome chrome/firefox/edge/safari
headless Run in headless mode false true/false
browser.window.maximize Maximize browser window true true/false
implicit.wait Implicit wait (seconds) 10 10
explicit.wait Explicit wait (seconds) 20 20
page.load.timeout Page load timeout (seconds) 30 30
retry.count Retry attempts for failed tests 2 0-5
screenshot.on.failure Take screenshot on failure true true/false
screenshot.path Screenshot directory path test-output/screenshots/ custom/path/
test.username Test username - user@example.com
test.password Test password - Pass@123

🎭 Parallel Execution

Enable Parallel Execution

Update src/test/java/com/automation/runners/TestRunner.java:

@DataProvider(parallel = true)
public Object[][] scenarios() {
    return super.scenarios();
}

Update src/test/resources/testng.xml:

<suite name="Test Suite" parallel="methods" thread-count="3">

Thread Count Configuration

  • Set thread-count="3" for 3 parallel threads
  • Adjust based on your machine's capacity
  • Recommended: CPU cores - 1

πŸ› Debugging & Troubleshooting

Enable Verbose Logging

Update testng.xml:

<suite name="Test Suite" verbose="10">

Highlight Elements During Execution

In your test code:

basePage.highlightElement(element);

Dry Run (Validate Step Definitions)

Update TestRunner.java:

dryRun = true  // Will not execute, just validate

Common Issues

Issue: WebDriver not found Solution: Run ./gradlew clean build to download dependencies

Issue: Screenshot directory not found Solution: Framework auto-creates it, but ensure write permissions

Issue: Tests not running Solution: Check tags in TestRunner match feature file tags

πŸ“ Framework Best Practices

1. Page Objects

  • βœ… One page class per web page
  • βœ… Use meaningful element names
  • βœ… Keep page objects independent
  • βœ… Extend BasePage for all page classes

2. Step Definitions

  • βœ… Keep steps simple and readable
  • βœ… Delegate to page objects (no direct Selenium code)
  • βœ… One step definition file per feature
  • βœ… Reuse existing steps where possible

3. Feature Files

  • βœ… Use descriptive scenario names
  • βœ… Tag appropriately (@Smoke, @Regression, etc.)
  • βœ… Keep scenarios independent
  • βœ… Use Background for common preconditions
  • βœ… Use Scenario Outline for data-driven tests

4. Waits

  • βœ… Always use explicit waits (from BasePage)
  • βœ… Never use Thread.sleep()
  • βœ… Use appropriate wait types for the situation

5. Assertions

  • βœ… Use TestNG assertions in step definitions
  • βœ… Provide meaningful assertion messages
  • βœ… Assert expected outcomes clearly

6. Configuration

  • βœ… Keep all configs in config.properties
  • βœ… Never hardcode URLs or credentials
  • βœ… Use environment-specific properties if needed

7. Reporting

  • βœ… Take screenshots on failure (enabled by default)
  • βœ… Use meaningful scenario and step names
  • βœ… Add tags for better report filtering

πŸ“ Project Structure Explained

src/main/java/com/automation/
β”œβ”€β”€ pages/              # Page Object Model
β”‚   β”œβ”€β”€ BasePage.java   # Base class with 100+ reusable methods
β”‚   └── LoginPage.java  # Login page object
└── utils/              # Utility classes
    β”œβ”€β”€ ConfigReader.java      # Reads config.properties
    β”œβ”€β”€ DriverManager.java     # WebDriver lifecycle management
    β”œβ”€β”€ ScreenshotUtil.java    # Screenshot capture utility
    └── RetryAnalyzer.java     # TestNG retry analyzer

src/test/java/com/automation/
β”œβ”€β”€ runners/            # TestNG Cucumber runners
β”‚   β”œβ”€β”€ TestRunner.java      # Main test runner
β”‚   └── SmokeTestRunner.java # Smoke test runner
β”œβ”€β”€ stepdefinitions/    # Cucumber step implementations
β”‚   └── LoginSteps.java
└── hooks/              # Cucumber hooks
    └── Hooks.java      # Before/After scenario hooks

src/test/resources/
β”œβ”€β”€ features/           # Cucumber feature files (BDD)
β”‚   └── Login.feature
β”œβ”€β”€ testdata/           # Test data files (CSV, JSON, Excel)
└── testng.xml          # TestNG suite configuration

πŸ”— Dependencies

Key dependencies managed by Gradle:

  • Selenium WebDriver 4.18.1: Browser automation
  • Cucumber 7.15.0: BDD framework
  • TestNG 7.9.0: Test execution framework
  • Allure 2.25.0: Advanced reporting
  • WebDriverManager 5.6.3: Automatic driver management
  • Apache POI 5.2.5: Excel operations
  • AssertJ 3.25.1: Fluent assertions
  • Gson/Jackson: JSON handling
  • Log4j 2.22.1: Logging

πŸš€ CI/CD Integration

Jenkins

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh './gradlew clean test'
            }
        }
        stage('Allure Report') {
            steps {
                allure includeProperties: false,
                       jdk: '',
                       results: [[path: 'build/allure-results']]
            }
        }
        stage('Cucumber Report') {
            steps {
                cucumber '**/cucumber.json'
                publishHTML([reportDir: 'target/cucumber-reports', reportFiles: 'cucumber.html'])
            }
        }
    }
}

GitHub Actions

name: Selenium Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 17
        uses: actions/setup-java@v2
        with:
          java-version: '17'
      - name: Run tests
        run: ./gradlew clean test
      - name: Generate Allure Report
        run: allure generate build/allure-results --clean -o build/allure-report
      - name: Upload Allure Report
        uses: actions/upload-artifact@v2
        with:
          name: allure-report
          path: build/allure-report/
      - name: Upload Cucumber reports
        uses: actions/upload-artifact@v2
        with:
          name: cucumber-reports
          path: target/cucumber-reports/

πŸ“ž Support & Maintenance

Framework Maintainer

Getting Help

  • Check this README first
  • Review BasePage methods for available actions
  • Examine existing tests for examples
  • Check Gradle build logs for dependency issues

πŸ“ˆ Future Enhancements

Potential improvements for the framework:

  • API testing integration (RestAssured)
  • Database validation utilities
  • Email/SMS verification helpers
  • Docker containerization
  • Performance testing integration
  • Visual regression testing
  • Allure Reports integration βœ… Completed

πŸŽ“ Quick Start Example

# 1. Navigate to project
cd /Users/doddakulanagendrababu/Documents/selenium-cucumber-framework

# 2. Build project
./gradlew clean build

# 3. Install Allure (if not installed)
brew install allure

# 4. Run tests
./gradlew clean test

# 5. View Allure report (recommended)
allure serve build/allure-results

# Or view Cucumber report
open target/cucumber-reports/cucumber.html

Framework Created: October 2025 Test Automation Framework: Selenium + Java + Cucumber + TestNG + Gradle + Allure Ready to Use: Yes βœ…

πŸ“Š View Allure Setup Guide

Happy Testing! πŸš€

About

A robust, enterprise-level test automation framework using Selenium WebDriver, Cucumber BDD, TestNG, and Gradle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published