Bird
Raised Fist0
Cypresstesting~20 mins

cy.go() for browser history in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Browser History 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 cy.go(-1) after navigation?

Consider the following Cypress test steps:

cy.visit('/page1')
cy.visit('/page2')
cy.go(-1)

What page will Cypress be on after cy.go(-1) executes?

Cypress
cy.visit('/page1')
cy.visit('/page2')
cy.go(-1)
AThe browser will stay on '/page2' because cy.go(-1) does nothing if history is empty.
BThe browser will be on '/page1' because cy.go(-1) goes back one page in history.
CThe browser will be on '/page3' because cy.go(-1) goes forward one page in history.
DThe browser will be on '/page2' because cy.go(-1) reloads the current page.
Attempts:
2 left
💡 Hint

Think about how browser history works when you visit pages and then go back.

❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies cy.go(1) navigated forward?

You have navigated back one page using cy.go(-1). Now you want to go forward one page using cy.go(1) and verify the URL is '/page2'. Which assertion is correct?

Acy.url().should('eq', '/page2')
Bcy.url().should('contain', '/page2')
Ccy.url().should('include', '/page2')
Dcy.url().should('equal', '/page2')
Attempts:
2 left
💡 Hint

Check the Cypress documentation for the correct assertion method to check URL substring.

🔧 Debug
advanced
2:00remaining
Why does cy.go(-2) cause an error in this test?

Given the test steps:

cy.visit('/home')
cy.visit('/about')
cy.go(-2)

Running cy.go(-2) causes the test to fail with a navigation error. Why?

ABecause there is only one page in history before '/about', so going back two pages is invalid.
BBecause cy.go() only accepts -1 or 1 as valid arguments.
CBecause cy.visit() clears the browser history each time it runs.
DBecause cy.go(-2) requires a preceding cy.reload() to work.
Attempts:
2 left
💡 Hint

Think about how many pages are in the browser history after two visits.

🧠 Conceptual
advanced
2:00remaining
What does cy.go() without arguments do?

In Cypress, what is the behavior of cy.go() when called without any arguments?

AIt reloads the current page, similar to cy.reload().
BIt navigates forward one page in the browser history.
CIt navigates back one page in the browser history.
DIt throws an error because an argument is required.
Attempts:
2 left
💡 Hint

Check the Cypress documentation for the default behavior of cy.go().

❓ framework
expert
3:00remaining
How to test browser history navigation with cy.go() in a single test?

You want to write a Cypress test that visits three pages in order: '/page1', '/page2', '/page3'. Then you want to go back twice and verify the URL is '/page1'. Which code snippet correctly implements this?

A
cy.visit('/page1')
cy.visit('/page2')
cy.visit('/page3')
cy.go(-2)
cy.url().should('include', '/page1')
B
cy.visit('/page1')
cy.visit('/page2')
cy.visit('/page3')
cy.go(-2)
cy.url().should('eq', '/page1')
C
cy.visit('/page1')
cy.visit('/page2')
cy.visit('/page3')
cy.go(-1)
cy.go(-1)
cy.url().should('eq', '/page1')
D
cy.visit('/page1')
cy.visit('/page2')
cy.visit('/page3')
cy.go(-2)
cy.url().should('contain', '/page1')
Attempts:
2 left
💡 Hint

Remember that cy.url() returns the full URL, so exact equality may fail if the base URL is included.

Practice

(1/5)
1. What does the Cypress command cy.go('back') do during a test?
easy
A. It reloads the current page.
B. It navigates the browser to the previous page in history.
C. It navigates the browser to the next page in history.
D. It opens a new browser tab.

Solution

  1. Step 1: Understand cy.go() usage

    The cy.go() command controls browser history navigation, similar to back and forward buttons.
  2. Step 2: Interpret the argument 'back'

    Using 'back' tells Cypress to go to the previous page in the browser history.
  3. Final Answer:

    It navigates the browser to the previous page in history. -> Option B
  4. Quick Check:

    cy.go('back') = previous page [OK]
Hint: Remember 'back' means previous page in history [OK]
Common Mistakes:
  • Confusing 'back' with 'forward'
  • Thinking it reloads the page
  • Assuming it opens a new tab
2. Which of the following is the correct syntax to move forward one page in browser history using cy.go()?
easy
A. cy.go('forward')
B. cy.go(-1)
C. cy.go('back')
D. cy.go(0)

Solution

  1. Step 1: Recall cy.go() arguments

    cy.go() accepts 'back', 'forward' or numbers like -1, 1 to move in history.
  2. Step 2: Identify correct forward syntax

    To move forward one page, use 'forward' or 1. Among options, only cy.go('forward') is correct syntax.
  3. Final Answer:

    cy.go('forward') -> Option A
  4. Quick Check:

    Forward navigation = 'forward' [OK]
Hint: Use 'forward' string or 1 to go forward [OK]
Common Mistakes:
  • Using negative number to go forward
  • Using 'back' instead of 'forward'
  • Using 0 which does nothing
3. Given this Cypress test code:
cy.visit('/page1')
cy.visit('/page2')
cy.go(-1)
cy.url().should('include', '/page1')

What will the assertion check after cy.go(-1) executes?
medium
A. The URL includes '/page1'
B. The URL includes '/page2'
C. The URL includes '/page3'
D. The URL remains unchanged

Solution

  1. Step 1: Trace navigation commands

    First, the test visits '/page1', then '/page2', so browser history has '/page1' then '/page2'.
  2. Step 2: Understand cy.go(-1)

    cy.go(-1) moves back one page in history, so it goes back from '/page2' to '/page1'.
  3. Step 3: Check the assertion

    The assertion checks that the URL includes '/page1', which is true after going back.
  4. Final Answer:

    The URL includes '/page1' -> Option A
  5. Quick Check:

    cy.go(-1) = back one page = '/page1' [OK]
Hint: Negative number in cy.go() means go back [OK]
Common Mistakes:
  • Assuming URL stays at '/page2'
  • Confusing -1 with forward navigation
  • Expecting URL to be '/page3' which was never visited
4. You wrote this Cypress test:
cy.visit('/home')
cy.visit('/about')
cy.go(2)
cy.url().should('include', '/contact')

But the test fails. What is the likely reason?
medium
A. cy.go(2) is invalid syntax and causes an error.
B. cy.go(2) goes back two pages, so URL is wrong.
C. cy.go(2) reloads the current page instead of navigating.
D. cy.go(2) tries to go forward two pages but no forward history exists.

Solution

  1. Step 1: Analyze browser history after visits

    After visiting '/home' then '/about', browser history has two pages: '/home' then '/about'. No forward pages exist.
  2. Step 2: Understand cy.go(2) behavior

    cy.go(2) tries to go forward two pages, but since no forward history exists, navigation fails.
  3. Step 3: Reason test failure

    The URL does not change to '/contact' because forward navigation is impossible, causing the assertion to fail.
  4. Final Answer:

    cy.go(2) tries to go forward two pages but no forward history exists. -> Option D
  5. Quick Check:

    Forward navigation beyond history length fails [OK]
Hint: Cannot go forward beyond available history [OK]
Common Mistakes:
  • Thinking cy.go(2) reloads page
  • Assuming cy.go(2) goes back
  • Believing cy.go(2) is invalid syntax
5. 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?
hard
A. cy.go(-2) moves back one page only, so URL includes '/middle'.
B. cy.go(-2) moves forward two pages, which is invalid here.
C. cy.go(-2) moves back two pages from '/end' to '/start', so URL includes '/start'.
D. cy.go(-2) reloads '/end' page twice, so URL stays '/end'.

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]
Hint: 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
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