How to use Cucumber with Selenium

  

How to Integrate Cucumber with Selenium in Java?

Cucumber is a testing framework that supports Behavior-Driven Development (BDD), which allows writing test cases in natural language using Gherkin syntaxSelenium is used for automating web applications. Together, they help in writing BDD-style automation tests for web applications.


Below is the Step-by-Step Integration of Cucumber with Selenium in Java.


Prerequisites to write this code:

  • Java installed

  • Maven or Gradle (we’ll use Maven)

  • IDE (e.g., IntelliJ or Eclipse)


1. Maven Project Setup


Below are pom.xml dependencies:
<dependencies>
    <!-- Cucumber -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.20.0</version>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>



2. Directory Structure


src/test/java ├── features │ └── Login.feature ├── stepdefinitions │ └── LoginSteps.java ├── runners │ └── TestRunner.java └── pages └── LoginPage.java

Below is the feature file to write plain english scenario using gherkin keywords under feature.

3. Login.feature


Feature: Login functionality

  Scenario: Successful login with valid credentials
    Given User is on the login page
    When User enters valid username and password
    And clicks the login button
    Then User should be redirected to the home page


Below is page class which is implemented in selenium java for writing web page automation
logic.


4. LoginPage.java

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
    WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    By username = By.id("username");
    By password = By.id("password");
    By loginBtn = By.id("login");

    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(loginBtn).click();
    }
}


Below is step definition class which is implemented in selenium java for implementing
feature file steps in the form of java codes.

5. LoginSteps.java

package stepdefinitions;

import io.cucumber.java.en.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.LoginPage;

public class LoginSteps {
    WebDriver driver;
    LoginPage login;

    @Given("User is on the login page")
    public void user_on_login_page() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        login = new LoginPage(driver);
    }

    @When("User enters valid username and password")
    public void user_enters_credentials() {
        login.enterUsername("testuser");
        login.enterPassword("password123");
    }

    @When("clicks the login button")
    public void click_login_button() {
        login.clickLogin();
    }

    @Then("User should be redirected to the home page")
    public void user_on_home_page() {
        String title = driver.getTitle();
        assert title.contains("Home");
        driver.quit();
    }
}


Below is the Test runner java class implemented in cucumber with testng to run
the script.


6. TestRunner.java

package runners;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/java/features",
    glue = {"stepdefinitions"},
    plugin = {"pretty", "html:target/cucumber-report.html"},
    monochrome = true
)
public class TestRunner {
}


Running the Test

  • Right-click TestRunner.java → Run as JUnit Test

  • It will execute the steps in the feature file using Selenium


Summary

ComponentRole
feature fileDefines scenarios in plain English
Step DefinitionsLinks Gherkin steps to Selenium actions
Page ClassRepresents elements using POM
TestRunnerCucumber JUnit runner

No comments:

Post a Comment