W3cubDocs

/Cypress

hash

Get the current URL hash of the page that is currently active.

This is an alias of cy.location('hash')

Syntax

cy.hash()
cy.hash(options)

Usage

Correct Usage

cy.hash()     // Get the url hash

Arguments

options (Object)

Pass in an options object to change the default behavior of cy.hash().

cy.hash( options )

Option Default Description
log true Displays the command in the Command log
timeout defaultCommandTimeout Time to wait for cy.hash() to resolve before timing out

Yields

When the current URL contains a hash:

  • cy.hash() yields the current URL’s hash (including the # character).

When the current URL does not contain a hash:

  • cy.hash() yields an empty string.

Examples

No Args

Assert that hash is #/users/1 given remote URL: http://localhost:8000/app/#/users/1

// yields #/users/1
cy.hash().should('eq', '#/users/1') // => true

Assert that the hash matches via RegExp

<ul id="users">
  <li>
    <a href="#/users/8fc45b67-d2e5-465a-b822-b281d9c8e4d1">Fred</a>
  </li>
</ul>
cy.get('#users li').find('a').click()
cy.hash().should('match', /users\/.+$/) // => true

Rules

Requirements

  • cy.hash() requires being chained off of cy.

Assertions

  • cy.hash() will automatically retry until assertions you've chained all pass.

Timeouts

  • cy.hash() can time out waiting for assertions you've added to pass.

Command Log

Assert that the hash matches #users/new

cy.hash().should('eq', '#users/new')

The commands above will display in the Command Log as:

Command Log for hash

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

Console Log for hash

See also

© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/hash.html