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
#/users/1
given remote URL: http://localhost:8000/app/#/users/1
Assert that hash is // 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 ofcy
.
Assertions
-
cy.hash()
will automatically retry until all chained assertions have passed
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](/_nuxt/img/test-url-hash-for-users-page.c2c5efa.png)
When clicking on hash
within the command log, the console outputs the
following:
![Console Log for hash](/_nuxt/img/hash-command-yields-url-after-hash.2c2ab88.png)