Data Driven Testing in Cucumber

What is Cucumber data driven testing?

In very simple terms, cucumber data driven testing means that you can pass data from cucumber feature files to your test cases. For example, let us consider that you are automating the login flow of some application. In this case, you can pass user name and password as test data from your feature file to selenium code.

Data-Driven Testing in Cucumber

1. Parameterization without Example Keyword:
Parameterization without Example Keyword
Using Regular Expression: 
("^user enters \"(.*)\" and \"(.*)\"$")
("^user enters \"([^\"]*)\" and \"([^\"]*)\"$")

2. Data-Driven Testing in Cucumber using Scenario Outline
Parameterization with Example Keyword

Parameterization with Examples Keyword
       Using Regular Expression: 
(“user enters (.*) and (.*)$")

Scenario Outline – This is used to run the same scenario for 2 or more different sets of test data. E.g. In our scenario, if you want to register another user you can data drive the same scenario twice.

Examples – All scenario outlines have to be followed with the Examples section. This contains the data that has to be passed on to the scenario.

Using Scenario Outline
Cucumber inherently supports data driven testing using Scenario Outline. Consider the following feature file using Scenario to define the test steps-

 Feature: Check addition in Google calculator
   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result

   Scenario: Addition
   Given I open google
   When I enter "2+2" in search textbox
   Then I should get result as "4"
In order to make it data driven we just have to use Scenario Outline along with Examples. Write the following code in feature file-

   Feature: Check addition in Google calculator
   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result

   Scenario Outline: Addition
   Given I open google
   When I enter "<calculation>" in search textbox
   Then I should get result as "<result>"
   
   Examples:
| calculation |result|
| 3+3      | 6 |
| 2+5      | 9 |
Please refer below Youtube video for detail explanation:


What is Data table in Cucumber?

Data Tables are handy for passing a list of values to a step definition. Cucumber provides a rich API for manipulating tables from within step definitions.
Data Tables are also used to handle large amount of data. Tables in Cucumber feature files are represented by using the pipeline “|” sign.
Please refer below Youtube video for detail explanation:



Behaviour Driven Development (BDD) Framework Setup

 Steps to be followed to setup BDD Framework:

1. Create a Maven Project

2. Add dependencies - Selenium java, webdrivermanager, cucumber-java, junit, cucumber-junit 

3. Create Features folder in src/test/resources

4. Create a new feature file. extension is .feature

5. Install Cucumber Eclipse plug-in or natural plug-in and Restart the eclipse.

6. Create a new feature file to get all the options

7. Write Scenarios in feature file

8. Run the feature file

9. Create step definition class or glue code under src/test/java package

10. Create a runner package/class

@RunWith(Cucumber.class) 

@CucumberOptions(features="src/test/resources/Features",glue={"com.StepDefinition"}

Please refer below YouTube video:


What is Cucumber Options? 
@CucumberOptions are like property files or settings for your test. Basically @CucumberOptions enables us to do all the things that we could have done if we have used cucumber command line.

Different Cucumber Options:


Please refer below YouTube video: