</>

Technology

Cucumber

Difficulty

Intermediate

Interview Question

How can Cucumber be integrated with Selenium WebDriver?

Answer

Cucumber + Selenium WebDriver Integration

Step 1: Add Maven Dependencies

XML
<dependencies>
    <!-- Selenium WebDriver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.18.0</version>
    </dependency>

    <!-- Cucumber Java -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.15.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Cucumber TestNG -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.15.0</version>
        <scope>test</scope>
    </dependency>

    <!-- WebDriverManager (auto driver setup) -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.7.0</version>
    </dependency>
</dependencies>

Required JARs (Legacy - without Maven)

CODE
cucumber-core-7.x.jar
cucumber-java-7.x.jar
cucumber-testng-7.x.jar
gherkin-26.x.jar
selenium-java-4.x.jar

Step 2: Write Feature File

Gherkin
# src/test/resources/features/login.feature
Feature: Login to Application

  Scenario: Successful Login
    Given the browser is launched and application is opened
    When the user enters username "admin" and password "Admin@123"
    And the user clicks the Login button
    Then the user should be on the dashboard page

Step 3: Create Step Definitions

Java
package stepDefinitions;

import io.cucumber.java.en.*;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import static org.testng.Assert.*;

public class LoginSteps {

    WebDriver driver;

    @Before
    public void setup() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Given("the browser is launched and application is opened")
    public void launchApp() {
        driver.get("https://example.com");
    }

    @When("the user enters username {string} and password {string}")
    public void enterCredentials(String user, String pass) {
        driver.findElement(By.id("username")).sendKeys(user);
        driver.findElement(By.id("password")).sendKeys(pass);
    }

    @When("the user clicks the Login button")
    public void clickLogin() {
        driver.findElement(By.id("loginBtn")).click();
    }

    @Then("the user should be on the dashboard page")
    public void verifyDashboard() {
        assertTrue(driver.getTitle().contains("Dashboard"));
    }

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

Step 4: Create TestRunner

Java
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

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

Follow AutomateQA

Related Topics