Cypress: Support cy.request() and cy.route() as a Custom Command

Created on 14 Dec 2018  路  2Comments  路  Source: cypress-io/cypress

Please add support for cy.request() and cy.route() as Custom Commands

Current behavior:

When adding cy.request() or cy.route() as a custom command such as

Cypress.Commands.add("sellersignin",
  cy.request({
  method: 'POST',
  url: '/api/user/whatever',
  form: true, 
  body: {
    identification: '[email protected]',
    password: 'whatever'
  }
 }))

Cypress returns "must be within an "IT" command, regardless of if cy.sellersignin() is called in a test.
While there's a workaround in Adding this to the beginning of a bank of tests, It means that the code very quickly gets sloppy with adding requests for logins and logouts when a test needing verification that one users actions can be viewed (or not viewed) by another user.
Not allowing these types of commands in a custom command defeats the purpose of having custom commands.

Desired behavior:

Be able to assign a cy.route() command as a custom command

Versions

Cypress Version 3.1.2, OSX, All browsers

Most helpful comment

From your example code, you don't seem to be wrapping the cy.request() in a function expression, but rather calling it at the time of the Cypress.Commands.add definition.

Instead, do:

Cypress.Commands.add("sellersignin", () => {
    cy.request({ method: 'POST', url: '/api/user/whatever', form: true, body: { identification: '[email protected]', password: 'whatever' } })
});

All 2 comments

From your example code, you don't seem to be wrapping the cy.request() in a function expression, but rather calling it at the time of the Cypress.Commands.add definition.

Instead, do:

Cypress.Commands.add("sellersignin", () => {
    cy.request({ method: 'POST', url: '/api/user/whatever', form: true, body: { identification: '[email protected]', password: 'whatever' } })
});

@dwelle suggestion is correct. Closing as resolved.

Was this page helpful?
0 / 5 - 0 ratings