Skip to main content

Cypress clearCookies Command

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.

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 For Using ClearCookies

  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.

    Cypress clearCookies Command

    Tags
    Submitted by arilio666 on April 20, 2022

    Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

    About

    At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.