Behaviour Driven Development (BDD) Testing using Selenium and Cucumber

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 <username> AND <password> 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

SOAIS - Worksoft Newsletter

To view on your browser, click here
Facebook Twitter LinkedIn
Dear Default Value,
 

Welcome to SOAIS Newsletter of September 2021!

Continuous Testing with Remote Execution
 
The speed of innovation continues to increase, driving rapid and relentless change for today’s ever-evolving IT landscapes, creating greater risk as IT and business teams scramble to ensure timely delivery. How can your organization keep pace? Test more, worry less. With Worksoft’s Connective Automation Platform, you can easily build and maintain automated tests, accelerating testing time without losing scope or volume. You can schedule and execute remote, continuous tests to intercept defects sooner and prioritize remediation - without sacrificing your nights and weekends. Explore how continuous test automation and remote execution can empower your organization.

Click here to connect with us to get more information on our services.
 

Skip Costly Rework with Dynamic Change Resiliency​

Change resiliency is imperative in ever-evolving IT environments. Our patented object action framework streamlines change management by assigning object definitions to your shared assets. The same object may be used in a thousand automation steps, but it can be easily updated by making one simple change to the model definition. The change automatically propagates to every single instance where that object may have been used without a single line of code or manual human involvement. For more change readiness you can also engage our Impact Analysis for SAP to predict how changes in SAP transports will affect your business processes. 

Please click here to watch the video to get a gist.
 

SOAIS Blog – Nuts and bolts of Certify Database Maintenance​

One of the key thing, which is often missed by the organizations, who have invested in using Worksoft Certify for automating their Business Process Validation initiatives, is implementing a Database Maintenance Plan. While the business and the test automation consultants get excited about the shiny new thing that they have got and start building the regression suite; planning and executing a database maintenance plan for most of the customers gets pushed down the priority list. However, since all the test assets in Certify are stored in a Database, a robust database maintenance plan is very important to maintain smooth operation of Certify with acceptable performance criteria. The customers usually start facing issues once they have built significant number of Certify processes which they have started executing on regular basis. Such executions add a lot of data to the tables storing results data and increase the overall size of the Certify database.

Please click here to read the complete blog.
 

Worksoft Blog – Process Intelligence: A Multi-Dimensional Approach

The ability to extract process knowledge has become easier through the years. Technology has evolved to the point where we can deploy capabilities that connect at multiple levels to extract different types of process insight. In the past, organizations were forced to spend enormous energy extracting data manually from different applications and databases. Then, they would have to use things like spreadsheets to transform the data and convert it into meaningful information. 

Please click here and read the complete blog.
 
India
Unit 9, Level 5, Navigator, ITPL,
Bangalore - 560 066.
Phone: +91 80 40071234
US
Suite 101, 1979, N Mill St,
Naperville, IL 60563
Phone 1-800-262-2427
Please click here to Unsubscribe / Unsubscribe Preferences

Leave us your info