Here’s the code sample from our tutorial on how to read, add, edit, and delete cookies with Cypress

 

describe("Cookie", () => {
beforeEach(() => {
cy.visit('/ingredients/cookie');
});
it('should get the value of a cookie', () => {
cy.getCookie('protein').should('have.property', 'value', 'chicken')
cy.getCookie('vegetable').should('have.property', 'value', 'broccoli')
});
it('should update the value of a cookie', () => {
cy.clearCookie('protein').then(() => {
cy.setCookie('protein', 'pork').then(() => {
cy.getCookie('protein').should('have.property', 'value', 'pork');
});
});
});
it('should delete the value of a cookie', () => {
cy.clearCookie('protein').then(() => {
cy.getCookie('protein').should('be.null');
});
});
it('should add a new cookie', () => {
cy.setCookie('fruit', 'papaya').then(() => {
cy.getCookie('fruit').should('have.property', 'value', 'papaya');
});
});
});
view raw cookie.spec.js delivered with ❤ by emgithub

View on GitHub
Colby Fayock
Developer Advocate

Ready for the next generation of testing?

Get started today Schedule a demo