Skip to content

Commit 065c956

Browse files
Cypress.Session Cypress API (#4900)
* docs around Cypress.session api * data not date * Update content/api/cypress-api/session.md Co-authored-by: Matt Henkes <[email protected]> * Update content/api/cypress-api/session.md Co-authored-by: Matt Henkes <[email protected]> * Update content/api/cypress-api/session.md Co-authored-by: Matt Henkes <[email protected]> * Update content/api/cypress-api/session.md Co-authored-by: Matt Henkes <[email protected]> * fix markdown * Update content/api/cypress-api/session.md * Apply suggestions from code review Co-authored-by: Matt Henkes <[email protected]>
1 parent 31cdf70 commit 065c956

File tree

1 file changed

+92
-5
lines changed

1 file changed

+92
-5
lines changed

content/api/cypress-api/session.md

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,109 @@
22
title: Cypress.session
33
---
44

5-
`Cypress.session` is a collection of session-related helper methods intended to
6-
be used alongside the [`cy.session()`](/api/commands/session) command.
5+
`Cypress.session` is a collection of async session-related helper methods
6+
intended to be used alongside the [`cy.session()`](/api/commands/session)
7+
command.
78

89
## Syntax
910

10-
Clear all saved sessions and re-run the current spec file.
11-
1211
```javascript
12+
// Clear all sessions saved on the backend, including cached global sessions.
1313
Cypress.session.clearAllSavedSessions()
14+
// Clear all storage and cookie date across all origins associated with the current session.
15+
Cypress.session.clearCurrentSessionData()
16+
// Get all storage and cookie data across all origins associated with the current session.
17+
Cypress.session.getCurrentSessionData()
18+
// Get all storage and cookie data saved on the backend associated with the provided session id.
19+
Cypress.session.getSession(id)
1420
```
1521

16-
This can also be done by clicking the "Clear All Sessions" button in the
22+
Clearing all session and automatically re-running the spec
23+
`Cypress.session.clearAllSavedSessions()` can also be done by clicking the
24+
"Clear All Sessions" button in the
1725
[Sessions Instrument Panel](/api/commands/session#The-Instrument-Panel).
1826

1927
<DocsImage src="/img/api/session/sessions-panel.png" alt="Sessions Instrument Panel" ></DocsImage>
2028

29+
### Arguments
30+
31+
**<Icon name="angle-right"></Icon> id** **_(String)_**
32+
33+
The name of the session used to retrieve data storage and cookie data.
34+
35+
## Examples
36+
37+
### Clearing the all session data
38+
39+
By default, Cypress will clear the current session data **before** each test
40+
when `testIsolation` is enabled. You can also remove all cached session data
41+
with `Cypress.session.clearAllSavedSessions()`.
42+
43+
```js
44+
Cypress.session.clearAllSavedSessions()
45+
```
46+
47+
### Clearing the current session data when testIsolation is disabled
48+
49+
By default, Cypress will clear the current session data **before** each test
50+
when `testIsolation` is enabled. If you have disabled `testIsolation` for a
51+
suite, it can be helpful to clear the current session data in a `before()` block
52+
to ensure the suite started in a clean test slate.
53+
54+
```js
55+
describe('Dashboard', { testIsolation: false }, () => {
56+
before(() => {
57+
// ensure clean test slate for these tests
58+
cy.then(Cypress.session.clearCurrentSessionData)
59+
})
60+
})
61+
```
62+
63+
### Verified the Applied Session Data
64+
65+
To check all cookies, localStorage and sessionStorage that was applied after
66+
`cy.session()` completes, you can use `Cypress.session.getCurrentSessionData()`.
67+
This can be helpful for quickly analyzing the current browser context while
68+
writing your `cy.session()` command.
69+
70+
Since this is an all-in-one helper of the `cy.getAllCookies()`,
71+
`cy.getAllLocalStorage()` and `cy.getAllSessionStorage()` commands, we generally
72+
recommend leveraging these commands for asserting the correct session data has
73+
been applied in the session validation block.
74+
75+
```js
76+
it('debug session', () => {
77+
cy.session('id', () => {
78+
...
79+
})
80+
.then(async () => {
81+
const sessionData = await Cypress.session.getCurrentSessionData()
82+
cy.debug()s
83+
})
84+
})
85+
```
86+
87+
### Debugging Cached Session Data
88+
89+
If your session seems to be recreated more than expected, or doesn't seem to be
90+
applying the cookies, `localStorage` or `sessionStorage` data that you'd expect,
91+
you can use `Cypress.session.getSession(id)` to view what session data has been
92+
cached by `cy.session()`. If you are missing any data, your setup and/or
93+
validate function may not be waiting long enough for all attributes to be
94+
applied to there page before the `cy.session()` command saves and finishes.
95+
96+
```js
97+
it('debug session', () => {
98+
cy.session('id', () => {
99+
...
100+
})
101+
.then(async () => {
102+
const sessionData = await Cypress.session.getSession('id')
103+
cy.debug()
104+
})
105+
})
106+
```
107+
21108
## See also
22109

23110
- [`cy.session()`](/api/commands/session)

0 commit comments

Comments
 (0)