Set a browser cookie.
Syntax
cy.setCookie(name, value) cy.setCookie(name, value, options)
Usage
Correct Usage
cy.setCookie('auth_key', '123key') // Set the 'auth_key' cookie to '123key'
Arguments
name (String)
The name of the cookie to set.
value (String)
The value of the cookie to set.
options (Object)
Pass in an options object to change the default behavior of cy.setCookie()
.
Option | Default | Description |
---|---|---|
log | true | Displays the command in the Command log |
domain | window.location.hostname | The domain the cookie is visible to |
expiry | 20 years into the future | When the cookie expires, specified in seconds since Unix Epoch. |
httpOnly | false | Whether the cookie is an HTTP only cookie |
path | / | The cookie path |
secure | false | Whether the cookie is a secure cookie |
timeout | responseTimeout | Time to wait for cy.setCookie() to resolve before timing out
|
Yields
cy.setCookie()
yields a cookie object literal with the following properties:
name
value
path
domain
httpOnly
secure
expiry
Examples
Name Value
Set a cookie
cy.getCookies().should('be.empty') cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09') cy.getCookie('session_id').should('have.property', 'value', '189jd09sufh33aaiidhf99d09')
Rules
Requirements
cy.setCookie()
requires being chained off ofcy
.
Assertions
cy.setCookie()
will only run assertions you've chained once, and will not retry.
Timeouts
cy.setCookie()
should never time out.
Because
cy.setCookie()
is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.
Command Log
cy.getCookies().should('be.empty') cy.setCookie('fakeCookie1', '123ABC') cy.getCookie('fakeCookie1').should('have.property', 'value', '123ABC')
The commands above will display in the Command Log as:
When clicking on setCookie
within the command log, the console outputs the following:
History
Version | Changes |
---|---|
0.16.0 |
cy.setCookie() command added |