Search code examples
mocha.jscypresspicocontainer

How to share describe() block in Cypress.io across various files, similar to Mocha's 'Shared Behaviour' facility


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


Solution

  • 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.