cucumbercypressbddcypress-cucumber-preprocessor

Execute 'step 1' inside 'step 2' - cypress+cucumber


I faced with many similar pieces of code in my steps, re-using steps inside 'bigger' steps can solve the problem. Is it possible to run step 1 inside step 2?

And('My Step 1', () => {
  some code;
});

And('My Step 2', () => {
  can I execute 'My Step 1' here?
  code;
});

Solution

  • You can add the steps in the command file and then call them in your tests. Your commands will look something like this:

    Cypress.Commands.add("Step1", function () {
      Do stuff here
    });
    

    And then in your tests you can reuse those commands like this:

    it('My Step 2', () => {
      cy.Step1();
      code;
    });
    

    Remember to import the command file in your index.js