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

No comments:

Post a Comment