0
0
Cypresstesting~20 mins

Programmatic login (cy.request) in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Programmatic Login Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the result of this Cypress test code?

Consider this Cypress test snippet that uses cy.request for programmatic login:

cy.request({
  method: 'POST',
  url: '/api/login',
  body: { username: 'user1', password: 'pass123' }
}).then((response) => {
  expect(response.status).to.eq(200)
  cy.setCookie('session_id', response.body.sessionId)
})
cy.visit('/dashboard')
cy.get('h1').should('contain.text', 'Welcome user1')

What will happen when this test runs?

Cypress
cy.request({
  method: 'POST',
  url: '/api/login',
  body: { username: 'user1', password: 'pass123' }
}).then((response) => {
  expect(response.status).to.eq(200)
  cy.setCookie('session_id', response.body.sessionId)
})
cy.visit('/dashboard')
cy.get('h1').should('contain.text', 'Welcome user1')
AThe test fails because cy.visit runs before the login request completes, so the dashboard is not authenticated.
BThe test passes because cy.request waits for the login to complete before visiting the dashboard.
CThe test fails because cy.setCookie is not a valid Cypress command.
DThe test passes but the assertion on the h1 text is skipped.
Attempts:
2 left
💡 Hint

Think about how Cypress commands are queued and asynchronous behavior.

assertion
intermediate
1:30remaining
Which assertion correctly verifies a successful login response?

You receive this JSON response from a login API:

{"status": "success", "token": "abc123", "user": {"id": 5, "name": "Alice"}}

Which Cypress assertion correctly checks the response indicates success and contains a token?

Aexpect(response.status).to.eq(200) && expect(response.body.token).to.be.undefined
Bexpect(response.body.status).to.be.true && expect(response.body.token).to.be.a('string')
Cexpect(response.body.status).to.equal('success') && expect(response.body.token).to.exist
Dexpect(response.body.status).to.eq('success') && expect(response.body.token).to.be.a('string')
Attempts:
2 left
💡 Hint

Check the exact property names and types in the response body.

locator
advanced
1:30remaining
Choose the best locator to verify login success message after programmatic login

After a successful programmatic login, the page shows a message:

<div role="alert" aria-live="polite" class="login-message">Login successful!</div>

Which Cypress selector is best to locate this message for assertion?

Acy.get('div.login-message')
Bcy.get('.login-message[role="alert"]')
Ccy.get('[aria-live="polite"]')
Dcy.get('div').contains('Login successful!')
Attempts:
2 left
💡 Hint

Consider accessibility and specificity for reliable selectors.

🔧 Debug
advanced
2:00remaining
Why does this programmatic login test fail intermittently?

Test code snippet:

cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
  .then((res) => {
    cy.setCookie('auth_token', res.body.token)
  })
cy.visit('/profile')
cy.get('.profile-name').should('be.visible')

Sometimes the test fails because the profile name is not visible. Why?

Cypress
cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
  .then((res) => {
    cy.setCookie('auth_token', res.body.token)
  })
cy.visit('/profile')
cy.get('.profile-name').should('be.visible')
Acy.visit runs before the cookie is set, so the profile page loads without authentication.
Bcy.setCookie is asynchronous and must be awaited with async/await syntax.
CThe selector '.profile-name' is incorrect and does not match any element.
DThe login API sometimes returns an invalid token causing the failure.
Attempts:
2 left
💡 Hint

Think about the order of Cypress commands and their execution.

framework
expert
2:30remaining
Which Cypress command sequence correctly performs programmatic login and visits a protected page?

You want to programmatically log in by sending a POST request, set the auth cookie, then visit the dashboard page. Which code snippet achieves this correctly?

A
cy.visit('/dashboard')
cy.request('POST', '/api/login', {user: 'u', pass: 'p'}).then(res =&amp;gt; {
  cy.setCookie('auth', res.body.token)
})
B
cy.request('POST', '/api/login', {user: 'u', pass: 'p'}).then(res =&amp;gt; {
  cy.setCookie('auth', res.body.token)
})
cy.visit('/dashboard')
C
cy.request('POST', '/api/login', {user: 'u', pass: 'p'}).then(res =&amp;gt; {
  cy.setCookie('auth', res.body.token)
  cy.visit('/dashboard')
})
D
cy.request('POST', '/api/login', {user: 'u', pass: 'p'})
cy.setCookie('auth', 'token')
cy.visit('/dashboard')
Attempts:
2 left
💡 Hint

Remember to chain commands so they run in the correct order.