Directory Structure of Cypress

Cypress.png


Define Methods in Your Project

  1. Its own API

    Cypress comes with its own API: you can call it directly without additional processing.

    For details on how to use it, please refer to the documentation: https://docs.cypress.io/api


  1. Custom global methods

    Custom global methods: there are many scenarios in the test cases are the same, the code of these scenarios can be encapsulated to facilitate a unified call, which can not only reduce duplicate code, but also to unify the code specification.

    In this case, you can use Cypress.Commands.add in /support/command.js.


  1. cy.task() and cy.exec()

    As Cypress is executed in the browser, some operations may need to be executed using nodejs or command.

    In this case, you need to use cy.task() or cy.exec().
    Of course you need to define your own methods, where the task method needs to be defined in /plugins directory and the exec method needs to be defined in root(/) directory.


  1. A single function or class

    When a function needs to be called just in a number of other files, but does not need to be defined globally, you can use this way.


Examples for call methods

cy.login() // login() is defined in command.js
cy.task('A') // task A is defined in plugins/index.js
cy.exec('node test.js') // test.js is defined in root directory
dash.check() // function check() is defined in dash.utils.js