We use cy.url() assertions to check if the web page's address is correct during tests. This helps us make sure the user is on the right page.
cy.url() assertions in Cypress
Introduction
Syntax
Cypress
cy.url().should('include', 'expected-part') cy.url().should('eq', 'https://example.com/page') cy.url().should('match', /regex/)
cy.url() gets the current page URL as a string.
Use .should() with assertions like include, eq, or match to check the URL.
Examples
Cypress
cy.url().should('include', '/dashboard')
Cypress
cy.url().should('eq', 'https://example.com/login')
Cypress
cy.url().should('match', /\/products\/\d+/)Sample Program
This test visits the homepage, clicks the login link, and asserts the URL includes '/login'.
Cypress
describe('URL assertion test', () => { it('checks URL after clicking login', () => { cy.visit('https://example.com') cy.get('a.login-link').click() cy.url().should('include', '/login') }) })
Important Notes
Always use clear and specific URL parts to avoid false positives.
Use cy.url() assertions to catch navigation errors early.
Combine with other assertions like checking page content for stronger tests.
Summary
cy.url() assertions help confirm the browser is on the right page by checking the URL.
Use .should() with include, eq, or match for flexible checks.
They are simple but powerful for testing navigation and redirects.
Practice
1. What does
cy.url().should('include', '/dashboard') check in a Cypress test?easy
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]
Hint: Remember 'include' means substring anywhere in URL [OK]
Common Mistakes:
- Confusing 'include' with exact match
- Thinking 'include' checks start or end only
- Using 'eq' when 'include' is needed
2. Which of the following is the correct syntax to assert the URL exactly equals 'https://example.com/home' in Cypress?
easy
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]
Hint: Use 'eq' for exact URL match, not 'include' or 'match' [OK]
Common Mistakes:
- Using 'include' instead of 'eq' for exact match
- Using 'match' without regex
- Using 'contain' which is not a valid Cypress assertion
3. Given the code snippet:
What will the assertion check?
cy.visit('/login')
cy.url().should('match', /\/login$/)What will the assertion check?
medium
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]
Hint: Regex ending with $ means URL ends with that string [OK]
Common Mistakes:
- Thinking 'match' checks substring anywhere without regex
- Confusing regex anchors like ^ and $
- Assuming 'match' equals exact string match
4. Identify the error in this Cypress assertion:
cy.url().should('equals', 'https://example.com/home')medium
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]
Hint: Use 'eq' not 'equals' for exact URL check [OK]
Common Mistakes:
- Using 'equals' instead of 'eq'
- Confusing assertion keywords
- Assuming 'should' can't be chained with 'url'
5. You want to verify that after login, the URL contains '/dashboard' but may have query parameters like '?user=123'. Which assertion is best to use?
hard
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]
Hint: Use 'include' to ignore query parameters in URL check [OK]
Common Mistakes:
- Using 'eq' which fails with query parameters
- Trying complex regex without need
- Using invalid 'contain' assertion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
