Files
cypress/docs/source/api/commands/location.md
T
Jennifer Shehane cbec33d637 Converted 'its' - 'pause'
- Updated some existing api docs to reflect structure changes
- Added missing ‘pause’ command to sidebar and .yml english.
2017-05-26 14:31:41 -04:00

4.0 KiB

title, comments, description
title comments description
location true

Get the remote window.location as an object.

Syntax

.location()
.location(key)
.location(options)
.location(key, options)

Usage

.location() cannot be chained off any other cy commands, so should be chained off of cy for clarity.

{% fa fa-check-circle green %} Valid Usage

cy.location()    

Arguments

{% fa fa-angle-right %} key (String)

A key on the location object.

{% fa fa-angle-right %} options (Object)

Pass in an options object to change the default behavior of .location().

Option Default Notes
log true whether to display command in command log

Yields

.location() yields the location object with the following properties:

  • hash
  • host
  • hostname
  • href
  • origin
  • pathname
  • port
  • protocol
  • search
  • toString

Timeout

Examples

Location Properties

Make assertions about every location property

cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')

cy.location().should(function(location) {
  expect(location.hash).to.eq('#/users/123/edit')
  expect(location.host).to.eq('localhost:8000')
  expect(location.hostname).to.eq('localhost')
  expect(location.href).to.eq('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
  expect(location.origin).to.eq('http://localhost:8000')
  expect(location.pathname).to.eq('/app.index.html')
  expect(location.port).to.eq('8000')
  expect(location.protocol).to.eq('http:')
  expect(location.search).to.eq('?q=dan')
  expect(location.toString()).to.eq('http://localhost:8000/app/index.html?q=brian#/users/123/edit')
})

Check location for query params and pathname

// we can yield the location subject and work with
// it directly as an object
cy.get('#search').type('niklas{enter}')
cy.location().should(function(location){
  expect(location.search).to.eq('?search=niklas')
  expect(location.pathname).to.eq('/users')
})

Key

Assert that a redirect works

cy
  .visit('http://localhost:3000/admin')
  .location('pathname').should('eq', '/login')

Notes

No need to use window.location

Cypress automatically normalizes the .location() command and strips out extraneous values and properties found in window.location. Also, the object literal yielded by .location() is just 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.

Console output of window.location

cy.window().then(function(window){
  console.log(window.location)
})
screen shot 2017-05-26 at 11 41 27 am

Console output of .location()

cy.location().then(function(location){
  console.log(location)
})
screen shot 2017-05-26 at 11 42 11 am

Command Log

Assert on the location's href

cy.location().should(function(location){
  expect(location.href).to.include('commands/querying')
})

The commands above will display in the command log as:

screen shot 2017-03-09 at 1 54 22 pm

When clicking on location within the command log, the console outputs the following:

screen shot 2017-03-09 at 1 54 58 pm

See also