In this article, we will learn how to integrate Selenium with Cucumber to implement a BDD-based automation framework using Selenium WebDriver. By leveraging Selenium with Cucumber, you can achieve powerful test automation.
Integrating Selenium with Cucumber enhances the testing process by enabling a collaborative environment.
Selenium with Cucumber: A Powerful Combination
Using Selenium with Cucumber enables teams to collaborate effectively and write clear test scenarios.Let us consider a scenario where we can utilize Selenium with Cucumber to enhance our testing capabilities.The integration of Selenium with Cucumber opens up new possibilities for behavior-driven testing.When testing applications, using Selenium with Cucumber can streamline the process significantly.Integrating Selenium with Cucumber can greatly enhance your testing efficiency.
What is BDD?
Understanding Selenium with Cucumber allows teams to define tests more collaboratively.
Using Selenium with Cucumber helps in maintaining clear communication among team members.With Selenium with Cucumber, you can write understandable test cases.Utilizing Selenium with Cucumber offers significant advantages in test automation.Choosing to utilize Selenium with Cucumber offers distinct advantages in the automation landscape.Utilizing Selenium with Cucumber also fosters better communication between technical and non-technical stakeholders.Following Selenium with Cucumber best practices leads to better test management.When you consider Selenium with Cucumber, it allows for clear requirements definition.Selenium with Cucumber allows for integrating automated tests into your development workflow seamlessly.Incorporating Selenium with Cucumber in your workflow can enhance productivity.
BDD (Behavior Driven Development) is a technique where you define your specifications or test cases using simple English-like sentences using Gherkin language. With this technique, the non-technical team members can easily understand the flow and collaborate easily with other team members in the process of software development. Let me consider a simple example.
With Selenium with Cucumber, teams can deliver reliable software more efficiently.By employing Selenium with Cucumber, teams can craft clear and concise test scenarios.The synergy of Selenium with Cucumber can significantly enhance the overall quality of the software being tested.Many prefer Selenium with Cucumber for its user-friendly approach to testing.By adopting Selenium with Cucumber, teams can create robust and maintainable test suites.
Let us consider a scenario where you want to test Google Homepage. In one of the test scenarios, we will verify that the page displays all the main elements. As part of the test case, you want to check that the Google Homepage displays the search text box, Gmail link, Images link, “Google Search” button, and “I’m Feeling Lucky” button. You can write this scenario in the below format with BDD.
Scenario: Login to Facebook
GIVEN User Already sign up for FacebookMany find that using Selenium with Cucumber streamlines their automated testing processes.WHEN User navigate to Facebook pageAND Enter correct username and passwordTHEN Verify homepage should be properlyCombining Selenium with Cucumber helps streamline the testing process.
Why Cucumber?
In the current market, there are multiple behavior-driven development tools such as Cucumber, SpecFlow, JDave, etc. Among all them, Cucumber is one of the most popular tools because of the following reasons.
- Cucumber BDD is an open-source tool.
- Cucumber Framework supports multiple languages where you can write your test scripts in multiple languages such as Java, Python, Ruby, .NET, etc.
- Cucumber support behaviour-driven development (BDD) automation and can be easily integrated with Selenium, Waitr, Ruby on Rails, and other web-based testing tools.
Teams adopting Selenium with Cucumber will see improved collaboration.By choosing Selenium with Cucumber, you ensure clarity in test cases.To fully leverage Selenium with Cucumber, proper setup is essential.Start by ensuring you have the tools for Selenium with Cucumber integration.Understand the setup requirements for Selenium with Cucumber before you begin.
Why Use Selenium with Cucumber?
Once you have set up Selenium with Cucumber, testing becomes straightforward.
Each jar is crucial for the effective functioning of Selenium with Cucumber.
Cucumber and Selenium with Cucumber are the two popular technologies in the current market. Now a day’s for functional testing most organizations are using Selenium with Cucumber.
Incorporating Selenium with Cucumber in your testing framework is highly beneficial.
- Software Engineer and Business Analyst.
- Manual Tester and Developers.
- Manual Tester and Automation Tester.
Using BDD (Behavior Driven Development) you define your specifications or test cases using simple English-like sentences using Gherkin language. With this technique, the non-technical team members can easily understand the flow and collaborate easily with other team members in the process of software development.
Prerequisite required for using Cucumber with Selenium.
We need the following items before we start integration cucumber with selenium:
Step 1: Download and install the Java platform on your machine
Step 2: Download and install Eclipse IDE
Step 3: Download Cucumber and Selenium JARs
There are few core Cucumber jars files and some additional dependent jars that you need to download.
- Cucumber-core
- Cucumber-Html
- cobertura code coverage
- Cucumber-java
- Cucumber-jUnit
- Cucumber-jvm-deps
- Cucumber-reporting
- Hemcrest-core
- Gherkin
- Junit
Let us look at the steps to download all these jar files.
Steps to download Cucumber Jars
The seamless integration of Selenium with Cucumber can lead to efficient test management.
Using the below steps you can download all the cucumber related jars from Maven Repository. You need to search for files and individually we need to download them one by one. We will give the detailed steps to download one of the jar files, i.e “cucumber-core jar.” In the same way, you can follow the same steps to download the remaining jars as well. The steps are given below:
1. Open Maven Repository using this link – https://mvnrepository.com/
2. In the Search text box, search for the particular jar ‘cucumber-core’ as shown in the below screenshot.
In the above image, in the search results, you will see that the same file name, cucumber-core, comes up 2 times. The first result is with the artifact id – io.cucumber. And the second one is with the id info.cukes.
Note: io.cucumber is the new version and info.cukes are the older version. Wherever you can see both io.cucumber and info.cukes, then you need to go with the version io.cucumber as its artifact id.
3. Click on the cucumber-core which has id io.cucumber.
It will redirect to a page that shows a different version of this jar file.
- To open click on the latest version. You will see the page as shown below
5. Click on the Cucumber Core jar to download the jar file to save the jar file on your machine.
6. Download the remaining jar files by following the same steps.
Note: As JUnit and mockito jars are not part of the main cucumber library you will not find io.cucumber/info.cukes for JUnit and mockito jars. These two are external dependencies that cucumber needs to run correctly.
We have downloaded all the jar files in one place as shown below.
Download Selenium Webdriver Jar files
- Selenium-server-standalone jars can be downloaded from https://www.selenium.dev/downloads/
2. Once click on the Download button you can see the Selenium Webdriver jars would start downloading.
3. Unzip the folder once the downloaded is complete.
Create a new Cucumber Eclipse project
Step 1: Launch the Eclipse
Step 2: Create a new Java project. Select File -> New -> Java Project
Step 3: Enter the Project Name and click on the Finish button
Step 4: Eclipse IDE would now look like below. It will display your newly created project.
With Selenium with Cucumber, you can achieve comprehensive test coverage efficiently.
Adding Cucumber and Selenium jars to your Eclipse project
1. Right-click on the Project and click Build Path > Click on Configure Build Path
2. Click on the Libraries tab, click on the Add External JARS button. Add all the libraries downloaded earlier.
3. You can see that all the cucumber related jars are now added to the Libraries tab as shown below
- Now add Selenium webdriver jars to the project. Make sure that you add client-combined jars, as well as the jars under the lib folder, as shown below
Add client combined jars
Add all jars under the lib folder
5. Once all the jar files are added, click on Apply and Close button. You can see a new sub-section Referenced Libraries inside the project folder. Expand this Referenced Libraries section. You will see that all the jar files that you had added when you expand it.
Create a cucumber feature file
Follow the below steps to first create a folder in your project and add the feature file to it.
1. Right-click on the project, then select New > Source Folder.
2. Now enter the folder name as ‘resources’ and click on the ‘Finish’ button
3. You can see that the resources folder is added to your project. Right-click on the resources folder and click on New and click Package
4. Enter the package name as features and click on the Finish button. Inside this package, you will add cucumber feature files
- Right-click on features package and select New > Click on Other, toadd cucumber feature file to this package.
6. Expand the General folder and then select the File option and click on the Next button
7. Enter file name as FacebookHomepage.feature. Make sure that at the end of the file name you add .feature. Now click on the Finish button
Now the cucumber feature file is added to the features package and you can also see a new popup window, Editors available on the Marketplace if you are using Cucumber in Eclipse for the first time. In the pop-up window, let the first option selected and click on the OK button.
You will see the Eclipse Marketplace window as shown below
2. Click on the Install button and Eclipse will start loading the files.
Click on the Confirm button. The Eclipse will now start loading the components. Once finished, you will see the License agreement screen
Check the checkbox ‘I accept the terms of license agreement’ and click on the Finish button. The Eclipse will start installing the tool.
Click on the Install Anyway button if you see a Security Warning popup.
You will see a Restart popup once the eclipse installs the plugin. To restart eclipse click on the Restart Now button.
Add test case to cucumber feature file
First, you will add the test case to the feature file. A particular syntax you need to follow in the Cucumber feature file.
Add the below content once you open the feature file.
Adding Cucumber Test Runner class to the Eclipse project
1. To add the test runner class first we need to have a new package. Right-click on the src folder and click on New and click Package
2. Enter the package name as testRunners and click on the Finish button
Using Selenium with Cucumber not only improves test readability but also facilitates collaboration.
As you explore Selenium with Cucumber, your understanding of BDD will deepen.
Adopting Selenium with Cucumber in your workflow can transform your testing approach.
3. The created package will be added to the project. Right-click on the package and click New and click Class
4. Give class name as TestRunner_FacebookHomepage and click on the Finish button
Ultimately, Selenium with Cucumber exemplifies the future of efficient testing.
Once you click on the finish button you can see that the Test runner class is successfully added to your eclipse project. Now to the test runner class let us add code.
In this section, you will add the basic code to run the cucumber feature file. You need to add two annotations to your test runner class. These annotations are
@RunWith(Cucumber.class)
@CucumberOptions(features=”<path of feature file>”, glue=”<path of definition class>”)
The complete code for the cucumber test runner class is as below.
package testRunners;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features="resources/features", glue="Def")
public class TestRunner_FacebookHomepage {
}
By using the following annotations in the above code we run the cucumber test:
@RunWith() This annotation specifies the test runner class to start executing our tests.
@CucmberOptions() This annotation will set some important properties information for our cucumber test like feature file, step definition, etc. in your project.
The Screenshot of the TestRunner file will look like as below.
How to create Cucumber Step Definition class
Here we create a new package and add a new step definition class.
- Right-click on the src folder and click New > Package
- Enter package name as stepDefinitions and click on the Finish button
The next step in Selenium with Cucumber is to define your tests clearly.
- Right-click on the created package and click New > Class
1. How does Selenium with Cucumber benefit testing?
Choosing to use Selenium with Cucumber is becoming increasingly popular among development teams.
In conclusion, mastering Selenium with Cucumber is essential for modern test automation strategies.
4. Enter the class name as StepDefs_FacebookHomepage and click on the Finish button
Add step definition methods to the class
package stepDefinitions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class StepDefs_FacebookHomepage {
WebDriver driver;
@Given("user navigates to facebook website")
public void invokeBrowser() {
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get("https://facebook.com");
}
@Then("user enters the emailId as (.*)")
public void enterEmailId(String emailId) {
driver.findElement(By.id("email")).sendKeys(emailId);
}
@Then("user enters the password as (.*)")
public void enterPassword(String password) {
driver.findElement(By.id("pass")).sendKeys("abc@123");
}
@Then("user clicks on login button")
public void click_login_button() {
driver.findElement(By.id("u_0_2")).click();
}
@Then("user logged in successfully")
public void verifyLoginIsSuccessful() {
System.out.println("Logged in successful");
}
@And("user closes the browser")
public void closeBrowser() {
driver.quit();
}
}
Now let us re-run the test runner class and check the step definition methods
It is essential to understand the impact of Selenium with Cucumber on test automation strategies.
Learning how to implement Selenium with Cucumber can significantly enhance your automation skills.
Run cucumber test runner class once again
Before running the cucumber test runner once again, in the test runner we need to add the path of the step definition class.
1. Open TestRunner_FacebookHomepage class
With the rise of automation, the integration of Selenium with Cucumber has become vital.
2. Add the package name of the step definition class in the glue keyword.
By following the steps for Selenium with Cucumber, you ensure successful execution.
6. How does Selenium with Cucumber improve testing?
Execution of tests with Selenium with Cucumber is more efficient with proper setup.
By learning to integrate Selenium with Cucumber effectively, testers can streamline their workflows.
@CucumberOptions annotation
@CucumberOptions(features=”resources/features”, glue=”stepDefinitions”)
In conclusion, the combination of Selenium with Cucumber propels automation to new heights.
The test runner class will look like as below
package testRunners;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features="resources/features", glue="stepDefinitions")
public class TestRunner_FacebookHomepage {
}
3. Now, right-click inside this class and click Run As > JUnit Test
4. Once the test case execution completes the console will display all the statements that you had added in step definition methods.
FAQs about Selenium with Cucumber
1.What is Selenium with Cucumber?
Selenium with Cucumber is a Behavior-Driven Development (BDD) framework that combines Selenium WebDriver for browser automation with Cucumber for writing test scenarios in plain English using Gherkin syntax.
2.What is BDD in software testing?
Behavior-Driven Development (BDD) is a testing approach that encourages collaboration between developers, testers, and business stakeholders by writing test scenarios in a human-readable format.
3.Why use Cucumber with Selenium?
Using Cucumber with Selenium helps create readable test cases, improves collaboration, supports reusable test steps, and simplifies test automation.
4.What is a Feature file in Cucumber?
A Feature file contains application requirements and test scenarios written in Gherkin language. It describes the expected behavior of the application.
5.What are Tags in Cucumber?
Tags are labels used to organize and execute specific groups of test scenarios, such as regression, smoke, or sanity tests.
Conclusion
Finally, reviewing your work in Selenium with Cucumber is essential for success.
BDD (Behavior Driven Development) enables teams to define application behavior using simple, English-like statements written in Gherkin, making test cases easy to understand even for non-technical stakeholders. Integrating Cucumber with Selenium follows a structured approach: first, create feature files using Gherkin to define features and scenarios; next, configure a test runner to connect Cucumber with Selenium; and finally, implement step definitions where the Selenium automation logic is written. This approach improves collaboration, test clarity, and automation efficiency, making it an essential concept for anyone pursuing a professional Selenium course.
In conclusion, Selenium with Cucumber is an invaluable tool for modern software testing.























