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
