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

No comments:

Post a Comment