Test Overview
This test navigates between two pages using Cypress commands and then uses cy.go() to move back and forward in the browser history. It verifies that the URL changes correctly after each navigation step.
This test navigates between two pages using Cypress commands and then uses cy.go() to move back and forward in the browser history. It verifies that the URL changes correctly after each navigation step.
describe('Test cy.go() browser history navigation', () => { it('should navigate back and forward in browser history', () => { cy.visit('https://example.cypress.io') cy.contains('type').click() cy.url().should('include', '/commands/actions') cy.go('back') cy.url().should('eq', 'https://example.cypress.io/') cy.go('forward') cy.url().should('include', '/commands/actions') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Browser is ready to open a page | — | PASS |
| 2 | cy.visit('https://example.cypress.io') | Browser opens https://example.cypress.io homepage | — | PASS |
| 3 | cy.contains('type').click() | Page navigates to the 'type' commands page | — | PASS |
| 4 | cy.url().should('include', '/commands/actions') | URL includes '/commands/actions' | Verify URL contains '/commands/actions' | PASS |
| 5 | cy.go('back') | Browser navigates back to the homepage | — | PASS |
| 6 | cy.url().should('eq', 'https://example.cypress.io/') | URL is exactly 'https://example.cypress.io/' | Verify URL equals homepage URL | PASS |
| 7 | cy.go('forward') | Browser navigates forward to the 'type' commands page | — | PASS |
| 8 | cy.url().should('include', '/commands/actions') | URL includes '/commands/actions' again | Verify URL contains '/commands/actions' after forward navigation | PASS |
cy.go('back') do during a test?cy.go() usagecy.go() command controls browser history navigation, similar to back and forward buttons.cy.go('back') = previous page [OK]cy.go()?cy.go() argumentscy.go() accepts 'back', 'forward' or numbers like -1, 1 to move in history.cy.go('forward') is correct syntax.cy.visit('/page1')
cy.visit('/page2')
cy.go(-1)
cy.url().should('include', '/page1')cy.go(-1) executes?cy.go(-1)cy.go(-1) moves back one page in history, so it goes back from '/page2' to '/page1'.cy.visit('/home')
cy.visit('/about')
cy.go(2)
cy.url().should('include', '/contact')cy.go(2) behaviorcy.go(2) tries to go forward two pages, but since no forward history exists, navigation fails.cy.visit('/start')
cy.visit('/middle')
cy.visit('/end')
cy.go(-2)
cy.url().should('include', '/start')cy.go(-2) do here, and why is the assertion correct?cy.go(-2)cy.go(-2) moves back two pages in history, from '/end' back to '/start'.Pass the test and this pattern moves unknown