End to End (E2E) Testing in Cucumber
By Mohit Joshi, Community Contributor - December 21, 2022
In software development, companies try to achieve the best possible automated testing at scale. However, since the development doesn’t initially start with a complex state, E2E testing is often ignored. The later stages of development require E2E testing. And hence, end to end testing should be automated to achieve the best results. This article explores how to use BDD test automation to perform E2E testing with Cucumber.
What is E2E testing?
E2E testing stands for end to end testing, is a method to ensure that the application is working as expected. The main purpose behind the implementation of E2E testing is to test from the real user’s perspective. Software systems are made of complex and interconnected subsystems. Therefore, E2E testing prevents the entire system from breakdown because of the failure of one subsystem.
E2E testing is now widely adopted in several technologies due to numerous benefits such as less expensive, bug detection, the correctness of the application, and many more. E2E testing helps in achieving greater success for the application by testing it under real-world conditions. Moreover, it would let QAs know how a failing test would impact the user.
What is Cucumber Framework?
Cucumber Framework is a testing tool that supports BDD Testing (Behavior Driven Development). It allows the user to write tests in the Gherkin language (easy descriptive English language), which anyone can understand. This helps in bringing more collaborators to one project, thus improving the quality of the work. Cucumber framework uses Ruby programming language, however for development and supports several other languages such as JavaScript, Java, C#, and more.
Introduction to BDD (Behavior Driven Development)
BDD (Behavior Driven Development) is a technique where a test is written in an easy language, plain English, which anyone could understand without any technical knowledge. This technique has many benefits while developing and testing.
Some features of the BDD technique are as follows:
- It encourages higher collaboration, as people without any technical knowledge could also be useful in writing tests. This helps in filling the gap between the developer, business analyst, and other stakeholders of the project.
- Gherkin language uses plain English, which helps in increasing the readability of the tests, thus, ultimately increasing the quality of writing tests.
Read More: How to achieve Advanced BDD Test Automation
Why use Selenium for End-to-End Testing?
Selenium is an open-source tool that is useful in automating tests in web browsers. It supports several programming languages such as C#, Java, JavaScript, NodeJs, PHP, and many more.
A few benefits of incorporating Selenium in our testing are:
- Selenium automation test could be implemented across multiple operating systems and programming languages.
- It gives a user-friendly interface that helps in faster development, customization, and manipulation at an advanced level.
- It has strong community support, which makes it a better choice.
Implementation of Cucumber End to End Testing
Now, let’s understand Cucumber E2E testing with the help of a practical example. We are going to perform a test, where the browser will first navigate to a URL, validate something there, and exits the browser. This simple test will help us with the basic concepts of E2E testing with Cucumber.
Run Cucumber E2E Tests on Real Devices
Step 1: Install IDE and Set up Java
- Install Eclipse IDE, however, you can use any IDE of your choice.
- Install Java. To ensure that Java is installed on your system, run a quick command in the terminal to check the Java version.
Java -version
Step 2: Install Selenium and Cucumber
Step 3: Create a new Maven Project
Install Maven Plugin. However, in the latest versions of Eclipse, it comes installed already.
Step 4: Add Maven dependencies and Selenium to your project
In this step, you have to add several dependencies. Navigate to the pom.xml file in the folder structure and add the dependencies mentioned below. These dependencies are available in the Maven Repository.
Step 5: Create a feature file
A feature file is a file where you will describe your tests in a Gherkin language (Like English). Navigate to src/test/resources in the folder structure and then create a feature folder. Also, add a feature file in the feature folder there.
Feature: Open an application Scenario: Open Selenium application Given user has opened the browser When I open BrowserStack demo website Then Login button should exist
Step 6: Install the Cucumber Eclipse Plugin and a web driver
Install the Cucumber Eclipse Plugin from the Eclipse marketplace. The Cucumber plugin lets the codebase know you’re creating a Cucumber project. Moreover, Install Browser Driver to automate your Selenium tests in a browser of your choice. In this example, we are going to use the FirefoxDriver.
Step 7: Create Step Definitions
Step Definition is a java method in a class containing Annotations. An annotation followed by the pattern is used to link the step Definition to all the steps created in the feature file.
To create Step definitions, navigate to the src/test/java package and then add a step definition folder and file there.
package CucumberJava; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import cucumber.annotation.en.Given; import cucumber.annotation.en.Then; import cucumber.annotation.en.When; public class cucumberJava { WebDriver driver = null; // Opening the browser @Given("user has opened the browser") public void openBrowser() { driver = new FirefoxDriver(); } // Launching the bstackdemo website for testing @When("I open BrowserStack demo website") public void goTobstackdemo() { driver.navigate().to("https://bstackdemo.com/signin"); } //Verifying if the page contains the active login button @Then("Login button should exist") public void loginButton() { if(driver.findElement(By.id("login-btn")).isEnabled()) { System.out.println("Test Pass"); } else { System.out.println("Test Fail"); } driver.close(); } }
Step 8: Create a Runner Class
Create a class file in the Step Definitions folder and add the runner class script. The runner class will look something like this.
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/test/java/stepDefinition"} ) public class TestRunner { }
Summing it up
Businesses are spending huge capital and effort in E2E testing to improve user engagement and customer satisfaction. To ensure seamless user flows in large and complex applications, it is essential to perform E2E testing in real user conditions to prevent disruption in user experience due to any defect.
E2E testing in Cucumber ensures better collaboration, even from the business side of the teams, who can work with developers and QAs to ensure all the user flows are well tested using Cucumber E2E Testing.
However, to leverage the maximum benefit of the Cucumber end to end testing, it is recommended to test on real device cloud like that of BrowserStack, so as to take real user conditions into account while testing. This makes identifying bottlenecks in the user flow easy, helping deliver a seamless experience.