2.7 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| url | true |
Get the current URL.
{% note info %}
This is the same as cy.location.href
{% endnote %}
Syntax
cy.url()
cy.url(options)
Usage
cy.url() 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.url()
Arguments
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.url().
cy.hash( options )
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
Yields
.url() yields the current URL as a string.
Timeout
Examples
Url
Assert the URL is http://localhost:8000/users/1/edit
// clicking the anchor causes the browser to follow the link
cy.get('#user-edit a').click()
cy.url().should('eq', 'http://localhost:8000/users/1/edit') // => true
Url is a shortcut for cy.location().href
cy.url() uses href under the hood.
cy.url() // these yield the same string
cy.location().its('href') // these yield the same string
Url versus href
Given the remote URL, http://localhost:8000/index.html, all 3 of these assertions are the same.
cy.location().its('href').should('eq', 'http://localhost:8000/index.html')
cy.location().invoke('toString').should('eq', 'http://localhost:8000/index.html')
cy.url().should('eq', 'http://localhost:8000/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.
Command Log
Assert that the url contains "#users/new"
cy.url().should('contain', '#users/new')
The commands above will display in the command log as:
When clicking on url within the command log, the console outputs the following: