0
0
Cypresstesting~20 mins

cy.location() for URL parts in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Master with cy.location()
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Cypress test snippet?

Consider the following Cypress test code that checks the URL parts using cy.location(). What will be the value of pathname logged?

Cypress
cy.visit('https://example.com/products/item1?ref=123')
  .location('pathname')
  .then(pathname => {
    cy.log(pathname)
  })
A"https://example.com/products/item1"
B"/products/item1?ref=123"
C"/products/item1"
D"example.com/products/item1"
Attempts:
2 left
💡 Hint

Remember that pathname returns only the path part of the URL, excluding domain and query parameters.

assertion
intermediate
2:00remaining
Which assertion correctly verifies the URL's query string using cy.location()?

You want to check that the current URL contains the query string ?user=admin. Which Cypress assertion is correct?

Acy.location('search').should('eq', '?user=admin')
Bcy.location('href').should('contain', '?user=admin')
Ccy.location('pathname').should('include', '?user=admin')
Dcy.location('host').should('eq', '?user=admin')
Attempts:
2 left
💡 Hint

Which part of the URL contains the query string?

🔧 Debug
advanced
2:00remaining
Why does this Cypress test fail to assert the hostname correctly?

Examine the test code below. It fails even though the URL is correct. What is the reason?

Cypress
cy.visit('https://myapp.test/dashboard')
cy.location('hostname').should('eq', 'myapp.test')
AThe test fails because cy.visit() was not chained with cy.location().
Bcy.location('hostname') returns the full URL, not just hostname.
Ccy.location('hostname') is asynchronous and needs await keyword.
DThe expected hostname has an extra slash at the end causing mismatch.
Attempts:
2 left
💡 Hint

Check the exact string returned by cy.location('hostname') and compare with the expected value.

🧠 Conceptual
advanced
2:00remaining
Which cy.location() property would you use to get the full URL including protocol, domain, path, and query?

You want to capture the entire URL string of the current page in Cypress. Which property of cy.location() gives you this?

A'origin'
B'href'
C'pathname'
D'host'
Attempts:
2 left
💡 Hint

Think about which property includes protocol, domain, path, and query.

framework
expert
2:00remaining
In a Cypress test, how do you correctly chain multiple assertions on different URL parts using cy.location()?

You want to assert that the URL's protocol is https: and the pathname is /home. Which code snippet correctly performs both assertions?

A
cy.location().should(loc => {
  expect(loc.protocol).to.eq('https:')
  expect(loc.pathname).to.eq('/home')
})
Bcy.location('protocol').should('eq', 'https:').location('pathname').should('eq', '/home')
C
cy.location('protocol').should('eq', 'https:')
cy.location('pathname').should('eq', '/home')
D
cy.location().then(loc => {
  cy.wrap(loc.protocol).should('eq', 'https:')
  cy.wrap(loc.pathname).should('eq', '/home')
})
Attempts:
2 left
💡 Hint

Consider how to access multiple properties from cy.location() in one callback.