We use cy.location() to check parts of the web page URL during tests. It helps us make sure the page is where we expect it to be.
cy.location() for URL parts in Cypress
cy.location([options]) // options can include { timeout: number } // To get a specific part of the URL: cy.location('pathname') cy.location('hostname') cy.location('protocol') cy.location('search') cy.location('hash')
cy.location() returns a Location object representing the current URL.
You can pass a string to get a specific part like 'pathname' or 'hostname'.
cy.location().should((loc) => {
expect(loc.pathname).to.eq('/home')
})cy.location('hostname').should('eq', 'example.com')
cy.location('protocol').should('eq', 'https:')
cy.location('search').should('include', '?q=testing')
This test visits a URL and checks that the path is '/home' and the protocol is 'https:'.
describe('URL parts test', () => { it('checks URL pathname and protocol', () => { cy.visit('https://example.com/home?user=123') cy.location('pathname').should('eq', '/home') cy.location('protocol').should('eq', 'https:') }) })
Use cy.location() inside a test after navigation or actions that change the URL.
Remember that cy.location() commands are asynchronous and return chainable Cypress objects.
Use assertions like .should() to verify URL parts.
cy.location() helps check parts of the current URL in tests.
You can get specific parts like pathname, hostname, protocol, search, and hash.
Use it to confirm navigation and URL changes work as expected.
Practice
cy.location('pathname') return in a Cypress test?Solution
Step 1: Understand
Thecy.location()usagecy.location()command gets parts of the current URL in Cypress tests.Step 2: Identify what 'pathname' returns
The 'pathname' is the part of the URL after the domain, showing the path like/homeor/about.Final Answer:
The path part of the current URL, like /home -> Option AQuick Check:
cy.location('pathname') = path part [OK]
- Confusing 'pathname' with full URL
- Thinking 'pathname' returns query parameters
- Mixing 'hostname' with 'pathname'
cy.location()?Solution
Step 1: Recall correct property names for URL parts
The correct property to get the domain name is 'hostname', not 'host' or 'domain'.Step 2: Check syntax for
The syntax iscy.location()cy.location('hostname')to get the hostname string.Final Answer:
cy.location('hostname') -> Option AQuick Check:
Use 'hostname' string inside cy.location() [OK]
- Using 'host' instead of 'hostname'
- Calling cy.location as a function property
- Using 'domain' which is invalid
https://example.com/products?item=123#details, what will cy.location('search') return?Solution
Step 1: Identify the 'search' part of the URL
The 'search' part is the query string starting with?, here it is?item=123.Step 2: Confirm what
This command returns the query string including thecy.location('search')returns?symbol.Final Answer:
?item=123 -> Option DQuick Check:
cy.location('search') = query string [OK]
- Confusing 'search' with 'hash'
- Returning path instead of query string
- Omitting the leading '?' in the answer
cy.location('hash').should('eq', 'details') to check the URL hash #details. Why does this test fail?Solution
Step 1: Understand what
It returns the hash part of the URL including thecy.location('hash')returns#symbol, e.g.,#details.Step 2: Check the assertion value
The test compares to'details'without#, so it fails because the strings don't match exactly.Final Answer:
The hash value includes the # symbol, so it should be '#details' -> Option CQuick Check:
Hash includes '#' in cy.location('hash') [OK]
- Forgetting '#' in hash comparison
- Confusing hash with pathname
- Using wrong assertion method
https://shop.com/cart?item=5#checkout. Which chained Cypress commands correctly check the protocol, pathname, search, and hash parts?Solution
Step 1: Understand how to check multiple URL parts with cy.location()
Each call tocy.location()returns a promise, so you call them separately for each part.Step 2: Check the correctness of values and chaining
cy.location('protocol').should('eq', 'https:') cy.location('pathname').should('eq', '/cart') cy.location('search').should('eq', '?item=5') cy.location('hash').should('eq', '#checkout') callscy.location()separately for each part with correct expected values including ':' in protocol and leading '/' in pathname.Step 3: Identify errors in other options
cy.location('protocol').should('eq', 'https:') .location('pathname').should('eq', '/cart') .location('search').should('eq', '?item=5') .location('hash').should('eq', '#checkout') chainslocation()calls incorrectly (no chaining support). cy.location('protocol').should('eq', 'https') cy.location('pathname').should('eq', 'cart') cy.location('search').should('eq', 'item=5') cy.location('hash').should('eq', 'checkout') misses ':' and leading characters. cy.location().should('eq', 'https://shop.com/cart?item=5#checkout') compares full URL string which is not supported bycy.location().Final Answer:
Separate calls to cy.location() with correct expected values -> Option BQuick Check:
Use separate cy.location() calls for each URL part [OK]
- Trying to chain cy.location() calls
- Missing ':' in protocol value
- Omitting leading '/' in pathname
- Comparing full URL string with cy.location()
Ready to prove it?
Pass the test and this pattern moves unknown
