In eg. cucumber-ruby, one has the ability to invoke steps from step definitions, as shown here. It would be super cool to have this support in cypress-cucumber-preprocessor as well.
Right now, I've solved it using resolveAndRunStepDefinition, but I realize that this isn't part of the official API.
Feature: some feature
Scenario: some scenario
When within "some section" I fill in "foo" for "bar"
const { When } = require("cypress-cucumber-preprocessor/steps");
const { resolveAndRunStepDefinition } = require("cypress-cucumber-preprocessor/lib/resolveStepDefinition");
When("within {string} {}", (section, subExpression) => {
cy.contains("section", section).within(() => {
resolveAndRunStepDefinition({
keyword: "When",
text: subExpression
});
});
});
When("I fill in {string} for {string}", (text, label) => {
cy.get(`input[label='${label}']`)
.type(text);
});
I have the same requirement. When I select a particular section and next 4-5 steps are related to the selected section only, I need to repat some phrase to identify the section
And calculator is visible
When I click 3 in calculator
And I click + in calculator
And I click 5 in calculator
And I click = in calculator
Then I should see 8 as result in calculator
I need not repeat the phrase if the steps are unique or if they are in scope step definition file. But in more real-life examples, we have multiple features which can't be limited to single feature files. Hence the step definitions need to be moved to the common step definition folder. And if they're in common folder then some phrase is required to make a step unique.
Expected mapping
And calculator is visible
When I click 3
And I click +
And I click 5
And I click =
Then I should see 8 as result
or
And calculator is visible
Within When I click 3
Within And I click +
Within And I click 5
Within And I click =
Within Then I should see 8 as result
@amitguptagwl, none of your example is really feasible and would necessarily require changes to Cucumber itself (this is likely not going to happen).
Normally, steps share no state, but you can create a step definition which alters the world context and have other steps respect this.
Or, you can create custom steps for your own domain
When I click 3 + 5 = in the calculator
@badeball reported to them
Even outside of the examples listed by the posters above, this is a really useful feature. It can be convenient to represent top-level steps that hide a lot of lower-level behaviors, which are also exposed as individual steps. Nested Steps is a great way to do this.
In my case, we don't contain the to-be-called-step in feature line, so I wrote a function using the workaround @badeball provided:
import cucumberStep from 'cypress-cucumber-preprocessor/lib/resolveStepDefinition';
function RunStep(step) {
cy.log("Step: "+step);
cucumberStep.resolveAndRunStepDefinition({
keyword: "*",
text: step
});
}
then call it in step_definition file
import {RunStep} from '../util';
Then(/(.*): user send a chat with text "(.*)"/, (page_name, text) => {
RunStep(page_name +': user clicks on "CHAT_BOX"');
RunStep(page_name +': user input "' + text + '" in "CHAT_BOX_INPUT"');
RunStep(page_name +': user clicks on "SENT_BUTTON"');
});
it worked perfectly, thanks to this workaround.
Most helpful comment
In my case, we don't contain the to-be-called-step in feature line, so I wrote a function using the workaround @badeball provided:
then call it in step_definition file
it worked perfectly, thanks to this workaround.