Files
cypress/docs/source/api/commands/getcookie.md
T
2017-05-25 17:57:56 -04:00

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:
![screen shot 2016-05-10 at 12 12 13 pm](https://cloud.githubusercontent.com/assets/1271364/15153750/7a1caa40-16a8-11e6-9f70-3858dacb6792.png)
When clicking on `getCookie` within the command log, the console outputs the following:
![screen shot 2016-05-10 at 12 12 05 pm](https://cloud.githubusercontent.com/assets/1271364/15153749/7a18b00c-16a8-11e6-86ad-ea969f46bb6c.png)
# 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)