Skip to content

Commit a48aff1

Browse files
mschiledebrisapron
authored andcommitted
docs: removing Cookies.defaults/preserveOnce (#4779)
1 parent 68cdeeb commit a48aff1

File tree

2 files changed

+8
-171
lines changed

2 files changed

+8
-171
lines changed

content/api/cypress-api/cookies.md

Lines changed: 6 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,13 @@
22
title: Cypress.Cookies
33
---
44

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-
195
`Cookies.debug()` enables you to generate logs to the console whenever any
206
cookies are modified.
217

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-
298
## Syntax
309

3110
```javascript
3211
Cypress.Cookies.debug(enable, options)
33-
Cypress.Cookies.preserveOnce(names...)
34-
Cypress.Cookies.defaults(options)
3512
```
3613

3714
### Arguments
@@ -40,14 +17,13 @@ Cypress.Cookies.defaults(options)
4017

4118
Whether cookie debugging should be enabled.
4219

43-
**<Icon name="angle-right"></Icon> names**
44-
45-
Names of cookies to be preserved. Pass an unlimited number of arguments.
46-
4720
**<Icon name="angle-right"></Icon> options** **_(Object)_**
4821

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 |
5127

5228
## Examples
5329

@@ -89,139 +65,11 @@ Debugging will be turned on until you explicitly turn it off.
8965
Cypress.Cookies.debug(false) // now debugging is turned off
9066
```
9167

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-
22168
## History
22269

22370
| Version | Changes |
22471
| --------------------------------------------- | --------------------------------------------------------------------------------------------- |
72+
| [11.0.0](/guides/references/changelog#11-0-0) | Removed `preserveOnce` and `defaults` |
22573
| [9.7.0](/guides/references/changelog#9-7-0) | Deprecated `preserveOnce` and `defaults` |
22674
| [5.0.0](/guides/references/changelog#5-0-0) | Renamed `whitelist` option to `preserve` |
22775
| [0.16.1](/guides/references/changelog#0-16-1) | `{verbose: false}` option added |

content/faq/questions/using-cypress-faq.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,8 @@ By default, Cypress automatically
723723
[clears all cookies **before** each test](/api/commands/clearcookies) to prevent
724724
state from building up.
725725

726-
You can preserve specific cookies across tests using the
727-
[Cypress.Cookies api](/api/cypress-api/cookies):
728-
729-
```javascript
730-
// now any cookie with the name 'session_id' will
731-
// not be cleared before each test runs
732-
Cypress.Cookies.defaults({
733-
preserve: 'session_id',
734-
})
735-
```
736-
737-
You **cannot** currently preserve localStorage across tests and can read more in
738-
[this issue](https://github.com/cypress-io/cypress/issues/461#issuecomment-325402086).
726+
You can preserve session details across tests using the
727+
[`cy.session()`](/api/commands/session) command.
739728

740729
## <Icon name="angle-right"></Icon> Some of my elements animate in; how do I work around that?
741730

0 commit comments

Comments
 (0)