I am not able to clear cookies when my Test case fails

110 Views Asked by At

Whenever my test case fails i need to go & clear the cookies manually form cypress dashboard browser otherwise my url does not load whenever i re-run the test case

describe('My Test Suite', () => {


beforeEach(() => {
    cy.fixture('login').then((loginData) => {
      data = loginData
      cy.clearCookies()
      cy.clearLocalStorage()
      cy.login(data.UserName, data.Password)
    })
  })


it('Click on Project Inventory & verify Headers', () => {

    CustPage.ProjectInventoryIcon().click()
})


afterEach(() => {
    cy.clearCookies()
    CustPage.MenuContainer().click()
    CustPage.Logout().click()
  })

I tried the above code but when my test case fails i need to clear cookies from cypress dashboard browser i want to clear through script once it fails

1

There are 1 best solutions below

3
Aladin Spaz On BEST ANSWER

As long as during the startup sequence you are performing the cy.visit() after you clear cookies, you should have no problem.

Make sure you use the All version to cover all domains

Clear localStorage data for all origins with which the test has interacted.

The docs say that clearAllCookies() is asynchronous, so if you follow up with an assertion that cookies are empty, the test guarantee that the cleardown has happened.

Also make sure to have testIsolation on, in fact Cypress should be removing session data automatically for you.

describe('My Test Suite', {testIsolation:true}, () => {

beforeEach(() => {

  cy.clearAllCookies()
  cy.getAllCookies().should('be.empty')

  cy.clearAllLocalStorage()
  cy.getAllLocalStorage().should('be.empty')

  cy.clearAllSessionStorage()
  cy.getAllSessionStorage().should('be.empty')

  // now you can visit the baseUrl
  cy.fixture('login').then(data => {
    cy.login(data.UserName, data.Password)
  })
})

afterEach() is not recommended for cleardown precisely for that reason - test fails might block the afterEach hook.