title |
---|
url |
Get the current URL of the page that is currently active.
This is an alias of cy.location('href')
cy.url()
cy.url(options)
Correct Usage
cy.url() // Yields the current URL as a string
options (Object)
Pass in an options object to change the default behavior of cy.url()
.
cy.url( options )
Option | Default | Description |
---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
defaultCommandTimeout |
Time to wait for cy.url() to resolve before timing out |
cy.url()
'yields the current URL as a string' // clicking the anchor causes the browser to follow the link
cy.get('#user-edit a').click()
cy.url().should('include', '/users/1/edit') // => true
cy.url().should('eq', 'http://localhost:8000/users/1/edit') // => true
cy.url()
uses href
under the hood.
cy.url() // these yield the same string
cy.location('href') // these yield the same string
Given the remote URL, http://localhost:8000/index.html
, all 3 of these assertions are the same.
cy.location('href').should('include', '/index.html')
cy.location().its('href').should('include', '/index.html')
cy.url().should('include', '/index.html')
href
and toString
come from the window.location
spec.
But you may be wondering where the URL property comes from. Per the window.location
spec, there actually isn't a URL property on the location
object.
cy.url()
exists because it's what most developers naturally assume would return them the full current URL. We almost never refer to the URL as an href
.
Instead of hardcoding the URL you can use the baseUrl
of the Cypress configuration.
Given the remote URL, http://localhost:8000/index.html
, these assertions are the same.
cy.url().should('eq', 'http://localhost:8000/index.html')
cy.url().should('eq', Cypress.config().baseUrl + '/index.html') // tests won't fail in case the port changes
cy.url().should('contain', '#users/new')
cy.url()
requires being chained off of cy
.cy.url()
will automatically retry until all chained assertions have passedcy.url()
can time out waiting for assertions you've added to pass.The commands above will display in the Command Log as:
When clicking on URL within the Command Log, the console outputs the following:
Version | Changes |
---|---|
< 0.3.3 | cy.url() command added |