Monday 30 April 2018

Gherkin: Escaping character (/)

Since Gherkin support regular expressions, characters like $, *, . has special meaning. If your scenario steps has these special characters, then you should use escaping character.

Ex
When I purchased for $100

Above step can be written like below.

@When("I purchased for \\$(100)")

As you observer above snippet, I defined $ like \\$ in java code.

Find the below working application.

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 

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)")
 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");
 }

}


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, you can able to see below kind of output.

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)

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




Previous                                                 Next                                                 Home

No comments:

Post a Comment