Files
cypress/docs/source/api/commands/hash.md
T

2.1 KiB

title, comments, description
title comments description
hash true

Get the current URL hash. This is the same as cy.location().hash

Returns the current URL hash as a string, including the # character. If no # character is present, an empty string will be returned.
Timeout cannot timeout

cy.hash()

Get the current URL hash.

Options

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

cy.hash( options ) cy.hash( options )

Option Default Notes
log true whether to display command in command log

Usage

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

// Hash returns #/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")
  .hash().should("match", /users\/.+$/) // => true

Notes

Hash is a shortcut for cy.location().hash

These 3 assertions are all the same.

// 1. verbose
cy.location().then(function(location){
  expect(location.hash).to.eq("#/users/1")
})

// 2. better
cy.location().its("hash").should("eq", "#/users/1")

// 3. best
cy.hash().should("eq", "#/users/1")

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:

screen shot 2015-11-29 at 1 34 12 pm

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

screen shot 2015-11-29 at 1 34 17 pm

Related