How do you work with URLs, navigation and browser history?
URLs are read with cy.url() and cy.location(), history is moved with cy.go() and a page is refreshed with cy.reload(). Both URL commands retry together with their assertion and wait for navigation on their own, while go and reload run once. Write the check as an assertion, not a one-off read in .then, and prefer cy.location('pathname'), which does not depend on the host.
Cypress · Practice and details
After a click the test checks the address at once and fails, although the page opens a moment later. Why?
The «Statistics» button leads to /statistics?period=30d. The test checks the address right after the click. Sometimes it passes, sometimes not. It depends on how the check is written: with a retried assertion or a one-off read of the value. The lesson is about URLs, navigation and history in Cypress.
How it works and why
cy.url(): the full URL as a string.cy.location(): an object withpathname,search,hashand other parts;cy.location('pathname')returns a single part. Convenient for checking parts separately.cy.go('back')/cy.go('forward'): the «back» and «forward» buttons.cy.reload(): reloads the page.
cy.url() and cy.location() are queries retried together with their assertions, so assertions on the URL wait for navigation themselves. cy.go() and cy.reload() are ordinary commands: they run once, and only the assertions after them are retried.
A retried query and a one-off read
cy.location('pathname').should(...)
Waits until the timeout for the URL to become the right one.
cy.url().then(u => expect(u)...)
Reads the address at this moment: if navigation has not finished, it fails.
should chained to a query.Example: before → after
// before: a single read, flaky
cy.get('[data-cy=stats-link]').click();
cy.url().then((url) => {
expect(url).to.include('/statistics');
});
// after: assertions retry until navigation
cy.get('[data-cy=stats-link]').click();
cy.location('pathname').should('eq', '/statistics');
cy.location('search').should('include', 'period=30d');
cy.go('back');
cy.url().should('include', '/dashboard');
For parameters it is convenient to open the page with a qs object: cy.visit('/statistics', { qs: { period: '30d' } }). A full URL can be compared with the base: cy.url().should('eq', Cypress.config('baseUrl') + '/statistics'), but cy.location('pathname') does not depend on the host and is more reliable across environments.
// a page that answers 404, without failing the test at visit itself
cy.visit('/missing', { failOnStatusCode: false });
cy.contains('Page not found').should('be.visible');
// an SPA hash route
cy.location('hash').should('eq', '#/settings');
// wait for navigation through the network
cy.intercept('GET', '/api/stats*').as('stats');
cy.get('[data-cy=stats-link]').click();
cy.wait('@stats');
By default cy.visit fails on any response other than 2xx and 3xx, so an intentional error page needs failOnStatusCode: false. Hash routes do not reload the page; check them with cy.location('hash'). If the app still loads data after navigating, waiting for the response through a cy.intercept alias is more reliable than guessing from the URL.
Limits and alternatives
- In SPAs navigation may not reload the page (
history.pushState): the URL changes without a load event andcy.go()walks the app history. An assertion on the URL still works. - Going to another origin inside a test needs
cy.origin(); without it Cypress blocks access to the other origin. - In Playwright:
await expect(page).toHaveURL(/statistics/),page.goBack(),page.goForward(),page.reload().
Common mistakes
- Reading the URL in
.thenand comparing with oneexpect: without a retry the test is flaky. - Checking the query string as a whole text (
?a=1&b=2): the order of parameters may change. Preferincludefor a single parameter. - Expecting
cy.reload()to retry itself: it runs once, only the assertions after it are retried.
Check yourself
Why is cy.location('pathname') better than cy.url() for checking a page?
Answer
It does not depend on host and protocol, so it works the same locally, on staging and in production, and lets you check path and parameters separately.
What is retried in cy.go('back'); cy.url().should('include', '/dashboard')?
Answer
cy.go runs once. cy.url() is retried together with the should assertion until the URL is right or the timeout expires.
cy.url and cy.location, for history cy.go back and forward, for refreshing cy.reload. Middle/Senior: cy.url and cy.location retry together with the assertion, so they wait for navigation, while go and reload run once. I check pathname and parameters separately and avoid one-off reads in then.