What if you could control browser back and forward buttons with just one command in your tests?
Why cy.go() for browser history in Cypress? - Purpose & Use Cases
Imagine testing a website where you need to check if clicking links changes pages correctly. You try to go back and forth in the browser history manually, clicking the back and forward buttons each time.
Doing this by hand is slow and boring. You might forget to click the right button or miss a step. It's easy to make mistakes and hard to repeat the exact same steps every time.
Using cy.go() in Cypress lets you control browser history with simple commands. You can move back or forward in history automatically, making tests faster, reliable, and repeatable.
Visit page A Click link to page B Click browser back button Check page A loads
cy.visit('pageA') cy.get('a[href="pageB"]').click() cy.go('back') cy.url().should('include', 'pageA')
It enables automated, precise control of browser navigation to test user flows without manual clicks.
Testing an online store where users browse products, then go back to the list to choose another item. cy.go() helps simulate this navigation smoothly in tests.
Manual browser navigation in tests is slow and error-prone.
cy.go() automates moving through browser history easily.
This makes tests faster, reliable, and repeatable.
Practice
cy.go('back') do during a test?Solution
Step 1: Understand
Thecy.go()usagecy.go()command controls browser history navigation, similar to back and forward buttons.Step 2: Interpret the argument 'back'
Using 'back' tells Cypress to go to the previous page in the browser history.Final Answer:
It navigates the browser to the previous page in history. -> Option BQuick Check:
cy.go('back')= previous page [OK]
- Confusing 'back' with 'forward'
- Thinking it reloads the page
- Assuming it opens a new tab
cy.go()?Solution
Step 1: Recall
cy.go()argumentscy.go()accepts 'back', 'forward' or numbers like -1, 1 to move in history.Step 2: Identify correct forward syntax
To move forward one page, use 'forward' or 1. Among options, onlycy.go('forward')is correct syntax.Final Answer:
cy.go('forward') -> Option AQuick Check:
Forward navigation = 'forward' [OK]
- Using negative number to go forward
- Using 'back' instead of 'forward'
- Using 0 which does nothing
cy.visit('/page1')
cy.visit('/page2')
cy.go(-1)
cy.url().should('include', '/page1')What will the assertion check after
cy.go(-1) executes?Solution
Step 1: Trace navigation commands
First, the test visits '/page1', then '/page2', so browser history has '/page1' then '/page2'.Step 2: Understand
cy.go(-1)cy.go(-1)moves back one page in history, so it goes back from '/page2' to '/page1'.Step 3: Check the assertion
The assertion checks that the URL includes '/page1', which is true after going back.Final Answer:
The URL includes '/page1' -> Option AQuick Check:
cy.go(-1) = back one page = '/page1' [OK]
- Assuming URL stays at '/page2'
- Confusing -1 with forward navigation
- Expecting URL to be '/page3' which was never visited
cy.visit('/home')
cy.visit('/about')
cy.go(2)
cy.url().should('include', '/contact')But the test fails. What is the likely reason?
Solution
Step 1: Analyze browser history after visits
After visiting '/home' then '/about', browser history has two pages: '/home' then '/about'. No forward pages exist.Step 2: Understand
cy.go(2)behaviorcy.go(2)tries to go forward two pages, but since no forward history exists, navigation fails.Step 3: Reason test failure
The URL does not change to '/contact' because forward navigation is impossible, causing the assertion to fail.Final Answer:
cy.go(2) tries to go forward two pages but no forward history exists. -> Option DQuick Check:
Forward navigation beyond history length fails [OK]
- Thinking cy.go(2) reloads page
- Assuming cy.go(2) goes back
- Believing cy.go(2) is invalid syntax
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?Solution
Step 1: Understand the navigation history
Visiting '/start', then '/middle', then '/end' creates a history stack: ['/start', '/middle', '/end'].Step 2: Interpret
cy.go(-2)cy.go(-2)moves back two pages in history, from '/end' back to '/start'.Step 3: Confirm the assertion
After moving back two pages, the URL should include '/start', so the assertion is correct.Final Answer:
cy.go(-2) moves back two pages from '/end' to '/start', so URL includes '/start'. -> Option CQuick Check:
Negative number moves back multiple pages [OK]
- Confusing negative with forward navigation
- Thinking cy.go(-2) reloads pages
- Assuming it moves back only one page
Ready to prove it?
Pass the test and this pattern moves unknown
