Cypress clearCookies Command

Profile picture for user arilio666

Cypress clearCookies command is a way in Cypress to clear out browser cookies for the current domain and subdomain.

To prevent the state from being shared across tests, Cypress intelligently clears all cookies automatically before each test. You can use this command when you want to remove a specific cookie inside a single test.

Table of Content

  1. Syntax
  2. Rules
  3. Example
  4. Video Tutorial

Syntax

cy.clearCookies()
cy.clearCookies(options)

Arguments Used In clearCookies.

Options: Pass in the options object to change the default behavior of cy.clearCookies()

  • Log: Displays the command in the command log, and the default value is true.
  • Timeout: Enter the time to wait for my.clearCookies() to resolve before timing out, and the default value is responseTimeout.

Rules

  1. cy.clearCookies() must be chained off of cy. cy.clearCookies() yields null and cannot be chained further.
  2. cy.clearCookies() cannot have any assertions chained to it.
  3. cy.clearCookies() should never time out.

Example

describe('Automate AutoPract',()=>{
    it('Should load the url',()=>{
        cy.visit('/')
        cy.get('.close').click()
        cy.get('.bar-style').click()
        cy.get('a').contains(' footwear ')
        cy.clearCookies()
        cy.getCookies().should('be.empty')        
    })
})

Cypress clearCookies Command

Hereafter the automation, we can see that we gave clear cookies and asserted it to check whether it is empty after clearing. As you can see, it is empty after removing, thus meaning it clears cookies when this command is executed.

Conclusion

So using this command is ultimately unnecessary and optional depending on the test it is being used on.