How to Perform DataDriven Testing In Cucumber?

Reading Time: 4 minutes

Hello People, As we know Most commercial automated software tools on the market support some sort of Data-Driven Testing. It allows us to automatically run a test case multiple times with different input and validation values. As Cucumber is more an automated testing framework than a ready-to-use tool. It takes extra effort to support data-driven testing to automate tests.

The good part is that the Cucumber inherently supports Data Driven Testing using Scenario Outline. There are different ways to use the data insertion within and outside the Cucumber with external files. Let’s understand how we can create a Data-Driven Testing Framework for automating a web application using Selenium WebDriver

What is Cucumber data-driven testing?

Data-Driven Testing with Cucumber is a methodology where a sequence of steps in the test script is run repeatedly against different input values fetched from the corresponding data source. It is one of the widely-used automation testing best practices for verifying the behavior and efficiency of tests when handling various types of input values.

Here are the popular external data feed or data sources in data-driven testing:

  • MS Excel Sheets (.xls, .xlsx)
  • CSV Files (.csv)
  • XML Files (.xml)
  • MS Access Tables (.mdb)

The data feed or data source not only contains the input values used for Selenium automation testing but can also be used for storing the expected test result and the output test result. This can be useful in comparing the test execution result and keeping the same for referring to later stages.

Advantages of Data-Driven Testing

Some of the significant benefits of Data-Driven Testing are:

  • Data-Driven Testing accelerates the process of efficiently performing regression testing on the features of a web product. Regression tests can verify end-to-end workflow logic using different values stored in external data sources.
  • Data-Driven Tests are easier to maintain since the test logic is logically separated from the data of testing the logic.
  • These Tests are helpful for record-keeping since you can store the test execution status along with the input values against which the test automation was run.
  • Data-Driven Testing is a preferred choice for iteratively testing the application (or web product) against a large data set.
  • The data set can contain input values covering positive and negative test scenarios, thereby helping achieve improved test efficiency and coverage.

Now that this blog covers the basics of data-driven testing let’s have a look at the Data-Driven Framework with Cucumber.

Data-Driven Framework in Selenium WebDriver

  • Data-Driven Testing Framework in Selenium is one of the widely-preferred automation testing frameworks that let you realize iterative development and testing.
  • For driving test cases and/or test suites from external data feeds using Data Driven Framework which is in line with data-driven testing principles.
  • The test script connects to the external data source (or feed) to get the required operations on the test data.
  • the test data set separates from the test implementation, reducing the overall effort of maintaining and updating the test code using the Data-Driven Framework.
  • Minimal changes in the business rules will require changes in the test data set, with/without minimal (or no) changes in the test code.
  • Selenium WebDriver lets you perform automated cross-browser testing on web applications.
  • It does not have the support to perform create, read, update, and delete (CRUD) operations on external data feeds like Excel sheets, CSV files, and more.
  • It uses third-party APIs like Apache POI and lets you access and performs relevant operations on external data sources.

What is Apache POI?

Apache Software Foundation develops and distributes open-source libraries i.e Apache POI (Poor Obfuscation Implementation) is a which by the. It is the most commonly used API for realizing data-driven tests in Selenium. The API provided by the library files in Apache POI lets the user manipulate Microsoft Documents – Excel files (*.xls, *.xlsx), Doc files (*.doc), and more. In a nutshell, Apache POI is the Java Excel solution that lets you read/write/modify data from/to external data feeds.

Data-Driven Testing in Cucumber

Cucumber supports Data Driven Testing using Scenario Outline and Examples keywords. Creating a feature file with Scenario Outline and Example keywords helps to reduce the code and test multiple scenarios with different values.

Pre-Requisite

  1. Cucumber – 6.10.4
  2. Java – 11
  3. Selenium – 3.141.59
  4. Junit – 4.13.2 ( You can use TestNG also)
  5. Cucumber JUnit – 6.10.4 (If using TestNG, then replace this with Cucumber TestNG)

In case, the project uses Maven, we need to add the below dependencies to the project.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
        
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 

http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
<modelVersion>4.0.0</modelVersion>
 
    
<groupId>org.example</groupId>
    
<artifactId>CucumberDemo</artifactId>
    
<version>1.0-SNAPSHOT</version>
 
    <properties>
        
<cucumber.version>6.10.4</cucumber.version>
        
<selenium.version>3.141.59</selenium.version>
        
<junit.version>4.13.2</junit.version>
        
<maven.compiler.source.version>11</maven.compiler.source.version>
        
<maven.compiler.target.version>11</maven.compiler.target.version>
    
</properties>
 
 
    <dependencies>
       
 <dependency>
          
  <groupId>io.cucumber</groupId>
           
 <artifactId>cucumber-java</artifactId>
           
 <version>${cucumber.version}</version>
        
</dependency>
 
       
 <dependency>
            
<groupId>org.seleniumhq.selenium</groupId>
            
<artifactId>selenium-java</artifactId>
           
 <version>${selenium.version}</version>
        
</dependency>
 
        <dependency>
           
 <groupId>junit</groupId>
            
<artifactId>junit</artifactId>
            
<version>${junit.version}</version>
            
<scope>test</scope>
        
</dependency>
 
       
 <dependency>
            
<groupId>io.cucumber</groupId>
            
<artifactId>cucumber-junit</artifactId>
            
<version>${cucumber.version}</version>
           
 <scope>test</scope>
        
</dependency>
 
        <dependency>
           
 <groupId>junit</groupId>
            
<artifactId>junit</artifactId>
            
<version>${junit.version}</version>
           
 <scope>test</scope>
        
</dependency>
 
    </dependencies>
 
    <build>
        <plugins>
 

            <plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-compiler-plugin</artifactId>
               
 <version>3.8.0</version>
                
<configuration>
                    
<source>${maven.compiler.source.version}</source>
                    
<target>${maven.compiler.target.version}</target>
                   
 <encoding>UTF-8</encoding>
               
 </configuration>
            
</plugin>
        
</plugins>
    
</build>

Scenario Outline

This keyword lets you run the same scenario for two or more different input data. It basically replaces the value assigned in the variable from the input values mentioned in the Examples input data set.

For better understanding, we can consider an example of the login page. Scenario Outline: Test user Login with different credentials

Scenario Outline: Test user Login with different credentials
	
Given Open Firefox and navigate to Login page
	
When valid "<username>" and "<password>" is entered
	
Then User should be logged in successfully

Examples:

    | username   | password |

    | rahul@gmail.com | Test@123 |

    | xyz@gmail.com| Testtest |

After creating the feature file, create a Test Runner class and steps to run a class for further execution.

package Login_cucumber;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = {"Cases"},glue={"StepsToTest"})

public class TestRunner {

}

After running the class, create a class for steps to run the actual test.

public class LoginSteps {
    
    WebDriver driver;    
 
    @Given("^Open Firefox and navigate to Login page$")

    public void given_Open_Firefox_and_navigate_to_Login_page() throws 

Throwable
    {
       driver = new FirefoxDriver();

       driver.get("http://LoginPage.com");

       driver.manage().window().maximize();")

    }
      
    @When("^valid \"(.*)\" and \"(.*)\ is entered$")

    public void when_valid_username_and_password_is_entered(String 

username,String password) throws Throwable
    
{

        driver.findElement(By.xpath(".//*[@id='login']")).sendKeys(username);

        driver.findElement(By.xpath(".//[@id='password']")).sendKeys(password);
    
}
      
    @Then("^user should logged in successfully$") throws Throwable

    public void then_user_should_logged_in_successfully()
    
{
    
driver.findElement(By.xpath(".//*[@id='submit']")).click();
    
System.out.println("User Logged in successfully");
    
}

}


On running this class, the login credentials i.e username and password will get filled from the feature file with new values each time till the Examples has values; in the current example, the test will run two times as it has two test data.

For more Reference Visit:

https://cucumber.io/docs

Leave a Reply