| title | url |
|---|
Get the current URL of the page that is currently active.
{% note info %}
This is an alias of {% url "cy.location('href')" location %}
{% endnote %}
cy.url()
cy.url(options){% fa fa-check-circle green %} Correct Usage
cy.url() // Yields the current URL as a string{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.url().
cy.url( options )
| Option | Default | Description |
|---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout cy.url %} |
{% yields sets_subject 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') // => truecy.url() uses href under the hood.
cy.url() // these yield the same string
cy.location('href') // these yield the same stringGiven 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 {% url 'Cypress configuration' 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 changescy.url().should('contain', '#users/new'){% requirements parent cy.url %}
{% assertions retry cy.url %}
{% timeouts assertions cy.url %}
The commands above will display in the Command Log as:
{% imgTag /img/api/url/test-url-of-website-or-web-application.png "Command Log url" %}
When clicking on URL within the Command Log, the console outputs the following:
{% imgTag /img/api/url/console-log-of-browser-url-string.png "Console Log url" %}
{% history %}
{% url "< 0.3.3" changelog#0-3-3 %} | cy.url() command added
{% endhistory %}
- {% url
cy.hash()hash %} - {% url
cy.location()location %}