0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.