Thursday, 3 May 2018

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

No comments:

Post a Comment