Files
cypress/docs/source/api/commands/url.md
2017-05-31 15:36:33 -04:00

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:

screen shot 2015-11-29 at 1 42 40 pm

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

screen shot 2015-11-29 at 1 42 52 pm

See also