Friday 4 May 2018

Gherkin: Data Tables

By using data tables, we can represent the data easily while writing scenarios.

Let me explain with an example.

Suppose in your organization, you had automated vending robot that deliver coffee, Tea, Coke, Juice to the employees in the early morning and wish them.

How are you going to model above scenario?
Without data tables, we can model above scenario like below.

VendingMachineDelivery.feature
Feature: Vending robot should deliver and wish Employees
 Vending machine should deliver coffee, Chocklet, Coke, Juice to the employees
 and wish them.
 
 Scenario: Delivering and wising employees
  
  Given a vending robot
  When time is 9AM
  Then to the employee with preference "Coffee", deliver "Coffee" to employee and wish him with message "A Special Coffee for you"
  And to the employee with preference "Coke", deliver "Coke" to employee and wish him with message "Enjoy your coke"
  And to the employee with preference "Juice", deliver "Juice" to employee and wish him with message "Morning Healthy Juice"
  And to the employee with preference "Chocklet", deliver "Chocklet" to employee and wish him with message "Good Morning"

Cucumber generates below skelton code.

@Given("^a vending robot$")
public void a_vending_robot() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^time is (\\d+)AM$")
public void time_is_AM(int arg1) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^to the employee with preference \"([^\"]*)\", deliver \"([^\"]*)\" to employee and wish him with message \"([^\"]*)\"$")
public void to_the_employee_with_preference_deliver_to_employee_and_wish_him_with_message(String arg1, String arg2, String arg3) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}


Observation
Scenario: Delivering and wising employees
                 
                  Given a vending robot
                  When time is 9AM
                  Then to the employee with preference "Coffee", deliver "Coffee" to employee and wish him with message "A Special Coffee for you"
                  And to the employee with preference "Coke", deliver "Coke" to employee and wish him with message "Enjoy your coke"
                  And to the employee with preference "Juice", deliver "Juice" to employee and wish him with message "Morning Healthy Juice"
                  And to the employee with preference "Chocklet", deliver "Chocklet" to employee and wish him with message "Good Morning"
        

As you observe the scenario, Then, And steps are similar and there is so much repetitive stuff. We can avoid this repetitive stuff using data tables.

We can model above scenario using data tables like below.

VendingMachineDelivery.feature

Feature: Vending robot should deliver and wish Employees
 Vending machine should deliver coffee, Chocklet, Coke, Juice to the employees
 and wish them.
 
 Scenario: Delivering and wising employees
  
  Given a vending robot
  When time is 9AM
  Then delivier to the employee based on their preferences.
   |Employee Preference    |Deliver    |Wish Message               | 
   |Coffee                 |Coffee     |A Special Coffee for you   |
   |Coke                   |Coke       |Enjoy your coke            |
   |Juice                  |Juice      |Morning Healthy Juice      |
   |Chocklet               |Chocklet   |Good Morning               |

Cucumber generates below skelton.

@Given("^a vending robot$")
public void a_vending_robot() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^time is (\\d+)AM$")
public void time_is_AM(int arg1) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^delivier to the employee based on their preferences\\.$")
public void delivier_to_the_employee_based_on_their_preferences(DataTable arg1) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    // For automatic transformation, change DataTable to one of
    // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
    // E,K,V must be a scalar (String, Integer, Date, enum etc)
    throw new PendingException();
}


Let’s print DataTable and see the result.


VendingMachineTest.java

package com.sample.test;

import java.util.List;

import cucumber.api.DataTable;
import cucumber.api.java.en.*;

public class VendingMachineTest {
 @Given("^a vending robot$")
 public void a_vending_robot() throws Exception {
  System.out.println("Vending Robot initialized");
 }

 @When("^time is (\\d+)AM$")
 public void time_is_AM(int arg1) throws Exception {
  System.out.println("Time is set to " + arg1);
 }

 @Then("^delivier to the employee based on their preferences\\.$")
 public void delivier_to_the_employee_based_on_their_preferences(DataTable arg1) throws Exception {
  List<List<String>> input = arg1.asLists(String.class);
  for(List<String> row : input){
   for(String column : row){
    System.out.print(column + ",");
   }
   System.out.println();
  }
 }


}


TestRun.java

package com.sample.main;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, features = "src/test/java/com/sample/features", glue = "com.sample.test", plugin = {
  "pretty" })
public class TestRun {

}


When you ran above application, you can able to see below output.

Feature: Vending robot should deliver and wish Employees
 Vending machine should deliver coffee, Chocklet, Coke, Juice to the employees
 and wish them.

  Scenario: Delivering and wising employees                   # src/test/java/com/sample/features/VendingMachineDelivery.feature:5
Vending Robot initialized
    Given a vending robot                                     # VendingMachineTest.a_vending_robot()
Time is set to 9
    When time is 9AM                                          # VendingMachineTest.time_is_AM(int)
Employee Preference,Deliver,Wish Message,
Coffee,Coffee,A Special Coffee for you,
Coke,Coke,Enjoy your coke,
Juice,Juice,Morning Healthy Juice,
Chocklet,Chocklet,Good Morning,
    Then delivier to the employee based on their preferences. # VendingMachineTest.delivier_to_the_employee_based_on_their_preferences(DataTable)

1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.253s

As you observe the output, data table is printed liked below.

Employee Preference,Deliver,Wish Message,
Coffee,Coffee,A Special Coffee for you,
Coke,Coke,Enjoy your coke,
Juice,Juice,Morning Healthy Juice,
Chocklet,Chocklet,Good Morning,



Previous                                                 Next                                                 Home

No comments:

Post a Comment