|
2 | 2 | title: Cypress.session
|
3 | 3 | ---
|
4 | 4 |
|
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. |
7 | 8 |
|
8 | 9 | ## Syntax
|
9 | 10 |
|
10 |
| -Clear all saved sessions and re-run the current spec file. |
11 |
| - |
12 | 11 | ```javascript
|
| 12 | +// Clear all sessions saved on the backend, including cached global sessions. |
13 | 13 | 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) |
14 | 20 | ```
|
15 | 21 |
|
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 |
17 | 25 | [Sessions Instrument Panel](/api/commands/session#The-Instrument-Panel).
|
18 | 26 |
|
19 | 27 | <DocsImage src="/img/api/session/sessions-panel.png" alt="Sessions Instrument Panel" ></DocsImage>
|
20 | 28 |
|
| 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 | + |
21 | 108 | ## See also
|
22 | 109 |
|
23 | 110 | - [`cy.session()`](/api/commands/session)
|
0 commit comments