2.9 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| getcookie | true |
Get a browser cookie by it's name.
Syntax
.getCookie(name)
.getCookie(name, options)
Usage
.getCookie() 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.getCookie('auth_key')
Arguments
{% fa fa-angle-right %} name (String)
The name of the cookie to get.
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .getCookie().
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
responseTimeout |
Total time to wait for the .getCookie() command to be processed |
Yields
.getCookie() yields a cookie object literal with the following properties:
namevaluepathdomainhttpOnlysecureexpiry
Timeout
.getCookie() will continue to look for the cookie for the duration of the defaultCommandTimeout
Examples
Get Cookie
Get session_id cookie after logging in
In this example, on first login, our server sends us back a session cookie.
// assume we just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.getCookie('session_id')
.should('have.property', 'value', '189jd09su')
{% note info %} Check out our example recipes using cy.getCookie to test logging in using HTML web forms, logging in using XHR web forms and logging in with single sign on {% endnote %}
Command Log
Get cookie
cy.getCookie('fakeCookie1')
.should('have.property', 'value', '123ABC')
The commands above will display in the command log as:
When clicking on getCookie within the command log, the console outputs the following:

