Bird
0
0

You want to programmatically log in and then visit a protected page. Which code snippet correctly ensures the login completes before visiting the page?

hard📝 Application Q8 of 15
Cypress - Authentication and Sessions
You want to programmatically log in and then visit a protected page. Which code snippet correctly ensures the login completes before visiting the page?
Acy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } }); cy.visit('/dashboard');
Bcy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } }).then(() => { cy.visit('/dashboard'); });
Ccy.visit('/dashboard'); cy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } });
Dcy.visit('/dashboard').then(() => { cy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } }); });
Step-by-Step Solution
Solution:
  1. Step 1: Understand asynchronous command chaining

    cy.request returns a promise; visiting protected page must wait for login.

  2. Step 2: Identify correct chaining

    cy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } }).then(() => { cy.visit('/dashboard'); }); uses then() to visit after login completes.

  3. Final Answer:

    cy.request({ method: 'POST', url: '/login', body: { username: 'user', password: 'pass' } }).then(() => { cy.visit('/dashboard'); }); -> Option B
  4. Quick Check:

    Chain visit inside then() after login [OK]
Quick Trick: Chain cy.visit inside then() after cy.request login [OK]
Common Mistakes:
  • Running cy.visit before login completes
  • Not chaining commands properly
  • Assuming synchronous execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes