In this article, we will learn how to integrate Cucumber with Selenium WebDriver.
What is BDD?
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.
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 Facebook
WHEN User navigate to Facebook page
AND Enter correct username and password
THEN Verify homepage should be properly.
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.
Why use Cucumber with Selenium?
Cucumber and Selenium are the two popular technologies in the current market. Now a day’s for functional testing most organizations are using Selenium. And, all these organizations are preferring to integrate Cucumber with selenium as Cucumber is easy to read and to understand the application flow. Cucumber acts as a bridge between:
- 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
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.
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, to add 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
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
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
- Right-click on the created package and click New > Class
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
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
2. Add the package name of the step definition class in the glue keyword.
@CucumberOptions annotation
@CucumberOptions(features=”resources/features”, glue=”stepDefinitions”)
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.
Conclusion.
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
Cucumber can be integrated with Selenium by using the below 3 steps
- Using Gherkin language we can create a feature file in which define the feature and scenarios step by step.
- Create Testrunner file and we integrate this file Cucumber with selenium.
- Create Step definition, and the selenium script is defined under this package.
One Response