2.4 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| url | true |
Get the current URL. cy.url() uses cy.location.href under the hood.
| Returns | the current URL as a string |
| Timeout | cannot timeout |
cy.url()
Get the current URL.
Options
Pass in an options object to change the default behavior of cy.url.
cy.url( options )
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
Usage
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()
.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 return the same string
cy.location().its("href") // these return the same string
Notes
Why is this command called url instead of 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: