liveBook · Manning

Custom 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 extends Cucumber expressions with a domain-specific placeholder. It recognizes scenario text with a regular expression and converts the matched value into an object or value that a step definition can use directly. section 8.4.3 section 14.3

Use a custom parameter type to make step definitions more expressive and reusable:

  • Replace an anonymous status string with a FrequentFlyerMember parameter, such as “Silver Frequent Flyer member.” section 8.4.3
  • Match a defined format, such as an ISO date, and convert it to a LocalDate. section 8.4.3
  • Convert an actor name or pronoun into a Screenplay actor for direct use in step code. section 12.10.3 section 14.3

From readable text to a domain object

A step such as “Tara is a Silver Frequent Flyer member with 20000 points” can use {frequentFlyer} and {int}. Cucumber matches “Silver Frequent Flyer member,” invokes the parameter-type method, creates a FrequentFlyerMember, and passes that object to the step definition alongside the points value. 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
}

The definition shows the three essential parts: a name or inferred method name, a matching regular expression, and a transformer that returns the target domain object. section 8.4.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()    
})

The Cucumber.js form defines a parameter name, regular expression, and transformer; actor and pronoun parameters can then map scenario language to Serenity/JS actors. section 14.3

How BDD in Action, Second Edition covers it

How deep it goes
Explains matching, naming, and transformation, with Java and JavaScript implementations; it stops short of broader type-registration mechanics.
Examples
Uses Frequent Flyer members, ISO dates, actor names, pronouns, and traveler sign-up scenarios as concrete conversion examples.
What it assumes
Assumes familiarity with Cucumber expressions and step definitions while teaching parameter matching and transformation through examples.
What you can do after
Builds reusable step parameters that convert scenario language into domain objects, dates, or actors before step code runs.
How it connects
Connects Cucumber expressions, step definitions, regular expressions, domain objects, Screenplay Pattern actors, and transformations.

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.