I have some step definitions that use custom parameters.
const assertEntity = function(name: string, operator: string,
otherName: string) {
console.log(`assertAttrs with ${name} ${operator} ${otherName}`);
};
Then("{name} object is {operator} {otherName}", assertEntity);
And the following feature file (truncated)
Scenario: Compare two similar API key objects
Given we have a new ApiKey called Red
And parameter types defined like this
defineParameterType({
regexp: /name/,
transformer: function(s) {
return s;
},
name: "name"
});
However cucumber says step definition is undefined...
? Given we have a new ApiKey called Red
Undefined. Implement with the following snippet:
Given('we have a new ApiKey called Red', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
I believe the problem is in my regexp but I've seen this done in examples so I'm not sure how to proceed.
I have some step definitions that use custom parameters.
const assertEntity = function(name: string, operator: string,
otherName: string) {
console.log(`assertAttrs with ${name} ${operator} ${otherName}`);
};
Then("{name} object is {operator} {otherName}", assertEntity);
And the following feature file (truncated)
Scenario: Compare two similar API key objects
Given we have a new ApiKey called Red
And parameter types defined like this
defineParameterType({
regexp: /name/,
transformer: function(s) {
return s;
},
name: "name"
});
However cucumber says step definition is undefined...
? Given we have a new ApiKey called Red
Undefined. Implement with the following snippet:
Given('we have a new ApiKey called Red', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
I believe the problem is in my regexp but I've seen this done in examples so I'm not sure how to proceed.
Share Improve this question asked Oct 1, 2018 at 15:25 Alex LoganAlex Logan 1,2413 gold badges18 silver badges27 bronze badges1 Answer
Reset to default 4How the Transformers work
- The regex has to match the parameter
- The cucumber expression has to match the step when converted back to a regex
You can use any variety of transformations. For instance:
Given I am on the "Home" page
Given I am on the "My Basket" page
Could both be matched by the transformer:
defineParameterType({
regexp: /"([^"]*)"/,
transformer(string) {
return urls[string.replace(/ /g, "_").toLowerCase()]
},
name: 'page',
useForSnippets: false
});
The transformation that is happening here is the url is being located in an array of various urls.
The Answer
For your example, the step definition that you have provided wouldn't match the step that you have provided.
But, if we were to go ahead and match this:
Given we have a new ApiKey called "Red"
By using a step definition like this:
Given('we have a new ApiKey called {name}', function(){
return pending
});
We would require a step transformer like this:
defineParameterType({
regexp: /"([^"]*)"/,
transformer: function(s) {
return s;
},
name: "name",
useForSnippets: false
});
Note: "([^"]*)"
is not the be-all end-all of regex matching with Cucumber, but it was a fairly standard regex to be found within the step definitions before cucumber expressions came out with 3.x.x, hence the 2 examples I've used are with them.