2.9 KiB
title, comments
| title | comments |
|---|---|
| getCookie | true |
Get a browser cookie by it's name.
Syntax
cy.getCookie(name)
cy.getCookie(name, options)
Usage
cy.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') // Get cookie with name '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 cy.getCookie().
| Option | Default | Notes |
|---|---|---|
log |
true |
Whether to display command in Command Log |
timeout |
responseTimeout |
Total time to wait for the cy.getCookie() command to be processed |
Yields
cy.getCookie() yields a cookie object literal with the following properties:
namevaluepathdomainhttpOnlysecureexpiry
Timeout
cy.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')
Using cy.getCookie() to test logging in
{% 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:
See also
- {% url
cy.clearCookie()clearcookie %} - {% url
cy.clearCookies()clearcookies %} - Cypress Cookies API
- getCookies
- setCookie

