2

I have a login script written in the command file that I use as a beforeEach in each test spec file. This worked in Cypress 9, but I recently upgraded to Cypress 11 and now the session doesn't carry over into the test. The command runs successfully, but when it goes to the test I'm not signed in, so it fails.

Cypress.Commands.add("login", () => {
    cy.request({
        method: 'POST',
        url: '/Account/Login', // baseUrl is prepended to url
        form: true, // indicates the body should be form urlencoded and sets Content-Type: application/x-www-form-urlencoded headers
        body: {
            UserName: Cypress.env('username'),
            Password: Cypress.env('password'),
            RememberMe: false,
            FranchiseSelected: 1,
        }
    })
})

Screenshot: Login Script on the left that I use as beforeEach, on the right shows that login is success but then fails on the second step since I'm on the login page instead where its supposed to go

Does anyone know what I'm missing to update this to work in Cypress 11?

I have made a login command that actually goes to the page and logs in, and that works, but I would really like for this POST command to work.

Klea
  • 169
  • 7

1 Answers1

0

I'm not sure of the cause, but try wrapping the request in cy.session()

Cypress.Commands.add("login", () => {
  cy.session('login', () => {
     cy.request({...})
  })
})

The session command acts like a cache for the session token. When you call the login command in beforeEach(), the first time calls the request but subsequent times just restore the token from cache.

TesterDick
  • 3,830
  • 5
  • 18