All IT Courses 50% Off
Selenium Tutorials

Selenium with Cucumber (BDD Framework)

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.

  1. Cucumber BDD is an open-source tool.
  2. Cucumber Framework supports multiple languages where you can write your test scripts in multiple languages such as Java, Python, Ruby, .NET, etc.
  3. 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:

  1. Software Engineer and Business Analyst.
  2. Manual Tester and Developers.
  3. 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/

Selenium with Cucumber (BDD Framework)

2. In the Search text box, search for the particular jar ‘cucumber-core’ as shown in the below screenshot.

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

It will redirect to a page that shows a different version of this jar file.

Selenium with Cucumber (BDD Framework)
  1. To open click on the latest version. You will see the page as shown below
Selenium with Cucumber (BDD Framework)

5. Click on the Cucumber Core jar to download the jar file to save the jar file on your machine.

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

Download Selenium Webdriver Jar files

  1. Selenium-server-standalone jars can be downloaded from https://www.selenium.dev/downloads/
Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

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

Selenium with Cucumber (BDD Framework)

Step 4: Eclipse IDE would now look like below. It will display your newly created project.

Selenium with Cucumber (BDD Framework)

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

Selenium with Cucumber (BDD Framework)
  1. 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

Selenium with Cucumber (BDD Framework)

Add all jars under the lib folder

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

2. Now enter the folder name as ‘resources’ and click on the ‘Finish’ button

Selenium with Cucumber (BDD Framework)

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

Selenium with Cucumber (BDD Framework)

4. Enter the package name as features and click on the Finish button. Inside this package, you will add cucumber feature files

Selenium with Cucumber (BDD Framework)
  1. Right-click on features package and select New > Click on Other, to add cucumber feature file to this package.
Selenium with Cucumber (BDD Framework)

6. Expand the General folder and then select the File option and click on the Next button

Selenium with Cucumber (BDD Framework)

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

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

You will see the Eclipse Marketplace window as shown below

Selenium with Cucumber (BDD Framework)

2. Click on the Install button and Eclipse will start loading the files.

Selenium with Cucumber (BDD Framework)

Click on the Confirm button. The Eclipse will now start loading the components. Once finished, you will see the License agreement screen

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

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

Selenium with Cucumber (BDD Framework)

2. Enter the package name as testRunners and click on the Finish button

Selenium with Cucumber (BDD Framework)

3. The created package will be added to the project. Right-click on the package and click New and click Class

Selenium with Cucumber (BDD Framework)

4. Give class name as TestRunner_FacebookHomepage and click on the Finish button

Selenium with Cucumber (BDD Framework)

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.

Selenium with Cucumber (BDD Framework)

How to create Cucumber Step Definition class

Here we create a new package and add a new step definition class.

  1. Right-click on the src folder and click New > Package
Selenium with Cucumber (BDD Framework)
  1. Enter package name as stepDefinitions and click on the Finish button
Selenium with Cucumber (BDD Framework)
  1. Right-click on the created package and click New > Class
Selenium with Cucumber (BDD Framework)

4. Enter the class name as StepDefs_FacebookHomepage and click on the Finish button

Selenium with Cucumber (BDD Framework)

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

  1. Using Gherkin language we can create a feature file in which define the feature and scenarios step by step.
  2. Create Testrunner file and we integrate this file Cucumber with selenium.
  3. Create Step definition, and the selenium script is defined under this package.
Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close

Get Selenium Training Online
Course Now at 50%OFF!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys will use this information to be in touch with you for providing updates.