'cucumber-js gherkin: regular expression using custom parameterType
As user of cucumber-js,
given a custom parameter type (eg below),
then I would like to have a feature whose step is defined by a regular expression, but references the custom parameter type.
Example custom parameter type:
defineParameterType({
name: "http-method",
useForSnippets: true,
regexp: /"(PUT|POST|GET|PATCH|DELETE)"/,
transformer: method => method.toLowerCase(),
});
Am I asking too much, or is it possible? I can't find it documented, and can't imagine how it would be implemented.
Solution 1:[1]
- The regex on the step definition needs to match the regex that you have defined on your
defineParameterType
. - The parameter name on the function that implements your step needs to match the name of your
defineParameterType
For example, the following steps
Feature: api request
Scenario: submit request
Given I send a "PUT" request to my endpoint
And I send a "POST" request to my endpoint
And I send a "GET" request to my endpoint
And I send a "PATCH" request to my endpoint
And I send a "DELETE" request to my endpoint
...are implemented by the following step definition
Given(/^I send a "(PUT|POST|GET|PATCH|DELETE)" request to my endpoint$/, async function (httpMethod) {
console.log(httpMethod)
});
...and with the following defineParameterType
(note that the regex declaration here is simpler than the one that you posted on your question)
defineParameterType({
name: "httpMethod",
useForSnippets: true,
regexp: /PUT|POST|GET|PATCH|DELETE/,
transformer: method => method.toLowerCase(),
});
...I get the following output
Feature: api request
Scenario: submit request
Given I send a "PUT" request to my endpoint
put
And I send a "POST" request to my endpoint
post
And I send a "GET" request to my endpoint
get
And I send a "PATCH" request to my endpoint
patch
And I send a "DELETE" request to my endpoint
delete
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ivson Souza |