What if your tests could instantly catch broken links without you lifting a finger?
Why cy.url() assertions in Cypress? - Purpose & Use Cases
Imagine you manually check if your website navigates to the right page by opening the browser, clicking links, and reading the URL in the address bar every time you make a change.
This manual checking is slow and tiring. You might miss small typos or forget to check the URL after every action. It's easy to make mistakes and hard to repeat the checks exactly the same way each time.
Using cy.url() assertions in Cypress lets you automatically check the current page URL during tests. It quickly confirms if navigation works as expected without opening the browser or reading the address bar yourself.
Open browser
Click link
Look at address bar
Say if URL is correctcy.url().should('include', '/dashboard')
This makes your tests faster, more reliable, and able to catch navigation errors automatically every time you run them.
When you build an online store, you want to be sure clicking "Checkout" always takes users to the payment page. cy.url() assertions check this automatically so you don't lose customers due to broken links.
Manual URL checks are slow and error-prone.
cy.url() assertions automate URL verification in tests.
This ensures navigation works correctly every time you test.
Practice
cy.url().should('include', '/dashboard') check in a Cypress test?Solution
Step 1: Understand the 'include' assertion
The 'include' assertion checks if the given string is part of the actual URL anywhere.Step 2: Apply to the example
Using 'include' with '/dashboard' means the URL must contain '/dashboard' anywhere, not necessarily exactly or at start/end.Final Answer:
It checks if the current URL contains '/dashboard' anywhere in it. -> Option AQuick Check:
cy.url().should('include', '/dashboard') = contains '/dashboard' [OK]
- Confusing 'include' with exact match
- Thinking 'include' checks start or end only
- Using 'eq' when 'include' is needed
Solution
Step 1: Identify exact match assertion
The 'eq' assertion checks if the actual URL exactly equals the given string.Step 2: Match syntax to requirement
Using cy.url().should('eq', 'https://example.com/home') asserts the URL is exactly that string.Final Answer:
cy.url().should('eq', 'https://example.com/home') -> Option DQuick Check:
Exact URL match uses 'eq' [OK]
- Using 'include' instead of 'eq' for exact match
- Using 'match' without regex
- Using 'contain' which is not a valid Cypress assertion
cy.visit('/login')
cy.url().should('match', /\/login$/)What will the assertion check?
Solution
Step 1: Understand the 'match' assertion with regex
The 'match' assertion checks if the URL matches the given regular expression.Step 2: Analyze the regex /\/login$/
The regex ends with '$' which means the URL must end with '/login'.Final Answer:
The URL ends with '/login'. -> Option BQuick Check:
Regex with $ means URL ends with pattern [OK]
- Thinking 'match' checks substring anywhere without regex
- Confusing regex anchors like ^ and $
- Assuming 'match' equals exact string match
cy.url().should('equals', 'https://example.com/home')Solution
Step 1: Check valid assertion keywords for cy.url()
Cypress supports 'eq', 'include', 'match', but not 'equals'.Step 2: Identify the invalid keyword usage
Using 'equals' causes a syntax error because it's not recognized by Cypress.Final Answer:
'equals' is not a valid assertion keyword in Cypress. -> Option AQuick Check:
Use 'eq' for exact match, not 'equals' [OK]
- Using 'equals' instead of 'eq'
- Confusing assertion keywords
- Assuming 'should' can't be chained with 'url'
Solution
Step 1: Understand the URL with query parameters
The URL may have extra parts after '/dashboard', so exact match ('eq') won't work.Step 2: Choose assertion allowing partial match
'include' checks if '/dashboard' is anywhere in the URL, ignoring query parameters.Final Answer:
cy.url().should('include', '/dashboard') -> Option CQuick Check:
Use 'include' to ignore query parameters [OK]
- Using 'eq' which fails with query parameters
- Trying complex regex without need
- Using invalid 'contain' assertion
Ready to prove it?
Pass the test and this pattern moves unknown
