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

2.1 KiB

title, comments
title comments
hash false

Get the current URL hash.

{% note info %} This is an alias of {% url cy.location('hash') location %} {% endnote %}

Syntax

cy.hash()
cy.hash(options)

Usage

cy.hash() 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.hash()     // Get the url hash

Arguments

{% fa fa-angle-right %} options (Object)

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

cy.hash( options )

Option Default Description
log true {% usage_options log %}
timeout {% url defaultCommandTimeout configuration#Timeouts %} {% usage_options timeout cy.hash %}

Yields {% helper_icon yields %}

When the current URL contains a hash:

{% yields new_subject cy.hash "yields the current URL's hash (including the # character)" %}

When the current URL does not contain a hash:

{% yields new_subject cy.hash "yields an empty string" %}

Requirements {% helper_icon defaultAssertion %}

{% requirements none cy.hash %}

Timeouts {% helper_icon timeout %}

{% timeouts assertions cy.hash %}

Examples

Hash

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

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

  • {% url cy.location() location %}
  • {% url cy.url() url %}