...

BDD Testing using Selenium and Cucumber

What is BDD?

BDD is an agile software development process that enables to close the gap between business and developer by:

  • Encouraging collaboration across roles to build shared understanding of the problem to be solved
  • Working in rapid, small iterations to increase feedback and the flow of value
  • Producing system documentation that is automatically checked against the system’s behaviour.

We do this by focusing collaborative work around concrete, real-world examples that illustrate how we want the system to behave. We use those examples to guide us from concept through to implementation, in a process of continuous collaboration. In BDD, “Given-When-Then” is the proposed approach for writing test cases.

Three practices Of BDD

Essentially, day-to-day BDD activity is a three-step, iterative process:

  1. DiscoveryFirst, take a small upcoming change to the system – a User Story – and talk about concrete examples of the new functionality to explore, discover and agree on the details of what’s expected to be done.
  2. FormulationNext, document those examples in a way that can be automated, and check for agreement.
  3. AutomationFinally, implement the behaviour described by each documented example, starting with an automated test to guide the development of the code.

What is Cucumber?

Cucumber is an open-source testing framework that supports Behaviour Driven Development for automation testing of web applications. The tests are first written in a simple scenario form that describes the expected behaviour of the system from the user’s perspective.

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

Cucumber and Selenium Testing: A Collaborative Approach

While automated Selenium testing adds accuracy and speed to the development cycle, Cucumber adds an extra edge to it, making it more collaborative for the non-technical management stakeholders. Widely beneficial for User Acceptance Testing where the test scenarios are largely driven by behaviour, Cucumber strengthens Automation Testing.

The Cucumber Framework

Cucumber framework mainly consists of three major parts – Feature File, Step Definitions, and the Test Runner File.

Feature File

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

Cucumber tests are written in these Feature Files that are stored with the extension – “.feature”. A Feature File can be given a description to make the documentation more legible. Keywords such as GIVEN, WHEN, and THEN used to write the test in Cucumber are called Annotations.

Example: The Login function on a website

Description: The user shall be able to login upon entering the correct username and password in the correct fields. The user should be directed to the homepage if the username and password entered are correct.

GIVEN user navigates to login page by opening Firefox

WHEN user enters correct

AND values

THEN user is directed to the homepage

Step Definitions

Now that the features are written in the feature files, 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 mapping data between each step of a scenario defined in the feature file and the code to be executed.

Step Definitions can use both Java and Selenium commands for the Java functions written to map a feature file to the code.

Example:

package StepDefinition;

import cucumber.api.java.en.Given;

import 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 launch the login page of application to define the GIVEN step of the feature

@When(“^user enters correct username and password values$”)

//take inputs for username and password fields using find element by xpath. Put the correct username and password values as inputs to define the WHEN step of the feature

@Then (“^user gets directed to homepage$”)

//Direct to the Homepage of the application as a result of correct username and password inputs in the WHEN step. This would define the THEN step of the feature

Test Runner File

To run the test, we create a Test Runner File, which is a JUnit Test Runner Class containing the Step Definition location and the other primary metadata required to run the test.

The Test Runner File uses the @RunWith() Annotation from JUnit for executing tests. It also uses the @CucumberOptions Annotation to define the location of feature files, step definitions, reporting integrations, etc.

Example:

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

package cucumberTest;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(

features = “src/test/Feature”

,glue={“src/main/stepDefinition”}

)

public class TestRunner {

}

Writing a test in Cucumber for Selenium Automation

Taking forward the scenario of the login feature, let’s create a sample test in Cucumber. This code will run the Login Scenario described in the Feature section and will open the website home page upon entering the right username and password.

package cucumberTest;

import io.cucumber.java.After;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import 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;

import io.cucumber.java.en.When;

public class Steps{

//Opening Firefox Browser and launching the login page of application

private final WebDriver driver = new FirefoxDriver();

@Given(“user navigates to the login page by opening 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()

public void closeBrowser()

{

driver.quit();

}

}

Summary

  • Cucumber testing tool is a purely business-driven development tool written in Ruby
  • The business-driven development approach is an advancement over test-driven development approach, which follows the ‘Given-When-Then’ steps for writing test cases
  • Cucumber provides compatibility with popular software platforms like Selenium, Watir, Ruby, and other popular platforms.

Leave a Reply

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