cypress-cucumber-preprocessor icon indicating copy to clipboard operation
cypress-cucumber-preprocessor copied to clipboard

Support for nested steps

Open badeball opened this issue 7 years ago • 9 comments

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);
});

badeball avatar May 23 '19 13:05 badeball

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 avatar Sep 25 '19 01:09 amitguptagwl

@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 avatar Sep 25 '19 07:09 badeball

@badeball reported to them

amitguptagwl avatar Sep 26 '19 04:09 amitguptagwl

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.

christopherdolan avatar Jun 23 '20 01:06 christopherdolan

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.

edmodo-CeciliaChen avatar Jan 29 '21 03:01 edmodo-CeciliaChen

As of October 2022 and by using the version

^13.0.2

I'm just wondering if the example above is possible in the latest version 🤔

I've been trying to use the registry and the similar function(resolveStepDefintion) that seems to have a typo or probably that was on purpose, so this is how it looks my current intent with no luck:

cypress/e2e/CypressUtils.js

import { Registry } from '@badeball/cypress-cucumber-preprocessor/lib/registry';

export const RunStepDefinition = stepName => {
  cy.log('Stepppppppp: ' + stepName);
  console.log(Registry, 'registry')
  const theRegistry = new Registry();
  theRegistry.resolveStepDefintion({
    keyword: '*',
    text: stepName
  });
};

And calling this one within a step definition file, for instance: cypress/e2e/step_definitions/myStepDefinition.js

import { RunStepDefinition } from '../../CypressUtils';

Given('I execute another step definition', function () {
  cy.log('before RunStepDefinition')
  // I have defined this step in a separate step definition file(step_definitions/anotherStep.js)
  RunStepDefinition('I visit the home page');
  cy.log('after RunStepDefinition');
});

Please let me know any comments, and If I need to change my approach here, thanks @badeball

heridev avatar Oct 14 '22 01:10 heridev

https://github.com/badeball/cypress-cucumber-preprocessor/blob/master/docs/cucumber-basics.md#nested-steps

badeball avatar Oct 14 '22 07:10 badeball

Niiiiice, thank you very much @badeball

heridev avatar Oct 14 '22 14:10 heridev

😅 ups it was in the basics haha, thank you again

heridev avatar Oct 14 '22 14:10 heridev