| title | location |
|---|
Get the global window.location object of the page that is currently active.
cy.location()
cy.location(key)
cy.location(options)
cy.location(key, options){% fa fa-check-circle green %} Correct Usage
cy.location() // Get location object
cy.location('host') // Get the host of the location object
cy.location('port') // Get the port of the location object{% fa fa-angle-right %} key (String)
A key on the location object. Returns this value instead of the full location object.
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.location().
| Option | Default | Description |
|---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout cy.location %} |
cy.location() yields the location object with the following properties:
hashhosthostnamehreforiginpathnameportprotocolsearchtoString
{% yields sets_subject cy.location 'yields the value of the location property as a string' %}
cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
cy.location().should((loc) => {
expect(loc.hash).to.eq('#/users/123/edit')
expect(loc.host).to.eq('localhost:8000')
expect(loc.hostname).to.eq('localhost')
expect(loc.href).to.eq('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
expect(loc.origin).to.eq('http://localhost:8000')
expect(loc.pathname).to.eq('/app/index.html')
expect(loc.port).to.eq('8000')
expect(loc.protocol).to.eq('http:')
expect(loc.search).to.eq('?q=dan')
expect(loc.toString()).to.eq('http://localhost:8000/app/index.html?q=brian#/users/123/edit')
})We can yield the location object within a {% url .should() should %} command and work with it directly.
cy.get('#search').type('niklas{enter}')
cy.location().should((loc) => {
expect(loc.search).to.eq('?search=niklas')
expect(loc.pathname).to.eq('/users')
})Grab only the pathname and add an assertion.
cy.visit('http://localhost:3000/admin')
cy.location('pathname').should('eq', '/login')Cypress automatically normalizes the cy.location() command and strips out extraneous values and properties found in window.location. Also, the object literal yielded by cy.location() is a basic object literal, not the special window.location object.
When changing properties on the real window.location object, it forces the browser to navigate away. In Cypress, the object yielded is a plain object, so changing its properties will have no effect on navigation.
cy.window().then((win) => {
console.log(win.location)
}){% imgTag /img/api/location/window-location-object-printed-in-console-log.png "Console.log of window.location" %}
cy.location().then((loc) => {
console.log(loc)
}){% imgTag /img/api/location/special-cypress-location-object-logged-in-console-output.png "Console Log of Cypress location command" %}
{% requirements parent cy.location %}
{% assertions retry cy.location %}
{% timeouts assertions cy.location %}
Assert on the location's href
cy.location().should((loc) => {
expect(loc.href).to.include('commands/querying')
})The commands above will display in the Command Log as:
{% imgTag /img/api/location/make-assertion-about-location-url-in-tests.png "Command Log of Cypress location command" %}
When clicking on location within the command log, the console outputs the following:
{% imgTag /img/api/location/location-object-in-console-log.png "Console Log of Cypress location command" %}
- {% url
cy.hash()hash %} - {% url
cy.url()url %}