Bird
Raised Fist0
Cypresstesting~20 mins

cy.url() assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
URL Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check if URL contains a specific path segment
You want to verify that the current page URL contains the segment /dashboard. Which assertion correctly checks this using cy.url()?
Acy.url().should('include', '/dashboard')
Bcy.url().should('have', '/dashboard')
Ccy.url().should('contain', '/dashboard')
Dcy.url().should('equal', '/dashboard')
Attempts:
2 left
💡 Hint
Think about the correct assertion keyword to check if a string is part of the URL.
❓ assertion
intermediate
2:00remaining
Verify URL equals expected full URL
You want to assert that the current URL exactly matches https://example.com/login. Which option is correct?
Acy.url().should('match', 'https://example.com/login')
Bcy.url().should('eq', 'https://example.com/login')
Ccy.url().should('equals', 'https://example.com/login')
Dcy.url().should('equal', 'https://example.com/login')
Attempts:
2 left
💡 Hint
Look for the exact assertion keyword Cypress supports for equality.
❓ Predict Output
advanced
2:00remaining
What is the test result of this URL assertion?
Given the current URL is https://example.com/profile/settings, what will be the result of this test code?
Cypress
cy.url().should('include', '/profile').and('include', '/settings')
ATest fails because 'and' cannot chain assertions
BTest passes because both substrings are in the URL
CTest fails because 'include' only works once per assertion
DTest passes but only checks the first substring
Attempts:
2 left
💡 Hint
Think about how chaining assertions with 'and' works in Cypress.
🔧 Debug
advanced
2:00remaining
Identify the error in this URL assertion code
This test code is intended to check if the URL starts with https://example.com/shop. What is wrong with it?
Cypress
cy.url().should('startWith', 'https://example.com/shop')
AThe assertion keyword 'startWith' is invalid in Cypress
BThe URL must be wrapped in a regex for this to work
CThe method 'should' cannot be used with 'startWith'
DThe URL must be converted to lowercase first
Attempts:
2 left
💡 Hint
Check Cypress documentation for valid assertion keywords.
🧠 Conceptual
expert
3:00remaining
Best practice for asserting dynamic URL parameters
You want to assert that the current URL contains the path /product and a query parameter id with any value. Which approach is best to write a reliable test?
AUse <code>cy.url().should('equal', '/product?id=123')</code> with a fixed id value
BUse <code>cy.url().should('include', '/product?id=')</code> to check the substring
CUse <code>cy.url().should('match', /\/product\?id=\w+/)</code> to match the pattern with regex
DUse <code>cy.url().should('contain', '/product').and('contain', 'id=')</code> to check both parts separately
Attempts:
2 left
💡 Hint
Think about how to handle dynamic values in URLs during assertions.

Practice

(1/5)
1. What does cy.url().should('include', '/dashboard') check in a Cypress test?
easy
A. It checks if the current URL contains '/dashboard' anywhere in it.
B. It verifies the URL exactly equals '/dashboard'.
C. It asserts the URL starts with '/dashboard'.
D. It confirms the URL ends with '/dashboard'.

Solution

  1. Step 1: Understand the 'include' assertion

    The 'include' assertion checks if the given string is part of the actual URL anywhere.
  2. Step 2: Apply to the example

    Using 'include' with '/dashboard' means the URL must contain '/dashboard' anywhere, not necessarily exactly or at start/end.
  3. Final Answer:

    It checks if the current URL contains '/dashboard' anywhere in it. -> Option A
  4. Quick 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
A. cy.url().should('contain', 'https://example.com/home')
B. cy.url().should('include', 'https://example.com/home')
C. cy.url().should('match', 'https://example.com/home')
D. cy.url().should('eq', 'https://example.com/home')

Solution

  1. Step 1: Identify exact match assertion

    The 'eq' assertion checks if the actual URL exactly equals the given string.
  2. Step 2: Match syntax to requirement

    Using cy.url().should('eq', 'https://example.com/home') asserts the URL is exactly that string.
  3. Final Answer:

    cy.url().should('eq', 'https://example.com/home') -> Option D
  4. Quick 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:
cy.visit('/login')
cy.url().should('match', /\/login$/)

What will the assertion check?
medium
A. The URL contains '/login' anywhere.
B. The URL ends with '/login'.
C. The URL exactly equals '/login'.
D. The URL starts with '/login'.

Solution

  1. Step 1: Understand the 'match' assertion with regex

    The 'match' assertion checks if the URL matches the given regular expression.
  2. Step 2: Analyze the regex /\/login$/

    The regex ends with '$' which means the URL must end with '/login'.
  3. Final Answer:

    The URL ends with '/login'. -> Option B
  4. Quick 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
A. 'equals' is not a valid assertion keyword in Cypress.
B. The URL string is missing a trailing slash.
C. The method 'should' cannot be used with 'url'.
D. The URL must be checked with 'include' instead of 'equals'.

Solution

  1. Step 1: Check valid assertion keywords for cy.url()

    Cypress supports 'eq', 'include', 'match', but not 'equals'.
  2. Step 2: Identify the invalid keyword usage

    Using 'equals' causes a syntax error because it's not recognized by Cypress.
  3. Final Answer:

    'equals' is not a valid assertion keyword in Cypress. -> Option A
  4. Quick 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
A. cy.url().should('eq', 'https://example.com/dashboard')
B. cy.url().should('contain', '/dashboard?user=123')
C. cy.url().should('include', '/dashboard')
D. cy.url().should('match', /^https:\/\/example\.com\/dashboard\?user=\d+$/)

Solution

  1. Step 1: Understand the URL with query parameters

    The URL may have extra parts after '/dashboard', so exact match ('eq') won't work.
  2. Step 2: Choose assertion allowing partial match

    'include' checks if '/dashboard' is anywhere in the URL, ignoring query parameters.
  3. Final Answer:

    cy.url().should('include', '/dashboard') -> Option C
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard