Java-Selenium-Cucumber

Java Selenium Cucumber Project Structure

Here’s the enhanced project structure including Java, Cucumber, Selenium, Maven, the Page Object Model with PageFactory, .github (CI/CD for nightly builds and push builds), a .gitignore file, and README.md, CODEOWNERS, and a pull request template.


Project Structure

MyAutomationProject/
├── .github/
│   └── workflows/
│       ├── nightly-build.yml          # Nightly build pipeline
│       ├── push-build.yml             # Build triggered on push
│   ├── CODEOWNERS                     # Code owners file
│   ├── pull_request_template.md       # Pull request template
├── .gitignore                         # Files and folders to ignore in git
├── README.md                          # Project documentation
├── pom.xml                            # Maven configuration file
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── myproject/
│   │   │           ├── base/
│   │   │           │   ├── BaseTest.java           # Base class for tests
│   │   │           │   └── DriverFactory.java      # Manages WebDriver instances
│   │   │           ├── config/
│   │   │           │   └── ConfigReader.java       # Reads configuration files
│   │   │           ├── pages/
│   │   │           │   ├── LoginPage.java          # Login Page with PageFactory
│   │   │           │   └── DashboardPage.java      # Dashboard Page with PageFactory
│   │   │           └── utils/
│   │   │               ├── TestUtils.java          # Shared utility methods
│   │   │               └── WaitUtils.java          # Explicit wait utility
│   └── test/
│       ├── java/
│       │   └── com/
│       │       └── myproject/
│       │           ├── runners/
│       │           │   └── TestRunner.java         # Cucumber test runner
│       │           ├── stepdefinitions/
│       │           │   └── LoginSteps.java         # Step definitions for login feature
│       │           └── hooks/
│       │               └── Hooks.java              # Cucumber hooks (setup and teardown)
│       └── resources/
│           ├── features/
│           │   └── Login.feature                   # Cucumber feature file for login
│           ├── cucumber.properties                 # Cucumber properties configuration
│           ├── testng.xml                          # Optional: TestNG suite configuration
│           └── config.properties                   # Configuration properties
└── target/                                         # Maven build output

Examples of Files


README.md

# MyAutomationProject

## Overview
This project is a Selenium-based automation framework built with Java, Cucumber, and Maven. It follows the Page Object Model (POM) with PageFactory for a clean and maintainable design.

## Features
- Behavior-Driven Development (BDD) using Cucumber.
- Selenium-based browser automation.
- Page Object Model with PageFactory.
- Test execution and reporting using Cucumber.
- Continuous Integration using GitHub Actions (nightly builds and push-triggered builds).

## Prerequisites
- Java 11 or higher.
- Maven installed.
- WebDriver binaries managed using WebDriverManager.

## How to Run Tests
1. Clone the repository.
2. Navigate to the project root.
3. Run tests:
   ```bash
   mvn test

Continuous Integration

  • Nightly builds configured in .github/workflows/nightly-build.yml.
  • Builds triggered on each push to the main branch in .github/workflows/push-build.yml.

Contributing

  1. Fork the repository.
  2. Create a new branch for your feature.
  3. Commit your changes.
  4. Submit a pull request (PR).
---
#### **`.github/CODEOWNERS`**
```plaintext
# Code owners file
# Specify individuals or teams responsible for specific files or directories.
# Format: path @github-username

# Owners for all files
* @owner1 @owner2

# Owners for specific folders
.github/ @ci-team
src/main/ @dev-team
src/test/ @qa-team

.github/pull_request_template.md

# Pull Request Template

## Description
- Summary of the changes and the related issue this PR addresses.

## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update

## Checklist
- [ ] Code adheres to the coding standards.
- [ ] Tests have been added and pass locally.
- [ ] Documentation has been updated (if applicable).

## Related Issues
- Resolves #[issue-number]

## Additional Notes
Provide any additional context or details here.

.github/workflows/nightly-build.yml

name: Nightly Build

on:
  schedule:
    - cron: '0 2 * * *' # Runs every day at 2 AM UTC

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Java
      uses: actions/setup-java@v3
      with:
        java-version: 11
        distribution: 'adopt'

    - name: Install dependencies
      run: mvn install

    - name: Run tests
      run: mvn test

.github/workflows/push-build.yml

name: Build on Push

on:
  push:
    branches:
      - main
      - 'feature/**'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Java
      uses: actions/setup-java@v3
      with:
        java-version: 11
        distribution: 'adopt'

    - name: Install dependencies
      run: mvn install

    - name: Run tests
      run: mvn test

Login.feature

Feature: Login

  Scenario: Successful login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be redirected to the dashboard

TestRunner.java

package com.myproject.runners;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"com.myproject.stepdefinitions", "com.myproject.hooks"},
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}

LoginSteps.java

package com.myproject.stepdefinitions;
import com.myproject.pages.LoginPage;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.testng.Assert;

public class LoginSteps {

    private LoginPage loginPage;

    public LoginSteps() {
        loginPage = new LoginPage(Hooks.getDriver());
    }

    @Given("the user is on the login page")
    public void theUserIsOnTheLoginPage() {
        Hooks.getDriver().get("https://example.com/login");
    }

    @When("the user enters valid credentials")
    public void theUserEntersValidCredentials() {
        loginPage.login("testuser", "password");
    }

    @Then("the user should be redirected to the dashboard")
    public void theUserShouldBeRedirectedToTheDashboard() {
        Assert.assertEquals(Hooks.getDriver().getTitle(), "Dashboard");
    }
}

Hooks.java

package com.myproject.hooks;
import com.myproject.base.DriverFactory;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import org.openqa.selenium.WebDriver;

public class Hooks {

    private static WebDriver driver;

    @Before
    public void setup() {
        driver = DriverFactory.getDriver();
        driver.manage().window().maximize();
    }

    @After
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    public static WebDriver getDriver() {
        return driver;
    }
}

This structure incorporates Cucumber for BDD testing with step definitions, feature files, and hooks for setup/teardown. It also includes robust CI/CD workflows and documentation for a complete automation framework.