Bird
Raised Fist0
Cypresstesting~20 mins

Cookie management in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cookie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Cypress test snippet?
Consider the following Cypress test code that sets a cookie and then checks its value. What will be the assertion result?
Cypress
cy.setCookie('session_id', 'abc123')
  .getCookie('session_id')
  .should('have.property', 'value', 'abc123')
ATest passes because the cookie value matches 'abc123'.
BTest passes but the cookie value is undefined.
CTest fails because 'should' cannot check cookie properties.
DTest fails because 'getCookie' is not a valid Cypress command.
Attempts:
2 left
💡 Hint
Remember that Cypress commands like setCookie and getCookie work asynchronously and can be chained.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a cookie named 'user' exists in Cypress?
You want to check if a cookie named 'user' exists after login. Which assertion is correct?
Acy.getCookie('user').should('exist')
Bcy.getCookie('user').should('have.property', 'name', 'user')
Ccy.getCookie('user').should('have.value', 'user')
Dcy.getCookie('user').should('contain', 'user')
Attempts:
2 left
💡 Hint
Check the properties of the cookie object returned by getCookie.
🔧 Debug
advanced
2:00remaining
Why does this Cypress test fail to clear cookies?
This test tries to clear cookies but fails. Identify the reason.
Cypress
cy.clearCookies('session_id')
cy.getCookie('session_id').should('not.exist')
AgetCookie cannot be chained after clearCookies.
BThe cookie name 'session_id' is invalid.
CclearCookies does not accept arguments; it clears all cookies only.
Dshould('not.exist') is not a valid assertion for cookies.
Attempts:
2 left
💡 Hint
Check the Cypress documentation for clearCookies usage.
🧠 Conceptual
advanced
2:00remaining
What is the best practice for managing cookies in Cypress tests to avoid flaky tests?
Which practice helps ensure cookie-related tests are reliable and isolated?
AClear all cookies before each test to start fresh.
BSet cookies only once before all tests run.
CAvoid clearing cookies to speed up tests.
DManually delete cookies using browser DevTools during tests.
Attempts:
2 left
💡 Hint
Think about test isolation and repeatability.
❓ framework
expert
2:00remaining
Which Cypress command sequence correctly sets a cookie, reloads the page, and verifies the cookie persists?
Select the correct code snippet that sets a cookie named 'token' with value 'xyz', reloads the page, and asserts the cookie still exists with the correct value.
A
cy.setCookie('token', 'xyz')
cy.reload()
cy.getCookie('token').should('have.property', 'value', 'xyz')
B
cy.setCookie('token', 'xyz')
cy.reload()
cy.getCookie('token').should('equal', 'xyz')
C
cy.setCookie('token', 'xyz')
  .reload()
  .getCookie('token')
  .should('have.property', 'value', 'xyz')
D
cy.setCookie('token', 'xyz')
  .reload()
  .getCookie('token')
  .should('equal', 'xyz')
Attempts:
2 left
💡 Hint
Remember Cypress commands are asynchronous and chaining vs separate calls matters.

Practice

(1/5)
1. What is the main purpose of managing cookies in Cypress tests?
easy
A. To speed up the browser rendering process
B. To change the website's HTML structure
C. To store and verify small pieces of information like user sessions
D. To create new browser tabs automatically

Solution

  1. Step 1: Understand what cookies do

    Cookies store small data like user sessions or preferences to remember users.
  2. Step 2: Relate to Cypress testing

    Cypress manages cookies to test if the site remembers users or settings correctly.
  3. Final Answer:

    To store and verify small pieces of information like user sessions -> Option C
  4. Quick Check:

    Cookies store info = A [OK]
Hint: Cookies store user info; Cypress tests that info [OK]
Common Mistakes:
  • Thinking cookies change page layout
  • Confusing cookies with browser tabs
  • Assuming cookies speed up rendering
2. Which Cypress command correctly sets a cookie named session_id with value abc123?
easy
A. cy.setCookie('session_id', 'abc123')
B. cy.cookieSet('session_id', 'abc123')
C. cy.set('session_id', 'abc123')
D. cy.createCookie('session_id', 'abc123')

Solution

  1. Step 1: Recall Cypress cookie setting syntax

    The correct command to set a cookie is cy.setCookie(name, value).
  2. Step 2: Match the command with options

    Only cy.setCookie('session_id', 'abc123') uses cy.setCookie with correct parameters.
  3. Final Answer:

    cy.setCookie('session_id', 'abc123') -> Option A
  4. Quick Check:

    Use cy.setCookie() to set cookies [OK]
Hint: Use cy.setCookie(name, value) to set cookies [OK]
Common Mistakes:
  • Using wrong command names like cookieSet or createCookie
  • Confusing cy.set with cy.setCookie
  • Missing quotes around cookie name or value
3. What will be the output of this Cypress code snippet?
cy.setCookie('token', 'xyz789')
cy.getCookie('token').should('have.property', 'value', 'xyz789')
medium
A. Test passes because cookie 'token' has value 'xyz789'
B. Test fails because cookie 'token' is not set
C. Syntax error due to wrong command usage
D. Test passes but cookie value is empty string

Solution

  1. Step 1: Analyze cookie setting

    The code sets a cookie named 'token' with value 'xyz789' using cy.setCookie.
  2. Step 2: Analyze assertion

    The code gets the cookie 'token' and asserts its value property equals 'xyz789'. This matches the set value.
  3. Final Answer:

    Test passes because cookie 'token' has value 'xyz789' -> Option A
  4. Quick Check:

    Set cookie then assert value = Pass [OK]
Hint: Set cookie then assert value matches to pass test [OK]
Common Mistakes:
  • Assuming getCookie returns undefined if setCookie was called
  • Confusing property name in assertion
  • Thinking syntax is incorrect
4. Identify the error in this Cypress code that tries to clear all cookies:
cy.clearCookie()
medium
A. clearCookie clears all cookies without arguments, so no error
B. clearCookie requires a cookie name argument to clear a specific cookie
C. clearCookie is not a valid Cypress command
D. clearCookie must be called with an array of cookie names

Solution

  1. Step 1: Understand clearCookie usage

    cy.clearCookie() requires a cookie name string to clear one cookie.
  2. Step 2: How to clear all cookies

    To clear all cookies, use cy.clearCookies() (note plural) without arguments.
  3. Final Answer:

    clearCookie requires a cookie name argument to clear a specific cookie -> Option B
  4. Quick Check:

    clearCookie needs name; clearCookies clears all [OK]
Hint: Use clearCookies() to clear all, clearCookie(name) for one [OK]
Common Mistakes:
  • Calling clearCookie() without name to clear all
  • Confusing clearCookie with clearCookies
  • Assuming clearCookie accepts multiple names
5. You want to test that a user stays logged in after a page reload by preserving the auth_token cookie. Which Cypress approach correctly achieves this?
hard
A. Use cy.reload(preserveCookies: true) to keep cookies
B. Use cy.preserveCookie('auth_token') before reload
C. Use cy.clearCookies() before reload to reset cookies
D. Use cy.getCookie('auth_token').then(cookie => { cy.setCookie(cookie.name, cookie.value) }) after reload

Solution

  1. Step 1: Understand cookie preservation

    Cypress clears cookies on reload by default, so to keep a cookie, you must reset it after reload.
  2. Step 2: Analyze options

    Use 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.
  3. Final Answer:

    Use cy.getCookie('auth_token').then(cookie => { cy.setCookie(cookie.name, cookie.value) }) after reload -> Option D
  4. Quick Check:

    Manually reset cookie after reload to preserve [OK]
Hint: Manually reset cookie after reload to keep it [OK]
Common Mistakes:
  • Assuming reload keeps cookies automatically
  • Using non-existent preserveCookie command
  • Clearing cookies before reload by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard