Verify cookie management on example.com
Preconditions (2)
✅ Expected Result: The cookie 'session_id' is set correctly, then removed successfully.
describe('Cookie management test on example.com', () => { beforeEach(() => { cy.visit('https://example.com'); }); it('sets, verifies, and clears a cookie', () => { // Set cookie cy.setCookie('session_id', 'abc123'); // Verify cookie is set with correct value cy.getCookie('session_id').should('have.property', 'value', 'abc123'); // Clear the cookie cy.clearCookie('session_id'); // Verify cookie is removed cy.getCookie('session_id').should('be.null'); }); });
This test visits https://example.com before each test to ensure a fresh state.
It sets a cookie named session_id with value abc123 using cy.setCookie.
Then it verifies the cookie exists and has the correct value using cy.getCookie and should('have.property', 'value', 'abc123').
Next, it clears the cookie with cy.clearCookie.
Finally, it verifies the cookie no longer exists by asserting cy.getCookie('session_id').should('be.null').
This approach uses Cypress commands consistently and relies on Cypress automatic waiting, avoiding manual waits.
Now add data-driven testing to set and verify cookies with 3 different session_id values.
session_id with value abc123?cy.setCookie(name, value).cy.setCookie with correct parameters.cy.setCookie('token', 'xyz789')
cy.getCookie('token').should('have.property', 'value', 'xyz789')cy.clearCookie()
auth_token cookie. Which Cypress approach correctly achieves this?cy.getCookie('auth_token').then(cookie => { cy.setCookie(cookie.name, cookie.value) }) after reload manually gets and sets the cookie again after reload, which preserves it correctly. Use cy.preserveCookie('auth_token') before reload is invalid syntax. Use cy.clearCookies() before reload to reset cookies clears cookies, which removes the token. Use cy.reload({ preserveCookies: true }) to keep cookies is not a valid reload option in Cypress.Pass the test and this pattern moves unknown