Test Automation using Selenium and Cucumber
Related Product On This Page What is Cucumber in Selenium?May 23, 2026 · 7 min read · Tool Comparison
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. 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. 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. 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. to discuss your testing challenges, automation strategies, and tool integrating. Gain actionable perceptiveness tailored to your projects and ensure fast, more reliable software delivery. Cucumber BDD fabric mainly consists of three major parts & # 8211;Feature File, Step Definitions, and the Test Runner 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. Know 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: 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. 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: 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. Must Read: Here are some of the: 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. On This Page # Ask-and-Contributeabout this theme with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
Test Automation using Selenium and Cucumber
Want to integrate Selenium with Cucumber?
What is Cucumber in Selenium?
Cucumber and Selenium Testing: A Collaborative Approach
Get Expert QA Guidance Today
Want to integrate Selenium with Cucumber?
The Cucumber Framework: BDD Framework for Selenium
1. Feature File
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
2. Step Definitions
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 feature3. Test Runner File
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
Prerequisites for Cucumber and Selenium Setup
How to write a test in Cucumber for Selenium Automation?
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 ();}}Best Practices in Cucumber Testing
Conclusion
Want to integrate Selenium with Cucumber?
Related Guides
Automate This With SUSA
Test Your App Autonomously