Test Automation using Selenium and Cucumber Framework: Tutorial
By Garima Tiwari, Community Contributor - February 11, 2023
Web Applications have become essential for businesses seeking advanced ways to connect with and serve their target audience in the digital realm. Given the importance of web apps in business operations these days, irrespective of the industry, they must be comprehensively tested to ensure that they offer high-end user experiences.
Automation Testing accelerates the software development lifecycle. Furthermore, on adopting the Behavior Driven Development (BDD) approach to development, different stakeholders like developers, QAs, and non-tech teams can collaborate actively in the project.
The scenarios are written based on the expected behavior of the software and it is tested to check if it matches said scenarios.
These scenarios are documented using a Domain Specific Language such as Gherkin. In each test scenario, natural language constructs constituting small English-like phrases are used to describe the behavior and expected outcome of an application. This is done using a dedicated software tool like Cucumber, that allows the execution of automated acceptance tests written in Gherkin.
- What is Cucumber in Selenium?
- Cucumber and Selenium Testing: A Collaborative Approach
- The Cucumber Framework: BDD Framework for Selenium
- 1. Feature File
- 2. Step Definitions
- 3. Test Runner File
- Setting up Cucumber BDD Framework for Selenium
- Prerequisites for Cucumber and Selenium Setup
- How to write a test in Cucumber for Selenium Automation?
- Best Practices in Cucumber Testing
What is Cucumber in Selenium?
Cucumber Framework in Selenium is an open-source testing framework that supports Behavior Driven Development for automation testing of web applications. The tests are first written in a simple scenario form that describes the expected behavior 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 non-technical management stakeholders. Widely beneficial for User Acceptance Testing where the test scenarios are largely driven by behavior, Cucumber strengthens Automation Testing.
The Cucumber Framework: BDD Framework for Selenium
Cucumber BDD framework mainly consists of three major parts – Feature File, Step Definitions, and the Test Runner File.
1. 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.
Example:
The Login function on a website
Feature File Name: userLogin.feature
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.
Keywords such as GIVEN, WHEN, and THEN used to write the test in Cucumber are called Annotations.
GIVEN user navigates to login page by opening Firefox WHEN user enters correct <username> AND <password> values THEN user is directed to the homepage
2. 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
3. Test Runner File
To run the test, one needs 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 { }
Setting up Cucumber BDD Framework for Selenium
Before exploring how Cucumber works and how to write a Cucumber Selenium test, let’s first figure out how to set up Cucumber. Installing the prerequisites mentioned below is all a user needs to begin with Cucumber Automation Testing.
Also Read: Overcoming Key Challenges in Test Automation
Prerequisites for Cucumber and Selenium Setup
- Install Java
- Install Eclipse IDE for Java
- Install Maven
- Install Selenium Webdriver
- Install JUnit
- Install Cucumber
How to write 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(); } }
Must Read: How to achieve Advanced BDD Test Automation
Best Practices in Cucumber Testing
Here are some of the best practices in Cucumber Testing:
- The versions of Cucumber-java, Cucumber-junit, and Cucumber-core jars should be the same for seamless connectivity.
- Adding an after hook to the code for capturing screenshots when the test fails can help diagnose the issue and debug it.
- Use Tags for organizing tests based on tag definition. This helps in cases where all tests don’t have to be run every time. Such tests can be marked using tags and run only when required. This saves time and processing capacity of the system and resources.
- As always, it is important to run the Cucumber Selenium tests on real browsers and devices. BrowserStack offers a Cloud Selenium Grid of 3000+ real browsers and devices for testing purposes – both live and automated. Simply sign up, choose the required device-browser-OS combination, and start testing websites for free.
Test Automation is absolutely essential to keep up with the growing demands of faster delivery and optimal quality on testers. Cucumber framework in Selenium allows test scenarios to be written using natural language constructs that describe the expected behavior of the software for a given scenario, making it ideal for user acceptance testing.
Lastly, testing websites on a real device cloud is the only way to ensure complete accuracy of results. So, always opt for real device testing to comprehensively verify website performance, cross browser compatibility, and the quality of user experience it offers.