liveBook · Manning

Parameter type

As explained in BDD in Action, Second Edition

A custom parameter type is a user-defined Cucumber parameter type that recognizes text in a step and converts it into a specified object or value.

A custom parameter type lets a step definition accept meaningful, domain-specific text and receive a converted value or object. A regular expression identifies the text to match; a parameter method transforms it. This can make step expressions clearer and the conversions reusable. section 8.4.3

Listing 8.7 A parameter type definition
@ParameterType("(Standard|Silver|Bronze|Gold) Frequent Flyer member")      #1
public FrequentFlyerMember frequentFlyer(String statusName) {            
    FrequentFlyerStatus status = FrequentFlyerStatus.valueOf(statusName);  #2
    return FrequentFlyerMember.withStatus(status);                         #3
}

This definition matches frequent-flyer member descriptions and converts the status into a FrequentFlyerMember object. section 8.4.3

A parameter type can also match a specific date format and convert it to a LocalDate. In Cucumber.js, custom types can resolve actor names or pronouns into Screenplay actors. section 8.4.3 section 14.3

import { defineParameterType } from '@cucumber/cucumber'
import { actorCalled, actorInTheSpotlight } from '@serenity-js/core'
 
defineParameterType({
    name: 'actor',
    regexp: /[A-Z][a-z]+/,
    transformer: (name: string) => actorCalled(name)
})
defineParameterType({
    name: 'pronoun',
    regexp: /he|she|they|his|her|their/,
    transformer: () => actorInTheSpotlight()    
})

These Cucumber.js definitions convert actor names and pronouns into Serenity/JS actor objects. section 14.3

How BDD in Action, Second Edition covers it

How deep it goes
Explains regular-expression matching, permitted values, method naming, and conversion into domain objects.
Examples
Uses frequent-flyer statuses, ISO-formatted dates, actor names, and pronouns as converted step parameters.
What you can do after
Apply Cucumber parameter types to convert matched step text into frequent-flyer objects, dates, or Screenplay actors.
How it connects
Connects Cucumber Expressions and step definitions with regular expressions, domain objects, and the Screenplay Pattern.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.