Bird
0
0

You want to test navigation between three pages: '/start', '/middle', and '/end'. You write:

hard📝 Application Q15 of 15
Cypress - Navigation and URL
You want to test navigation between three pages: '/start', '/middle', and '/end'. You write:
cy.visit('/start')
cy.visit('/middle')
cy.visit('/end')
cy.go(-2)
cy.url().should('include', '/start')

What does cy.go(-2) do here, and why is the assertion correct?
A<code>cy.go(-2)</code> moves back one page only, so URL includes '/middle'.
B<code>cy.go(-2)</code> moves forward two pages, which is invalid here.
C<code>cy.go(-2)</code> moves back two pages from '/end' to '/start', so URL includes '/start'.
D<code>cy.go(-2)</code> reloads '/end' page twice, so URL stays '/end'.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the navigation history

    Visiting '/start', then '/middle', then '/end' creates a history stack: ['/start', '/middle', '/end'].
  2. Step 2: Interpret cy.go(-2)

    cy.go(-2) moves back two pages in history, from '/end' back to '/start'.
  3. Step 3: Confirm the assertion

    After moving back two pages, the URL should include '/start', so the assertion is correct.
  4. Final Answer:

    cy.go(-2) moves back two pages from '/end' to '/start', so URL includes '/start'. -> Option C
  5. Quick Check:

    Negative number moves back multiple pages [OK]
Quick Trick: Negative numbers in cy.go() move back that many pages [OK]
Common Mistakes:
  • Confusing negative with forward navigation
  • Thinking cy.go(-2) reloads pages
  • Assuming it moves back only one page

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes