6

I am working on a Web-test automation framework, and would like to few functionalities present in describe() block in one cypress ..spec.js file, by methods present in another in another cypress ..spec.js file?

Kindly read about Shared Behaviour facility provided in Mocha: https://github.com/mochajs/mocha/wiki/Shared-Behaviours enter image description here enter image description here

I tried that but its not working. 1. Is it possible to achieve something similar to Mocha Shared steps (as descibed above)? 2. Or is there something similar to Cucumber-ruby/Pico-container's WORLD object?

Kindly advise.

enter image description here

user2451016
  • 1,857
  • 3
  • 20
  • 44

1 Answers1

1

You can use custom commands to re-use steps over several files. This can be done by the following steps.

  1. Create a custom command in cypress/support/commands.js with the steps which you want to use in several files. You can use this syntax:
Cypress.Commands.add('customCommand', function() {
  cy.get('object')
    .clear()
    .type('something')
    // do other steps
})
  1. After you created the custom command you can use it in the testscripts by this syntax:
describe('Description of the test', function () {
  it('first scenario of the test', function () {
    cy.customCommand()
  })
})

Concluding: to share steps over several testfiles, you need to place the shared steps in the commands.js instead of in a testfile.

Mr. J.
  • 3,499
  • 1
  • 11
  • 25
  • There're certain functionalities those are in 'describe()' block that I need to reuse. But those can not be stored as Custome commands, as those are not commands & rather large chunk of code. Kindly advise – user2451016 May 23 '19 at 11:14
  • 1
    can you provide an example? – Mr. J. May 23 '19 at 11:22