Confirming navigation happened correctly is one of the most common assertions in end-to-end testing. This lesson covers cy.url() and cy.location() for checking the current page after an action.
cy.url() and cy.location()
cy.url() yields the full current URL as a string, ideal for straightforward checks like confirming a redirect happened. cy.location() yields a parsed location object, useful when you need to check a specific part of the URL, like just the pathname or query string, independently.
Because navigation is often asynchronous (waiting on a network request, then a client-side route change), URL assertions benefit heavily from automatic retrying, the assertion simply waits until the URL actually changes, rather than needing an explicit delay after the triggering click.
cy.get('[data-cy="login-submit"]').click();
cy.url().should('include', '/dashboard'); // waits as long as needed, up to the timeout
Testing Query Parameters Precisely
For pages driven by query parameters (filters, pagination, tabs), asserting on cy.location('search') is more precise than checking the full URL string, since it isolates just the part of the URL that actually encodes application state.
Asserting on the full URL with eq in environments where the domain or port varies (like different local ports), causing unnecessary environment coupling.
Adding a manual wait before a URL assertion instead of letting the automatic retry handle timing.
Ignoring query parameters entirely when they represent meaningful, testable application state (filters, sorting, pagination).
Using include when an exact match with eq would catch unintended extra path segments.
Key Takeaways
cy.url() yields the full URL; cy.location(key) yields a specific parsed part.
URL assertions retry automatically, so no manual wait is needed after a navigation-triggering action.
Prefer cy.location('search') for precise query-parameter assertions over parsing the full URL manually.
Choose include versus eq deliberately based on how precise the check needs to be.
Pro Tip
When testing a page driven by query parameters (filters, sort order, pagination), assert on cy.location('search') directly rather than the whole URL, it keeps the test focused on the actual state that matters and ignores irrelevant host/port differences across environments.
You now know how to reliably assert on URLs and locations. Next, apply assertions specifically to Form Assertions.