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

Thursday, 3 May 2018

Gherkin: Match range of characters

‘-‘ is used to specify the range of characters.

For Example
[a-f,A-F,0-9]{4}
Above regular expression is used to check for 32bit hexa decimal strings.

([1-9][0-9]*)
Above regular expression match a positive integer.
@When("I purchased for \\$([1-9][0-9]*)")
public void i_purchased_for_$(int arg1) throws Exception {
 System.out.println("Purchased for " + arg1 + "$");
}



Previous                                                 Next                                                 Home

How to view packages as folder structure in eclipse?

Eclipse project explorer displays the project structure by grouping the packages like below.


If you want to see the package structure like the folder structure in operating system, you can do it by using Navigator window.

How to open Navigator window?
Window -> Show View -> navigator

Project structure will be displayed like below in navigator window.
You may like

Previous                                                    Next                                                    Home

Gherkins: *: Match zero or more occurrences

By using ‘*’ modifier, you can match zero (or) more occurrences.

Ex
(.*) : Matches zero or more characters

Example 1
When I provided user name "Krishna"

Above step can be modeled like below.

@When("I provided user name \"(.*)\"")

Example 2
And a  password "pwd12345"

Above step can be modeled like below.

@And("a  password \"(.*)\"")

Find the below working application.

UserRegistration.feature
Feature: Registering new users to the Application 

Scenario: Registration Happy scenarion 

 Given As a non-registered user 
 When I provided user name "Krishna" 
 And a  password "pwd12345" 
 Then I should be registered successfully

UserRegistrationTest.java
package com.sample.test;

import cucumber.api.java.en.*;

public class UserRegistrationTest {

 @Given("As a non-registered user")
 public void as_a_non_registered_user() throws Exception {
  System.out.println("Non registred user");
 }

 @When("I provided user name \"(.*)\"")
 public void i_provided_user_name(String arg1) throws Exception {
  System.out.println("User Name : " + arg1);
 }

 @And("a  password \"(.*)\"")
 public void a_password(String arg1) throws Exception {
  System.out.println("password " + arg1);
 }

 @Then("I should be registered successfully")
 public void i_should_be_registered_successfully() throws Exception {
  System.out.println("Registered successfully");
 }

}


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 TestRun.java application, you can able to see below output in console.
Feature: Registering new users to the Application

  Scenario: Registration Happy scenarion     # src/test/java/com/sample/features/UserRegistration.feature:3
Non registred user
    Given As a non-registered user           # UserRegistrationTest.as_a_non_registered_user()
User Name : Krishna
    When I provided user name "Krishna"      # UserRegistrationTest.i_provided_user_name(String)
password pwd12345
    And a  password "pwd12345"               # UserRegistrationTest.a_password(String)
Registered successfully
    Then I should be registered successfully # UserRegistrationTest.i_should_be_registered_successfully()

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.250s


Previous                                                 Next                                                 Home

Wednesday, 2 May 2018

Gherkin: . : Match any character

‘.’ Is used to match any single character.

For example,
And a  password "pwd12345" of exactly Eight characters

We can model above step like below. Since one ‘.’ Represents single character, we can use Eight ‘.’s to map ‘pwd12345’.

@And("^a  password \"(........)\" of exactly Eight characters$")
public void a_password_of_exactly_characters(String arg1) throws Exception {
                 
}

Find the below working application.

UserRegistration.feature
Feature: Registering new users to the Application 

Scenario: Registration Happy scenarion 

 Given As a non-registered user 
 When I provided user name "Krishna" 
 And a  password "pwd12345" of exactly Eight characters 
 Then I should be registered successfully

UserRegistrationTest.java
package com.sample.test;

import cucumber.api.java.en.*;

public class UserRegistrationTest {

 @Given("As a non-registered user")
 public void as_a_non_registered_user() throws Exception {
  System.out.println("Non registred user");
 }

 @When("^I provided user name \"([^\"]*)\"$")
 public void i_provided_user_name(String arg1) throws Exception {
  System.out.println("User Name : " + arg1);
 }

 @And("^a  password \"(........)\" of exactly Eight characters$")
 public void a_password_of_exactly_characters(String arg1) throws Exception {
  System.out.println("password " + arg1);
 }

 @Then("^I should be registered successfully$")
 public void i_should_be_registered_successfully() throws Exception {
  System.out.println("Registered successfully");
 }

}


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 run TestRun application, you will see below kind of output.
Feature: Registering new users to the Application

  Scenario: Registration Happy scenarion                   # src/test/java/com/sample/features/UserRegistration.feature:3
Non registred user
    Given As a non-registered user                         # UserRegistrationTest.as_a_non_registered_user()
User Name : Krishna
    When I provided user name "Krishna"                    # UserRegistrationTest.i_provided_user_name(String)
password pwd12345
    And a  password "pwd12345" of exactly Eight characters # UserRegistrationTest.a_password_of_exactly_characters(String)
Registered successfully
    Then I should be registered successfully               # UserRegistrationTest.i_should_be_registered_successfully()

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.281s






Previous                                                 Next                                                 Home

Tuesday, 1 May 2018

Gherkin: Grouping the steps using | (pipe symbol)

Let’s say, I have a scenario like below.

Scenario: Give 10% percent discount on 100$ purchase 

 Given As a customer of the company 
 When I purchased for $100 
 Then I should get 10 percentage discount 

I written step definitions like below.

 @Given("As a customer of the company")
 public void as_a_customer_of_the_company() throws Exception {
  System.out.println("I am the customer of the company");
 }

 @When("I purchased for \\$(100)")
 public void i_purchased_for_$(int arg1) throws Exception {
  System.out.println("Purchased for " + arg1 + "$");
 }

 @Then("I should get (10) percentage discount")
 public void i_should_get_percentage_discount(int arg1) throws Exception {
  System.out.println("I got " + arg1 + " percentage of discount");
 }


Now the company decides to give 12% discount to the users who purchased for 200$.

Scenario: Give 12% percent discount on 200$ purchase 

 Given As a customer of the company 
 When I purchased for $200 
 Then I should get 12 percentage discount

When, Then steps are coded like below.

 @When("I purchased for \\$(200)")
 public void i_purchased_for_$(int arg1) throws Exception {
  System.out.println("Purchased for " + arg1 + "$");
 }

 @Then("I should get (12) percentage discount")
 public void i_should_get_percentage_discount(int arg1) throws Exception {
  System.out.println("I got " + arg1 + " percentage of discount");
 }


As you closely observe, secnario1 and scenario2 are similar, only difference is in numbers (In first scenario purchase is for $100, in second scenario purchase is for $200, same in the case of discount also)

In these cases we can group the things using pipe (|) symbol.

Example
@When("I purchased for \\$(100|200)")
public void i_purchased_for_$(int arg1) throws Exception {
         System.out.println("Purchased for " + arg1 + "$");
}

Above step definition match the step either with $100 (or) $200.

PurchaseOrder.feature

Feature: Give special discount to the users 

Scenario: Give 10% percent discount on 100$ purchase 

 Given As a customer of the company 
 When I purchased for $100 
 Then I should get 10 percentage discount 
 
Scenario: Give 12% percent discount on 200$ purchase 

 Given As a customer of the company 
 When I purchased for $200 
 Then I should get 12 percentage discount

PurchaseOrderTest.java

package com.sample.test;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class PurchaseOrderTest {

 @Given("As a customer of the company")
 public void as_a_customer_of_the_company() throws Exception {
  System.out.println("I am the customer of the company");
 }

 @When("I purchased for \\$(100|200)")
 public void i_purchased_for_$(int arg1) throws Exception {
  System.out.println("Purchased for " + arg1 + "$");
 }

 @Then("I should get (10|12) percentage discount")
 public void i_should_get_percentage_discount(int arg1) throws Exception {
  System.out.println("I got " + arg1 + " percentage of discount");
 }
}


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 TestRun application, you can see below output in the console.

Feature: Give special discount to the users

  Scenario: Give 10% percent discount on 100$ purchase # src/test/java/com/sample/features/PurchaseOrder.feature:3
I am the customer of the company
    Given As a customer of the company                 # PurchaseOrderTest.as_a_customer_of_the_company()
Purchased for 100$
    When I purchased for $100                          # PurchaseOrderTest.i_purchased_for_$(int)
I got 10 percentage of discount
    Then I should get 10 percentage discount           # PurchaseOrderTest.i_should_get_percentage_discount(int)

  Scenario: Give 12% percent discount on 200$ purchase # src/test/java/com/sample/features/PurchaseOrder.feature:9
I am the customer of the company
    Given As a customer of the company                 # PurchaseOrderTest.as_a_customer_of_the_company()
Purchased for 200$
    When I purchased for $200                          # PurchaseOrderTest.i_purchased_for_$(int)
I got 12 percentage of discount
    Then I should get 12 percentage discount           # PurchaseOrderTest.i_should_get_percentage_discount(int)

2 Scenarios (2 passed)
6 Steps (6 passed)
0m0.298s




Previous                                                 Next                                                 Home