October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

How to Test Navigation in Cypress

A practical Cypress guide to verifying links, route changes, redirects, browser history, network-dependent navigation, and cross-origin transitions.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test navigation in Cypress by loading a starting page, performing the action a user would take, and asserting both the resulting location and meaningful content on the destination page. Use cy.url() for the full URL or cy.location() for a specific pathname, query string, or hash. Then choose the right pattern for redirects, browser history, network requests, or cross-origin transitions.

Test a link from click to destination

A useful end-to-end navigation test checks more than whether the address bar changed: it verifies that the user arrived at the intended page and that the page rendered something expected. Cypress’s first end-to-end test uses this setup-action-assertion pattern. (Cypress first end-to-end test)

it('navigates from the home page to actions', () => {
  cy.visit('/')
  cy.contains('type').click()
  cy.url().should('include', '/commands/actions')
  cy.get('h1').should('be.visible')
})

Configure baseUrl in Cypress configuration so tests can visit relative paths such as / or /admin without hard-coding a host and port in every spec. Cypress recommends this approach for environment-independent tests. (Cypress best practices)

Assert the location component that matters

cy.url() yields the full current URL and is an alias for the href location value. cy.location() lets a test target a particular component, such as pathname, search, or hash. Both are useful; prefer the narrowest assertion that captures the behavior under test. Cypress retries URL assertions until the chained assertion passes. (cy.url(); cy.location())

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Route or redirect: assert pathname.
  • Query state: assert search when the query string is part of the behavior.
  • Hash routing: assert hash. For hash-based routes, the route and any query following # are in the hash component, not the regular search component.
  • Full destination: use cy.url() when the complete URL is important. If comparing the full host, derive it from configured baseUrl rather than embedding an environment-specific value.
cy.get('[data-cy=edit-user]').click()
cy.location('pathname').should('eq', '/users/1/edit')

Use equality when the complete component is specified by the behavior; use a looser assertion only when variable parts are intentional.

Test direct visits and redirects

cy.visit() follows redirects. To test what the browser ultimately displays, visit the route and assert the final location and, where useful, the destination page. Cypress documents visiting a protected route and checking that the browser lands on a login path. (cy.visit(); cy.location())

cy.visit('/admin')
cy.location('pathname').should('eq', '/login')
cy.get('h1').should('contain', 'Log in')

cy.visit() waits for the page’s load event. Its documented requirements include an HTML response and a successful 2xx response after redirects. For a browser-navigation test, assert the final URL or location rather than treating the initial request as the destination. (cy.visit())

When the HTTP redirect response itself matters

A browser test and an HTTP redirect test answer different questions. The browser test checks the page a user reaches after following redirects. To inspect the redirect response itself, use cy.request() with followRedirect: false and assert its redirect destination information. (cy.request(); Cypress API testing)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Exercise browser back and forward

Use cy.go('back') and cy.go('forward') to test history traversal; -1 and 1 are equivalent directions. Cypress waits for a new page load when history navigation causes a full refresh. For a history change such as hash navigation that does not load a new page, the command resolves immediately. Assert the resulting route and relevant page state after the command. (cy.go())

it('returns to the product list with browser back', () => {
  cy.visit('/products')
  cy.get('[data-cy=product-link]').click()
  cy.location('pathname').should('include', '/products/')

  cy.go('back')
  cy.location('pathname').should('eq', '/products')
  cy.get('[data-cy=product-list]').should('be.visible')
})

For forward navigation, first establish a history entry by navigating away, go back, and then call cy.go('forward'); assert the expected destination just as you would after a back action.

Coordinate route changes with network requests

If a route transition triggers a request that must be observed or stubbed, register cy.intercept() before cy.visit() whenever the request may begin during application startup. Adding the intercept after the visit resolves can be too late if the app has already issued the request. Choose a real response when the test needs to verify backend integration, or stub a response when isolating the UI transition is the goal. (cy.visit(); Cypress network requests)

it('loads a route using its startup request', () => {
  cy.intercept('GET', '/api/products', { fixture: 'products.json' }).as('products')
  cy.visit('/products')
  cy.wait('@products')
  cy.location('pathname').should('eq', '/products')
  cy.get('[data-cy=product-list]').should('be.visible')
})

Adjust the request matcher, fixture, and page selectors to your application. The ordering is the important part: set up interception before the visit if startup can trigger the request.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle navigation across origins

When a test interacts with a different origin after navigating there, wrap that origin’s interactions in cy.origin(). Current Cypress guidance says this applies even when the origins share a superdomain. Cypress 14.0.0 changed the default behavior by stopping automatic injection of document.domain; check the version installed in your project before relying on version-specific setup. (Cross-origin testing; cy.origin())

cy.visit('/account')
cy.get('[data-cy=external-login]').click()

cy.origin('https://identity.example.com', () => {
  cy.get('input[name=email]').type('[email protected]')
  cy.get('button[type=submit]').click()
})

Replace the example origin and selectors with the actual destination. Keep the origin-specific commands inside the callback; do not assume commands can interact with the new origin as if it were still the starting site.

Choose a test by the navigation behavior

Behavior under test Mechanism Useful success assertion
User follows an in-app link cy.visit(), user action, then location assertion Destination pathname plus a visible destination element
Browser lands after a redirect cy.visit() and inspect final browser location Final pathname and destination content
Redirect response at HTTP layer cy.request() with followRedirect: false Redirect destination information from the response
Back or forward history cy.go('back') or cy.go('forward') Resulting location and page state
Startup request during route load Register cy.intercept() before cy.visit() Observed or stubbed request and rendered route
Interaction after an origin change cy.origin() for the destination origin Expected state after destination interaction

Common failures and how to fix them

  • The URL assertion passes, but the page is wrong or empty. Add an assertion for destination content or behavior, not just the URL.
  • The test misses a startup request. Register the intercept before cy.visit(), since the app may issue the request before the visit finishes.
  • The redirect assertion checks the original route. cy.visit() follows redirects; assert the final browser location.
  • A hash route fails a search-string assertion. Check hash for route state after #; use search for the ordinary query component.
  • A full-URL assertion breaks in another environment. Prefer a relative cy.visit() with configured baseUrl and assert the relevant location component.
  • Commands fail after navigation to another origin. Use cy.origin() around interactions for the destination origin, and confirm behavior against the installed Cypress version.

The Cypress recipes index includes a category for links that open a new tab. Use the linked recipe for implementation details rather than assuming a particular workaround applies to every app. (Cypress recipes)

Or skip the browser setup

For a screenshot of a destination page rather than an interactive Cypress assertion, ScreenshotNeo can return an image or PDF with one GET request. (ScreenshotNeo)

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners are accepted and removed before the shot, along with known newsletter popups and chat widgets. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing; response headers identify the page verdict and billing status. Its MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up free for 1,000 screenshots a month, with no card required.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.