Comprehensive Guide to Selenium, Java, TestNG, and Cucumber for Automated Testing

 Comprehensive Guide to Selenium, Java, TestNG, and Cucumber for Automated Testing

You're working with Selenium + Java + TestNG + Cucumber, which is a powerful combination for automated testing. Here's a deep dive into each component and how they work together:


1️.Understanding the Components

🔹 Selenium

  • A browser automation tool used for UI testing.
  • Supports multiple browsers (Chrome, Firefox, Edge, etc.).
  • Works with different languages (Java, Python, C#, etc.).

🔹 Java

  • The programming language used to write test scripts.
  • Supports Object-Oriented Programming (OOP) for structured test automation.

🔹 TestNG

  • A testing framework designed for parallel execution, annotations, and reporting.
  • Works well with Selenium for test execution and assertions.

🔹 Cucumber

  • A Behavior-Driven Development (BDD) tool using Gherkin syntax.
  • Makes test cases more readable and understandable for non-technical stakeholders.

2️.Setting Up the Framework

🔹 Maven Dependencies (pom.xml)

To integrate all components, include these dependencies:

xml

<dependencies>

    <!-- Selenium -->

    <dependency>

        <groupId>org.seleniumhq.selenium</groupId>

        <artifactId>selenium-java</artifactId>

        <version>4.10.0</version>

    </dependency>

 

    <!-- Cucumber Dependencies -->

    <dependency>

        <groupId>io.cucumber</groupId>

        <artifactId>cucumber-java</artifactId>

        <version>7.13.0</version>

    </dependency>

    <dependency>

        <groupId>io.cucumber</groupId>

        <artifactId>cucumber-testng</artifactId>

        <version>7.13.0</version>

    </dependency>

 

    <!-- TestNG -->

    <dependency>

        <groupId>org.testng</groupId>

        <artifactId>testng</artifactId>

        <version>7.7.1</version>

        <scope>test</scope>

    </dependency>

</dependencies>


3️ Writing Tests with Cucumber and Selenium

Step 1: Create a Feature File

  • Located in src/test/resources/features/login.feature
  • Uses Gherkin syntax:

gherkin

Feature: Login functionality

 

  Scenario: Successful login

    Given the user is on the login page

    When the user enters valid username and password

    And clicks the login button

    Then the user should be redirected to the homepage


Step 2: Implement Step Definitions

  • Located in src/test/java/stepDefinitions/LoginSteps.java
  • Uses Selenium WebDriver:

java

package stepDefinitions;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.en.*;

import org.openqa.selenium.By;

 

public class LoginSteps {

    WebDriver driver;

 

    @Given("the user is on the login page")

    public void userOnLoginPage() {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        driver = new ChromeDriver();

        driver.get("https://example.com/login");

    }

 

    @When("the user enters valid username and password")

    public void userEntersCredentials() {

        driver.findElement(By.id("username")).sendKeys("testuser");

        driver.findElement(By.id("password")).sendKeys("password123");

    }

 

    @And("clicks the login button")

    public void userClicksLoginButton() {

        driver.findElement(By.id("loginBtn")).click();

    }

 

    @Then("the user should be redirected to the homepage")

    public void userRedirectedToHomepage() {

        String expectedUrl = "https://example.com/home";

        String actualUrl = driver.getCurrentUrl();

        assert actualUrl.equals(expectedUrl) : "Test Failed: Incorrect URL";

        driver.quit();

    }

}


Step 3: Create the Cucumber Test Runner

  • Located in src/test/java/testRunner/TestRunner.java
  • Uses TestNG for execution:

java

package testRunner;

 

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

 

@CucumberOptions(

    features = "src/test/resources/features",

    glue = "stepDefinitions",

    plugin = {"pretty", "html:target/cucumber-reports.html"},

    monochrome = true

)

public class TestRunner extends AbstractTestNGCucumberTests {

}


4️.Running Tests

Using Maven Command

mvn test

Using TestNG

  1. Create a testng.xml file:

xml

<suite name="CucumberTestSuite">

    <test name="CucumberTests">

        <classes>

            <class name="testRunner.TestRunner"/>

        </classes>

    </test>

</suite>

  1. Run it in the terminal:

mvn clean test


5️.Enhancements

Page Object Model (POM)

  • Keeps locators and actions separate from test scripts.
  • Example LoginPage.java:

java

package pages;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class LoginPage {

    WebDriver driver;

 

    By username = By.id("username");

    By password = By.id("password");

    By loginButton = By.id("loginBtn");

 

    public LoginPage(WebDriver driver) {

        this.driver = driver;

    }

 

    public void enterUsername(String user) {

        driver.findElement(username).sendKeys(user);

    }

 

    public void enterPassword(String pass) {

        driver.findElement(password).sendKeys(pass);

    }

 

    public void clickLogin() {

        driver.findElement(loginButton).click();

    }

}


Parallel Execution with TestNG

Modify testng.xml to enable parallel execution:

xml

<suite name="ParallelSuite" parallel="tests" thread-count="2">

    <test name="ChromeTest">

        <parameter name="browser" value="chrome"/>

        <classes>

            <class name="testRunner.TestRunner"/>

        </classes>

    </test>

 

    <test name="FirefoxTest">

        <parameter name="browser" value="firefox"/>

        <classes>

            <class name="testRunner.TestRunner"/>

        </classes>

    </test>

</suite>


Reporting

  • Cucumber Report Plugin:
    • Modify @CucumberOptions in TestRunner.java:

java

plugin = {

    "pretty",

    "html:target/cucumber-reports.html",

    "json:target/cucumber-reports.json"

}

  • Extent Reports with TestNG
    • Use ExtentReports for advanced reporting.

Conclusion

🔹 Cucumber + Selenium + Java + TestNG provides a structured and scalable way to automate testing.
🔹 Use Page Object Model (POM) and parallel execution for maintainability and efficiency.
🔹 Generate HTML and JSON reports to analyze test results.


Do you need help with parallel execution, reporting, CI/CD (Jenkins), or handling dynamic web elements? 🚀

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
https://otieu.com/4/9433883
https://otieu.com/4/9433883