cy.location() for URL parts in Cypress - Build an Automation Script
describe('Login and verify URL parts', () => { it('should login and check URL parts using cy.location()', () => { cy.visit('https://example.com/login'); cy.get('#username').type('testuser'); cy.get('#password').type('Test@1234'); cy.get('#loginBtn').click(); // Verify protocol cy.location('protocol').should('eq', 'https:'); // Verify hostname cy.location('hostname').should('eq', 'example.com'); // Verify pathname cy.location('pathname').should('eq', '/dashboard'); // Verify search query cy.location('search').should('eq', '?welcome=true'); }); });
This test script uses Cypress to automate the login process and verify URL parts.
First, it visits the login page URL.
Then it types the username and password into inputs identified by their IDs.
After clicking the login button, Cypress waits automatically for navigation.
Using cy.location() with specific properties, it checks the protocol, hostname, pathname, and search query string.
Assertions use should('eq', ...) to confirm each URL part matches the expected value.
This approach avoids hardcoded waits and uses clear selectors and assertions for maintainability and readability.
Now add data-driven testing with 3 different sets of valid usernames and passwords, verifying URL parts after each login.
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
