0
0
Cypresstesting~20 mins

Token-based authentication in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Token Auth Master
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 this Cypress test that tries to authenticate using a token and then checks the user profile page.

describe('Token Auth Test', () => {
  it('should load profile with valid token', () => {
    cy.visit('/profile', {
      onBeforeLoad(win) {
        win.localStorage.setItem('authToken', 'valid-token-123')
      }
    })
    cy.get('[data-cy=user-name]').should('contain.text', 'Alice')
  })
})

What will happen when this test runs?

Cypress
describe('Token Auth Test', () => {
  it('should load profile with valid token', () => {
    cy.visit('/profile', {
      onBeforeLoad(win) {
        win.localStorage.setItem('authToken', 'valid-token-123')
      }
    })
    cy.get('[data-cy=user-name]').should('contain.text', 'Alice')
  })
})
ATest fails because cy.get selector is incorrect and throws an error
BTest fails because localStorage cannot be set in onBeforeLoad
CTest passes because the token is set before page load and user name is found
DTest passes but assertion fails because user name is not 'Alice'
Attempts:
2 left
💡 Hint

Think about when localStorage is set and how Cypress commands work.

assertion
intermediate
1:30remaining
Which assertion correctly verifies the token is stored in localStorage?

You want to check that the token 'abc123' is saved in localStorage under the key 'authToken' after login.

Which Cypress assertion is correct?

Cypress
cy.window().then(win => {
  // assertion here
})
Aexpect(win.localStorage.authToken).to.equal('abc123')
Bexpect(win.localStorage.getItem('authToken')).to.equal('abc123')
Cexpect(win.localStorage.getItem('authToken')).to.be.undefined
Dexpect(win.localStorage.authToken).to.be.true
Attempts:
2 left
💡 Hint

Remember how to get an item from localStorage and check its value.

locator
advanced
1:30remaining
Which locator is best practice to select the login button for token authentication?

You want to select the login button in your Cypress test to trigger token-based login.

Which locator is best for maintainability and accessibility?

Acy.get('button.login')
Bcy.get('#loginBtn')
Ccy.get('button').contains('Login')
Dcy.get('[data-cy=login-button]')
Attempts:
2 left
💡 Hint

Think about selectors that are stable and do not depend on styling or text.

🔧 Debug
advanced
2:00remaining
Why does this Cypress test fail to authenticate with token?

Look at this test code snippet:

cy.visit('/dashboard')
cy.window().then(win => {
  win.localStorage.setItem('authToken', 'token123')
})
cy.get('[data-cy=welcome-msg]').should('contain.text', 'Welcome')

Why does the test fail to show the welcome message?

AThe token is set after the page loads, so the app does not see it on initial load
BlocalStorage.setItem syntax is incorrect and throws an error
CThe selector '[data-cy=welcome-msg]' is invalid and causes failure
DThe token value 'token123' is expired and rejected by the app
Attempts:
2 left
💡 Hint

Consider when localStorage is set relative to page load.

framework
expert
3:00remaining
How to securely handle token refresh in Cypress tests?

Your app uses short-lived tokens and refresh tokens to maintain authentication.

Which approach is best to handle token refresh automatically in Cypress tests?

AUse Cypress task to call backend refresh endpoint and update tokens dynamically
BIntercept API calls and stub token refresh responses with fixed tokens
CManually set new tokens in localStorage before each test without API calls
DIgnore token refresh and rely on long-lived tokens for tests
Attempts:
2 left
💡 Hint

Think about how to keep tokens fresh and tests realistic.