mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-23 08:50:20 -05:00
101 lines
2.9 KiB
Markdown
101 lines
2.9 KiB
Markdown
---
|
|
title: getcookie
|
|
comments: true
|
|
description: ''
|
|
---
|
|
|
|
Get a browser cookie by it's name.
|
|
|
|
# Syntax
|
|
|
|
```javascript
|
|
.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**
|
|
|
|
```javascript
|
|
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`](https://on.cypress.io/guides/configuration#timeouts) | Total time to wait for the `.getCookie()` command to be processed
|
|
|
|
## Yields
|
|
|
|
`.getCookie()` yields a cookie object literal with the following properties:
|
|
|
|
- `name`
|
|
- `value`
|
|
- `path`
|
|
- `domain`
|
|
- `httpOnly`
|
|
- `secure`
|
|
- `expiry`
|
|
|
|
## Timeout
|
|
|
|
`.getCookie()` will continue to look for the cookie for the duration of the [`defaultCommandTimeout`](https://on.cypress.io/guides/configuration#timeouts)
|
|
|
|
# Examples
|
|
|
|
## Get Cookie
|
|
|
|
**Get `session_id` cookie after logging in**
|
|
|
|
In this example, on first login, our server sends us back a session cookie.
|
|
|
|
```javascript
|
|
// 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](https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/logging_in_html_web_form_spec.js), [logging in using XHR web forms](https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/logging_in_xhr_web_form_spec.js) and [logging in with single sign on](https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/logging_in_single_sign_on_spec.js)
|
|
{% endnote %}
|
|
|
|
# Command Log
|
|
|
|
**Get cookie**
|
|
|
|
```javascript
|
|
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
|
|
|
|
- [clearCookie](https://on.cypress.io/api/clearcookie)
|
|
- [clearCookies](https://on.cypress.io/api/clearcookies)
|
|
- [getCookies](https://on.cypress.io/api/getcookies)
|
|
- [setCookie](https://on.cypress.io/api/setcookie)
|
|
- [Cypress Cookies API](https://on.cypress.io/api/cookies)
|