2
2
title : Cypress.Cookies
3
3
---
4
4
5
- <Alert type =" warning " >
6
-
7
- ⚠️ ** ` Cypress.Cookies.preserveOnce() ` and ` Cypress.Cookies.defaults() ` are
8
- deprecated in Cypress 9.7.0** . In a future release, support for
9
- ` Cypress.Cookies.preserveOnce() ` and ` Cypress.Cookies.defaults() ` will be
10
- removed. Consider using the experimental [ ` cy.session() ` ] ( /api/commands/session )
11
- command instead to cache and restore cookies and other sessions details between
12
- tests.
13
-
14
- </Alert >
15
-
16
- ` Cookies.preserveOnce() ` and ` Cookies.defaults() ` enable you to control Cypress'
17
- cookie behavior.
18
-
19
5
` Cookies.debug() ` enables you to generate logs to the console whenever any
20
6
cookies are modified.
21
7
22
- Cypress automatically clears all cookies ** before** each test to prevent state
23
- from building up.
24
-
25
- You can take advantage of ` Cypress.Cookies.preserveOnce() ` or even preserve
26
- cookies by their name to preserve values across multiple tests. This enables you
27
- to preserve sessions through several tests.
28
-
29
8
## Syntax
30
9
31
10
``` javascript
32
11
Cypress .Cookies .debug (enable, options)
33
- Cypress .Cookies .preserveOnce (names... )
34
- Cypress .Cookies .defaults (options)
35
12
```
36
13
37
14
### Arguments
@@ -40,14 +17,13 @@ Cypress.Cookies.defaults(options)
40
17
41
18
Whether cookie debugging should be enabled.
42
19
43
- ** <Icon name =" angle-right " ></Icon > names**
44
-
45
- Names of cookies to be preserved. Pass an unlimited number of arguments.
46
-
47
20
** <Icon name =" angle-right " ></Icon > options** ** _ (Object)_ **
48
21
49
- Set defaults for all cookies, such as preserving a set of cookies to bypass
50
- being cleared before each test.
22
+ Pass in an options object to control the behavior of ` Cookies.debug() ` .
23
+
24
+ | option | description | default |
25
+ | ------- | --------------------------------------------------- | ------- |
26
+ | verbose | Whether or not to display the entire cookie object. | true |
51
27
52
28
## Examples
53
29
@@ -89,139 +65,11 @@ Debugging will be turned on until you explicitly turn it off.
89
65
Cypress .Cookies .debug (false ) // now debugging is turned off
90
66
```
91
67
92
- ### Preserve Once
93
-
94
- #### Preserve cookies through multiple tests
95
-
96
- Cypress gives you an interface to automatically preserve cookies for multiple
97
- tests. Cypress automatically clears all cookies before each new test starts by
98
- default.
99
-
100
- By clearing cookies before each test you are guaranteed to always start from a
101
- clean state. Starting from a clean state prevents coupling your tests to one
102
- another and prevents situations where mutating something in your application in
103
- one test affects another one downstream.
104
-
105
- <Alert type =" info " >
106
-
107
- The most common use case for preserving cookies is to prevent having to log in
108
- to your application before each individual test. This is a problem if the
109
- majority of each test is spent logging in a user.
110
-
111
- </Alert >
112
-
113
- You can use ` Cypress.Cookies.preserveOnce() ` to preserve cookies through
114
- multiple tests.
115
-
116
- There are _ likely_ better ways to do this, but this isn't well documented at the
117
- moment. Every application is different and there is no one-size-fits-all
118
- solution. For the moment, if you're using session-based cookies, this method
119
- will work.
120
-
121
- ``` javascript
122
- describe (' Dashboard' , () => {
123
- before (() => {
124
- // log in only once before any of the tests run.
125
- // your app will likely set some sort of session cookie.
126
- // you'll need to know the name of the cookie(s), which you can find
127
- // in your Resources -> Cookies panel in the Chrome Dev Tools.
128
- cy .login ()
129
- })
130
-
131
- beforeEach (() => {
132
- // before each test, we can automatically preserve the
133
- // 'session_id' and 'remember_token' cookies. this means they
134
- // will not be cleared before the NEXT test starts.
135
- //
136
- // the name of your cookies will likely be different
137
- // this is an example
138
- Cypress .Cookies .preserveOnce (' session_id' , ' remember_token' )
139
- })
140
-
141
- it (' displays stats' , () => {
142
- // ...
143
- })
144
-
145
- it (' can do something' , () => {
146
- // ...
147
- })
148
-
149
- it (' opens a modal' , () => {
150
- // ...
151
- })
152
- })
153
- ```
154
-
155
- ### Defaults
156
-
157
- #### Set global default cookies
158
-
159
- You can modify the global defaults and preserve a set of Cookies which will
160
- always be preserved across tests.
161
-
162
- Any change you make here will take effect immediately for the remainder of every
163
- single test.
164
-
165
- <Alert type =" info " >
166
-
167
- :: include { file =partials/support-file-configuration.md }
168
-
169
- </Alert >
170
-
171
- #### ` preserve ` accepts:
172
-
173
- - String
174
- - Array
175
- - RegExp
176
- - Function
177
-
178
- #### Preserve String
179
-
180
- ``` javascript
181
- // now any cookie with the name 'session_id' will
182
- // not be cleared before each test runs
183
- Cypress .Cookies .defaults ({
184
- preserve: ' session_id' ,
185
- })
186
- ```
187
-
188
- #### Preserve Array
189
-
190
- ``` javascript
191
- // now any cookie with the name 'session_id' or 'remember_token'
192
- // will not be cleared before each test runs
193
- Cypress .Cookies .defaults ({
194
- preserve: [' session_id' , ' remember_token' ],
195
- })
196
- ```
197
-
198
- #### Preserve RegExp
199
-
200
- ``` javascript
201
- // now any cookie that matches this RegExp
202
- // will not be cleared before each test runs
203
- Cypress .Cookies .defaults ({
204
- preserve: / session| remember/ ,
205
- })
206
- ```
207
-
208
- #### Preserve Function
209
-
210
- ``` javascript
211
- Cypress .Cookies .defaults ({
212
- preserve : (cookie ) => {
213
- // implement your own logic here
214
- // if the function returns truthy
215
- // then the cookie will not be cleared
216
- // before each test runs
217
- },
218
- })
219
- ```
220
-
221
68
## History
222
69
223
70
| Version | Changes |
224
71
| --------------------------------------------- | --------------------------------------------------------------------------------------------- |
72
+ | [ 11.0.0] ( /guides/references/changelog#11-0-0 ) | Removed ` preserveOnce ` and ` defaults ` |
225
73
| [ 9.7.0] ( /guides/references/changelog#9-7-0 ) | Deprecated ` preserveOnce ` and ` defaults ` |
226
74
| [ 5.0.0] ( /guides/references/changelog#5-0-0 ) | Renamed ` whitelist ` option to ` preserve ` |
227
75
| [ 0.16.1] ( /guides/references/changelog#0-16-1 ) | ` {verbose: false} ` option added |
0 commit comments