0
0
Cypresstesting~3 mins

Why cy.go() for browser history in Cypress? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could control browser back and forward buttons with just one command in your tests?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
Visit page A
Click link to page B
Click browser back button
Check page A loads
After
cy.visit('pageA')
cy.get('a[href="pageB"]').click()
cy.go('back')
cy.url().should('include', 'pageA')
What It Enables

It enables automated, precise control of browser navigation to test user flows without manual clicks.

Real Life Example

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.

Key Takeaways

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.