Cypress has inbuild support to read browser cookies. There are two commands for this - GetCookie and GetCookies. Refer documentation for more details

GetCookie

This command get a cookie by its name.

1
2
3
4
5
cy.getCookie(name)

cy.getCookie(name, options)

Note: name is the name of cookie . Options can be used to change default behaviour like logging , timeout

Examples usage is as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Below line is added to get intellisense while writing code in visual studio code

/// <reference types="cypress" />


it('Read Cookies In Cypress',() =>  {

    cy.visit("www.commbank.com.au");

    //GetCookie returns an object with properties like name, domain, httpOnly, path, secure, value, expiry ( if provided), sameSite(if provided)

    //Checking for individual cookie property value
    cy.getCookie('s_cc').should('have.property','value','true');
    cy.getCookie('s_cc').should('have.property','domain','.commbank.com.au');


   // Checking multiple properties of a cookie. *cy.getCookie* will get an object. *Then* helps to work with object yielded from previous
    cy.getCookie('s_cc').then((cookie) => {

        cy.log(cookie);
        cy.log(cookie.name);
        expect(cookie.domain).to.equal('.commbank.com.au');
        expect(cookie.name).to.equal('s_cc');
        expect(cookie.httpOnly).to.equal(false);
        expect(cookie.path).to.equal('/');

        expect(cookie).to.not.have.property('expiry');
    })


})

Results from test run will look like below

result

Comments