javascriptcypresssession-cookiespreserve

Cypress preserving cookies over for a complete test suite


I am testing that requires you to be logged in while performing tests. In order to stay logged in I am preserving Cookies during a test run.

I use a custom function:

Cypress.Commands.add('preserveAllCookiesOnce', () => {
    Cypress.Cookies.defaults({
        preserve: (cookie) => {
            return true;
        }
    })
})

Also tried

beforeEach(function () {
        Cypress.Cookies.preserveOnce('session', 'YII_CSRF_TOKEN');
    })

(Double-checked the name of the cookies)

Now in this test case I have included two post requests after logging in. But after those two POST request tests I get a 403 error when trying to access the page again.

If you need more details, please let me know. I am trying to keep the code to the minimum needed to understand the problem:

import {Login} from "../../pages/login/Login";

describe('test POST/client validation', () => {
    beforeEach(function () {
        cy.preserveAllCookiesOnce()
    })
    it('log in', () => {
        login.goToLoginPage()
        login.loginCredentials(Cypress.env('userEmail'), Cypress.env('userPass'))
    })
    it('test some stuff', function () {
        cy.visit(Cypress.env('url') + '/index.php?r=tc/tcSettings/index&language=de')
        .....
    })
    it('send POST/client request with incorrect values', function () {
        cy.request({
            method: 'POST',
           ...
        })
            .then(response => {
                expect(response.status).to.eq(400)
                expect(response.body.fields).to.contain({stuff})
            })
    })
    it('send POST/client request with correct values', function () {
            cy.request({
              ...
            })
                .then(response => {
                    expect(response.status).to.eq(200)
                })
    })
    it('go to clients page and assert created client', () => {
            cy.visit(Cypress.env('url') + '/index.php?r=client/index&language=de') 
    })
})

It looks like preserving the cookies does not work through the POST tests. When I try to access the website in the last step I get a 403 status.

Usually I can run any number of it instances with the command cy.preserveAllCookiesOnce() so my guess it, that it might have to do with the POST requests in between

Screenshot of Cookies after the POST step:

Cookies console


Solution

  • Is the cy.preserveAllCookiesOnce() a custom command that you created in your Cypress's project? Note that you should use Cypress.Cookies.preserveOnce() instead:

    beforeEach(function () {
        // before each test, we can automatically preserve the
        // 'session_id' and 'remember_token' cookies. this means they
        // will not be cleared before the NEXT test starts.
        //
        // the name of your cookies will likely be different
        // this is an example
        Cypress.Cookies.preserveOnce('session_id', 'remember_token');
    })
    

    To enable or disable cookie debugging, use Cypress.Cookies.debug().

    // Cypress will now log in the console when
    // cookies are set or removed
    Cypress.Cookies.debug(true)
    
    cy.setCookie('fakeCookie', '123ABC')
    cy.clearCookie('fakeCookie')
    cy.setCookie('fakeCookie', '123ABC')
    cy.clearCookie('fakeCookie')
    cy.setCookie('fakeCookie', '123ABC')
    

    You can read more about Cypress.Cookies's API here.