Test Automation using Selenium and Cucumber

Related Product On This Page What is Cucumber in Selenium?May 23, 2026 · 7 min read · Tool Comparison

Related Product

Test Automation using Selenium and Cucumber

Are you struggling to bridge the gap between developer and non-technical stakeholders in your test automation workflow? While Selenium is a powerful puppet for automatize web coating, the need for clear, understandable test cases often create a barrier between technical and non-technical team. This is where Cucumber comes in.

Want to integrate Selenium with Cucumber?

Get skilful guidance to make seamless, BDD-driven mechanization workflows with Selenium and Cucumber.

Cucumber, with its Behavior-Driven Development (BDD) approach, allows squad to write tests in plain English, do them easily understandable for everyone involved, regardless of their technical expertness.

By integrating Cucumber with Selenium, teams can automate tests while keeping them readable, maintainable, and aligned with business goals.

This guide explores how to implement mechanization use Cucumber and Selenium, include setup steps, hardheaded examples, and best practices for politic, more collaborative testing.

What is Cucumber in Selenium?

Cucumber Framework in Selenium is an open-source examination framework that support Behavior Driven Development for. The tests are first written in a simple scenario form that describes the expected behaviour of the system from the user ’ s position.

Largely used for acceptance tests, Cucumber is pen in Ruby, while the tests are written in Gherkin, a non-technical and human-readable language.

Cucumber and Selenium Testing: A Collaborative Approach

While adds accuracy and speed to the development cycle, Cucumber adds an extra edge to it, make it more collaborative for non-technical direction stakeholder. Widely beneficial for User Acceptance Testing where the are largely driven by doings, Cucumber strengthens Automation Testing.

Read More:

To scale your Selenium testing effortlessly, BrowserStack Automate provides a cloud-based infrastructure that lets you run WebDriver tests across thousands of real browsers and devices, ensuring quicker execution, higher accuracy, and better test reportage.

Get Expert QA Guidance Today

to discuss your testing challenges, automation strategies, and tool integrating. Gain actionable perceptiveness tailored to your projects and ensure fast, more reliable software delivery.

Want to integrate Selenium with Cucumber?

Get expert guidance to create seamless, BDD-driven automation workflows with Selenium and Cucumber.

The Cucumber Framework: BDD Framework for Selenium

Cucumber BDD fabric mainly consists of three major parts & # 8211;Feature File, Step Definitions, and the Test Runner File.

1. Feature File

A standalone unit or a individual functionality (such as a login) for a project can be called a Feature. Each of these lineament will have scenarios that must be tested utilise Selenium integrated with Cucumber. A file that stores data about features, their descriptions, and the scenarios to be tested is called aFeature File.

Cucumber tests are written in these Feature Files that are stored with the extension & # 8211;“ .feature ”. A Feature File can be given a description to make the documentation more legible.

Example:

The Login use on a site

Feature File Name: userLogin.feature

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Description: The user shall be capable to login upon inscribe the right username and password in the correct fields. The user should be directed to the homepage if the username and parole entered are right.

Keywords such asGIVEN, WHEN, and THENused to write the test in Cucumber are calledAnnotations.

GIVEN user navigates to login page by opening Firefox WHEN exploiter enters correct & lt; username & gt; AND & lt; password & gt; value THEN exploiter is direct to the homepage

Know

2. Step Definitions

Now that the features are compose in the feature register, the code for the related scenario has to be run. To know which batch of code needs to be run for a given scenario, Steps Definitions come into the picture. A Steps Definitions file stores the function data between each step of a scenario defined in the lineament file and the code to be executed.

Step Definitions can use both bid for the Java functions write to map a lineament file to the codification.

Example:

package StepDefinition; import cucumber.api.java.en.Given; importation cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class Steps {@ Given (`` ^user navigates to the login page by opening Firefox $ '') //Code to Open Firefox Browser and establish the login page of application to define the GIVEN step of the feature @ When (`` ^user enters rectify username and parole values $ '') //take inputs for username and password fields using find element by xpath. Put the correct username and password values as comment to define the WHEN stride of the feature @ Then (“ ^user gets directed to homepage $ ”) //Direct to the Homepage of the application as a termination of correct username and password inputs in the WHEN step. This would define the THEN step of the feature

3. Test Runner File

To run the test, one take aTest Runner File, which is a JUnit Test Runner Class comprise the Step Definition position and the other master metadata expect to run the test.

The Test Runner File uses the@ RunWith ()Annotation from JUnit for accomplish tests. It also uses the@ CucumberOptionsAnnotation to specify the location of lineament files, step definition, reporting integrations, etc.

Example:

Test Runner Class in cucumberTest software, with the feature file in“ src/test/Feature” location and Step Definition files in“ src/main/stepDefinition ” folder.

parcel cucumberTest; import org.junit.runner.RunWith; importation cucumber.api.CucumberOptions; importation cucumber.api.junit.Cucumber; @ RunWith (Cucumber.class) @ CucumberOptions (feature = `` src/test/Feature '', glue= {`` src/main/stepDefinition ''}) public grade TestRunner {}

Setting up Cucumber BDD Framework for Selenium

Before exploring how Cucumber work and how to write a Cucumber Selenium test, let ’ s 1st fig out how to set up Cucumber. Installing the prerequisites observe below is all a user take to start with.

Also Read:

Prerequisites for Cucumber and Selenium Setup

How to write a test in Cucumber for Selenium Automation?

Taking forward the scenario of the login feature, let ’ s create a sample test in Cucumber. This codification will run theLogin Scenariotrace in theFeaturesection and will open the website home page upon entering the right username and password.

package cucumberTest; import io.cucumber.java.After; importation org.openqa.selenium.By; import org.openqa.selenium.WebDriver; importee org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; meaning io.cucumber.java.en.When; public form Steps {//Opening Firefox Browser and launching the login page of application private net WebDriver driver = new FirefoxDriver (); @ Given (`` user navigate to the login page by open Firefox '') public void user_is_on_login_page () {driver.get (`` //Login Page URL '');} //Entering correct username and password values @ When (`` user enters correct username and password values '') public void enter_Username_Password () {driver.findElement (By.xpath (`` .// * [@ id='username '] /a '')) .sendkeys (“ //CorrectUsername value ”); driver.findElement (By.xpath (`` .// * [@ id='password '] /a '')) .sendkeys (“ //CorrectPassword value ”);} //Open homepage upon login @ Then (`` user gets directed to homepage '') public void direct_to_homepage () throws Throwable {driver.get (“ Homepage url ”);} @ After () populace void closeBrowser () {driver.quit ();}}

Must Read:

Best Practices in Cucumber Testing

Here are some of the:

  • The versions ofCucumber-java, Cucumber-junit, and Cucumber-corejars should be the same for seamless connectivity.
  • Adding an after hook to the code for capturing screenshots when the test fails can help name the issue and debug it.
  • Use Tags for mastermind tests ground on tag definition. This helps in lawsuit where all tryout don ’ t feature to be run every clip. Such tests can be marked expend tags and run alone when required. This saves time and processing capability of the scheme and resources.
  • As always, it is significant to run the Cucumber Selenium tests on real browser and devices. BrowserStack offers a of 3000+ existent browsers and devices for testing purposes & # 8211; both unrecorded and automated. Simply sign up, choose the needed device-browser-OS combination, and start testing websites for complimentary.

Conclusion

is absolutely essential to keep up with the grow demands of faster speech and optimum character on testers. Cucumber framework in Selenium allows exam scenarios to be indite utilize natural language constructs that describe the expected behavior of the package for a given scenario, make it ideal for.

Lastly, testing websites on a is the only way to ascertain consummate truth of results. So, invariably opt for real device testing to comprehensively control website execution,, and the quality of user experience it offer.

Want to integrate Selenium with Cucumber?

Get expert guidance to create seamless, BDD-driven automation workflows with Selenium and Cucumber.
Tags
71,000+ Views

# Ask-and-Contributeabout this theme with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free