Meet 2025’s Top-rated Software Test Management Tool. Learn More >

Selenium WebDriver Tutorial: How to Start Automation Testing using Selenium and Cucumber Framework

Selenium WebDriver Tutorial

In this article

In the world of software testing, Selenium WebDriver has emerged as the most used tool for automating web applications. Along with the Cucumber framework, it provides a seamless environment for Behavior-Driven Development (BDD) testing. 

In this post, we will guide you through the process of setting up your automation testing using Selenium WebDriver and Cucumber, while also delving into key aspects like cross-browser testing, parallel testing, Jenkins integration, test suite management, and test data management.

Getting Started with Selenium WebDriver and Cucumber

Before starting the setup process let’s understand basics about Selenium & Cucumber.:

1. Selenium WebDriver: 

Selenium is an open-source testing framework that allows you to automate actions performed by users on a web application. WebDriver, a part of Selenium, provides a browser-specific driver to interact with web elements. The official website for Selenium is: https://www.selenium.dev/

2. Cucumber Framework: 

Cucumber is a BDD tool that enables collaboration between developers and non-developers (testers, business analysts, etc.) in the testing process. It uses a simple syntax to write human-readable test scenarios.The official website for Cucumber is: https://cucumber.io/

To demonstrate these concepts, we will use a demo script available at https://qa-practice.netlify.app/bugs-form . Let’s go through the steps of writing and executing a test scenario.

Writing and Executing a Test Scenario

Let’s try to understand a scenario where we need to test the functionality of a registration form on a web page. Let’s walk through the different steps on setting up the project and executing demo scripts.

Set Up Your Project

Start by creating a Maven or Gradle project and add the necessary dependencies:

  • Selenium WebDriver
  • Cucumber
  • Cucumber JVM
  • JUnit (for running tests)

Feature File

Create a feature file (e.g., registration.feature) with the test scenario in Gherkin syntax:

Feature: User Registration
  Scenario: Successful user registration
    Given I am on the registration page
    When I fill in the registration form
    And I click the Register button
    Then I should see a successful registration message
  1. Feature:
    This keyword is used to define the high-level description of the feature you’re testing. It’s followed by a colon and the name of the feature. In this case, the feature is “User Registration.”
  2. Scenario:
    This keyword is used to define individual test scenarios within a feature. It’s followed by a colon and the name of the scenario. In this case, the scenario is “Successful user registration.”
  3. Given:
    This is a step definition in the Gherkin language that represents the initial context or setup of the test. It’s used to describe the starting point of the scenario. In this case, “I am on the registration page” is the Given step. It usually involves setting up the initial conditions or state of the system.
  4. When:
    This is a step definition that represents an action taken by the user or the system. It describes an event that occurs as part of the test scenario. In this case, “I fill in the registration form” is the When step. It typically involves interacting with the system or performing an action.
  5. And:
    In Gherkin, the And keyword is used to continue a step definition without specifying a new keyword like Given, When, or Then. It’s used to add more steps to a Given, When, or Then section. In this case, “I click the Register button” is an And step, and it’s an extension of the When step.
  6. Then:
    This step definition represents an expected outcome or result of the test scenario. It’s used to define the verification step to confirm that the system behaves as expected. In this case, “I should see a successful registration message” is the Then step. It usually involves assertions or verifications.

These annotations are part of the Gherkin language, which is a human-readable domain-specific language used to define test scenarios in a structured and understandable format. Cucumber then maps these Gherkin statements to the actual step definitions in your code using regular expressions or other mechanisms. This way, the business stakeholders, developers, and testers can collaborate effectively using a common language to define and understand the behavior of the system.

Step Definitions

Create step-definition classes for your scenarios. For the example scenario above, you might have a class like RegistrationSteps.java:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.*;

public class RegistrationSteps {

    private WebDriver driver;

    @Given(“I am on the registration page”)
    public void iAmOnRegistrationPage() {
        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver.exe”);
        driver = new ChromeDriver();
        driver.get(“https://qa-practice.netlify.app/bugs-form”);
    }

    @When(“I fill in the registration form”)
    public void iFillRegistrationForm() {
        WebElement firstNameField = driver.findElement(By.id(“firstName”));
        firstNameField.sendKeys(“John”);

        WebElement lastNameField = driver.findElement(By.id(“lastName”));
        lastNameField.sendKeys(“Doe”);

        WebElement emailField = driver.findElement(By.id(“email”));
        emailField.sendKeys(johndoe@example.com);        WebElement phoneNumberField = driver.findElement(By.id(“phoneNumber”));        phoneNumberField.sendKeys(“1234567890”);



        // Similarly, fill in other form fields
    }

    @And(“I click the Register button”)
    public void iClickRegisterButton() {
        WebElement registerButton = driver.findElement(By.id(“registerBtn”));
        registerButton.click();
    }

    @Then(“I should see a successful registration message”)
    public void iSeeSuccessfulRegistrationMessage() {
        WebElement successMessage = driver.findElement(By.id(“successMessage”));
        String actualMessage = successMessage.getText();

        assertTrue(actualMessage.contains(“Successfully registered the following information”));
        // You can add more assertions or verifications here
    }

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

Runner Class

Create a runner class to execute your Cucumber scenarios. For example, TestRunner.java:

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features = “src/test/resources”,
    glue = “path.to.step.definitions”,
    plugin = {“pretty”, “html:target/cucumber”}
)
public class TestRunner {
}

Replace path.to.step.definitions with the actual package where your step definitions are located.

Run the Tests:

Run your tests using JUnit by executing the TestRunner class. This will execute the Cucumber scenarios you defined in the feature files. You can also execute using Java commands as below:

  • Open a terminal/command prompt.
  • Navigate to the directory where your RunnerTest class is located.
  • Use the following command to execute the runner class:
java -cp “path_to_classpath” cucumber.api.cli.Main –glue “package_with_step_definitions” “path_to_feature_files”

      Here,

  1. path_to_classpath should include the classpath to your compiled classes and necessary libraries.
  2. package_with_step_definitions is the package where your step definitions are located.
  3. path_to_feature_files is the path to the directory containing your feature files.

If you are using maven then simply use “mvn test”

Enhancing Software Testing Efficiency and Quality

To enhance the effectiveness of the Selenium framework, we need to ensure that it has support for cross-browser compatibility, parallel execution, efficient test data management, and Jenkins integration. Let’s briefly discuss each item.

Cross-Browser Compatibility: 

In today’s world, we are required to provide support for the web application across various browsers. However, each browser might interpret and display a website differently. Cross-browser compatibility testing is about ensuring that your application works smoothly across a range of browsers like Chrome, Firefox, Safari, and others. This is crucial to provide a consistent user experience for everyone, regardless of their browser choice. Using tools like Selenium WebDriver, you can automate these tests efficiently and catch compatibility issues early in the development cycle. The best combination is to do Selenium Grid with Docker setup for cross-browser testing.

Parallel Execution: 

As your test suite grows, the time needed to execute all tests can increase significantly. Parallel testing comes to the rescue by allowing multiple tests to run simultaneously. This reduces the overall execution time and provides faster feedback. It’s like having multiple workers complete tasks together, rather than one after the other. This approach not only saves time but also improves efficiency and helps you deliver your software faster.

Jenkins Integration: 

Jenkins is a popular automation server that can be incredibly valuable for software testing. It can automatically trigger test executions whenever new code is added or modified, ensuring that changes don’t break existing functionality. This integration is a core part of continuous testing in the CI/CD (Continuous Integration/Continuous Deployment) pipeline. By catching issues early and often, you can maintain a high level of code quality and deliver updates with confidence.

Efficient Test Management: 

Managing tests can become challenging as the number of tests increases. A well-organized test suite groups related tests together logically, making it easier to run specific tests or test suites. Additionally, efficient test data management is vital. Tests often require specific data to run, and managing this data separately from the tests themselves helps ensure consistency and reliability. By having a solid test management strategy, you can streamline testing processes and ensure comprehensive test coverage.

Conclusion

In this tutorial, we’ve explored the basics of setting up automation testing using Selenium WebDriver and Cucumber. We’ve walked through writing a test scenario, understanding step definitions, and the importance of cross-browser testing, parallel testing, Jenkins integration, test suite management, and test data management. This foundation will empower you to build robust automation frameworks that contribute to the quality and reliability of your software applications. Start your journey into automation testing and unlock new possibilities for efficient and effective software delivery.

Picture of Siddharth

Siddharth

Siddharth is the founder and author of Automation Reinvented and has conducted training sessions on UI/API automation with CICD integration. He also works closely with companies to help them develop new automation tools. Currently working as SDET for a product company and also an active contributor in QA community space

All Posts

Deliver quality software with QA Touch

Questions? Explore our docs, videos, and more just one click away!

Real people with life changing results

Insights from QA Teams on QA Touch’s Impact

Frequently asked questions

Everything you need to know about the product and billing

Why QA Touch?

QA Touch is an AI-driven test management platform built by testers for testers. It simplifies collaboration between developers and QA engineers while helping to manage, track, and organize test cases efficiently. Streamline your testing processes, enhance QA visibility, and deliver high-quality software with ease.

QA Touch offers comprehensive features to manage the entire test management process. From easy migration with CSV files to audio-visual recording of issues and activity logs and a shareable dashboard for real-time reporting to stakeholders, we ensure the testing teams are always on top of things.

Our focus is on providing complete visibility and control over testing workflows and fostering collaboration between testers and other stakeholders (both internal and external). You can have a look at all the features here.

Once you sign up, it takes only 30 minutes to get your QA Touch account up and running. After registration, you will receive an account activation email with all the details. Log in with your account details and create your first test project on QA Touch—it’s that simple. You are now ready to start inviting your team and assigning them roles.

If you are finding it difficult to log in or facing any difficulty, feel free to reach our support team at info@qatouch.com

Why is QA Touch the best test management tool for me?

QA Touch is an AI-driven test management platform that simplifies collaboration between your developers and testers. Beyond creating, organizing, and executing test cases, QA Touch enables you to manage projects, track bugs, and monitor time—all in one platform.

With an intuitive UI and seamless two-way integrations, QA Touch adapts to your workflow, making test management, project oversight, and bug tracking smarter and more efficient.

With secure OKTA, Microsoft Azure SSO, and Google SSO enterprise features, you can stay connected in every app.

We have integrations with dozens of major apps like Slack, Jira, Monday.com, Cypress, and many more. Explore the whole list of integrations now supported here: Explore integrations

The test management tool is a modern software application that helps QA teams and developers manage their testing process efficiently. It provides a structured approach to creating, organizing, executing, and tracking tests to ensure software applications meet specified requirements and function properly before release.

Don’t just take our word for it.

QATouch is a leader in G2 market reports.