0
votes

I am validating the cookie in my below 'login' Cypress test, but cypress throws following error :

TypeError: cy.chain is not a function

Clearly imported the following under '../support/index.js' Can someone please advise why the error is throwing

import './commands'

cypress test:

describe("Login test validate cookie", () => {
    it.only('Verify the cookies test for login', function() {
        cy
        .login(Cypress.env('email'), Cypress.env('password'))
        cy
        .getCookie('csrftoken')
        .then((csrftoken) => {
            console.log(csrftoken)
        })
    })

Following is my 'login' method/function ../support/commands.js

Cypress.Commands.add('login', (email, password) => {
    return cy.chain().request({
        method: 'POST',
        form: true,
        url: '${cypress.env("test_server")}',
        body: '{"email", "password"}',
    })
}); 

The below details are provided in 'cypress.env.json' file

{ 
  "email": "[email protected]",
  "password": "test1234"
}
1

1 Answers

0
votes

The error is correct; cy.chain() is indeed not a function. However, there are a number of issues in your command:

  1. Unless you plan to chain off this command, you don't need to return anything. It certainly doesn't hurt anything though.
  2. As mentioned previously, .chain() is unnecessary.
  3. Your url field needs to use backticks (``) for the ${...} to work.
  4. Your body field is going to contain "email" and "password", instead of your actual email and password.

It's possible there are other issues, but these are the ones I can see.

Here's what your command would look like without those issues:

Cypress.Commands.add('login', (email, password) => {
    cy.request({
        method: 'POST',
        form: true,
        url: `${cypress.env("test_server")}`,
        body: `{"${email}", "${password}"}`,
    });
});