@@ -7,7 +7,13 @@ Cache and restore [cookies](/api/cypress-api/cookies),
7
7
[ ` localStorage ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage ) ,
8
8
and
9
9
[ ` sessionStorage ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage )
10
- in order to reduce test setup times.
10
+ (i.e. session data) in order to recreate a consistent browser context between
11
+ tests.
12
+
13
+ The ` cy.session() ` command will inherit the
14
+ [ ` testIsolation ` ] ( /guides/core-concepts/writing-and-organizing-tests#Test-Isolation )
15
+ mode value to determine whether or not the page is cleared when caching and
16
+ restoring the browser context.
11
17
12
18
<Alert type =" warning " >
13
19
@@ -22,20 +28,18 @@ Enabling this flag does the following:
22
28
23
29
- It adds the ` cy.session() ` and [ ` cy.origin() ` ] ( /api/commands/origin ) commands,
24
30
and [ ` Cypress.session ` ] ( /api/cypress-api/session ) API.
25
- - It adds the following new behaviors (that will be the default in a future
26
- major update of Cypress) at the beginning of each test:
27
- - The page is cleared (by setting it to ` about:blank ` ). Disable this by
28
- setting
29
- [ ` testIsolation=legacy ` ] ( /guides/core-concepts/writing-and-organizing-tests#Test-Isolation ) .
30
- - All active session data (cookies, ` localStorage ` and ` sessionStorage ` )
31
- across all domains are cleared.
32
- - It supersedes
33
- the [ ` Cypress.Cookies.preserveOnce() ` ] ( /api/cypress-api/cookies#Preserve-Once ) and
34
- [ ` Cypress.Cookies.defaults() ` ] ( /api/cypress-api/cookies#Defaults ) methods.
31
+ - It adds the concept of
32
+ [ ` testIsolation ` ] ( /guides/core-concepts/writing-and-organizing-tests#Test-Isolation )
33
+ which defaults to ` on ` , such that:
34
+ - The page is cleared (by setting it to ` about:blank ` ).
35
+ - Cookies, local storage and session storage in all domains are cleared.
36
+ - It supersedes the
37
+ [ ` Cypress.Cookies.preserveOnce() ` ] ( /api/cypress-api/cookies#Preserve-Once ) and
38
+ [ ` Cypress.Cookies.defaults() ` ] ( /api/cypress-api/cookies#Defaults ) methods.
35
39
- Cross-origin requests will now succeed, however, to interact with a
36
40
cross-origin page you must use a ` cy.origin ` block.
37
41
38
- Because the page is cleared at the beginning of each test,
42
+ Because the page is cleared at the beginning of each test by default ,
39
43
[ ` cy.visit() ` ] ( /api/commands/visit ) must be explicitly called at the beginning
40
44
of each test.
41
45
@@ -128,8 +132,11 @@ runs, Cypress will preserve all cookies, `sessionStorage`, and `localStorage`,
128
132
so that subsequent calls to ` cy.session() ` with the same ` id ` will bypass
129
133
` setup ` and just restore the cached session data.
130
134
131
- The page and all active session data (cookies, ` localStorage ` and
132
- ` sessionStorage ` ) across all domains are always cleared before ` setup ` runs.
135
+ The page is cleared before ` setup ` when ` testIsolation='on' ` and is not cleared
136
+ when ` testIsolation='off' ` .
137
+
138
+ Cookies, local storage and session storage in all domains are always cleared
139
+ before ` setup ` runs, regardless of the testIsolation configuration.
133
140
134
141
** <Icon name =" angle-right " ></Icon > options** ** _ (Object)_ **
135
142
@@ -331,10 +338,10 @@ describe('account details', () => {
331
338
332
339
### Switching sessions inside tests
333
340
334
- Because ` cy.session() ` clears the page and all active session data before
335
- running ` setup ` , you can use it to easily switch between sessions without first
336
- needing to log the previous user out. This allows tests to more accurately
337
- represent real-world scenarios and helps keep test run times short.
341
+ Because ` cy.session() ` clears the page and all session data before running
342
+ ` setup ` , you can use it to easily switch between sessions without first needing
343
+ to log the previous user out. This allows tests to more accurately represent
344
+ real-world scenarios and helps keep test run times short.
338
345
339
346
``` jsx
340
347
const login = (name ) => {
@@ -669,24 +676,46 @@ it('t3', () => {
669
676
670
677
## Notes
671
678
672
- ### When the page and active session data are cleared
679
+ ### When the page and session data are cleared
680
+
681
+ ### Test Isolation ` on `
682
+
683
+ The page is cleared and cookies, local storage and session storage (session
684
+ data) in all domains are cleared automatically when ` cy.session() ` runs and
685
+ ` testIsolation ` is ` on ` . This guarantees consistent behavior whether a session
686
+ is being created or restored and allows you to switch sessions without first
687
+ having to explicitly log out.
688
+
689
+ | | Page cleared (test) | Session data cleared |
690
+ | -------------------------- | :---------------------------------------------: | :---------------------------------------------: |
691
+ | Before ` setup ` | <Icon name =" check-circle " color =" green " ></Icon > | <Icon name =" check-circle " color =" green " ></Icon > |
692
+ | Before ` validate ` | <Icon name =" check-circle " color =" green " ></Icon > | |
693
+ | Before ` cy.session() ` ends | <Icon name =" check-circle " color =" green " ></Icon > | |
694
+
695
+ [ ` cy.visit() ` ] ( /api/commands/visit ) must be explicitly called afterwards to
696
+ ensure the page to test is loaded.
697
+
698
+ ### Test Isolation ` off `
699
+
700
+ When ` testIsolation ` is ` off ` , the page will not clear, however, the session
701
+ data will clear when ` cy.session() ` runs.
702
+
703
+ | | Page cleared (test) | Session data cleared |
704
+ | -------------------------- | :-----------------: | :---------------------------------------------: |
705
+ | Before ` setup ` | | <Icon name =" check-circle " color =" green " ></Icon > |
706
+ | Before ` validate ` | | |
707
+ | Before ` cy.session() ` ends | | |
673
708
674
- The page is cleared and all active session data (cookies, ` localStorage ` , and
675
- ` sessionStorage ` ) across all domains are cleared automatically when
676
- ` cy.session() ` runs. This guarantees consistent behavior whether a session is
677
- being created or restored and allows you to switch sessions without first having
678
- to explicitly log out.
709
+ [ ` cy.visit() ` ] ( /api/commands/visit ) does not need to be called afterwards to
710
+ ensure the page to test is loaded.
679
711
680
- | | Current page cleared | Active session data cleared |
681
- | -------------------- | :---------------------------------------------: | :---------------------------------------------: |
682
- | Before ` setup ` | <Icon name =" check-circle " color =" green " ></Icon > | <Icon name =" check-circle " color =" green " ></Icon > |
683
- | Before ` validate ` | <Icon name =" check-circle " color =" green " ></Icon > | |
684
- | After ` cy.session() ` | <Icon name =" check-circle " color =" green " ></Icon > | |
712
+ NOTE: Turning test isolation off may improve performance of end-to-end tests,
713
+ however, previous tests could impact the browser state of the next test and
714
+ cause inconsistency when using .only(). Be mindful to write isolated tests when
715
+ test isolation is off.
685
716
686
- Calling ` cy.session() ` clears the current page in addition to restoring the
687
- cached session data. [ ` cy.visit() ` ] ( /api/commands/visit ) must be explicitly
688
- called afterwards to ensure a page is visited if you did not provide a
689
- ` validate ` function that called ` cy.visit() ` .
717
+ When test isolation is ` off ` , it is encouraged to setup your session in a before
718
+ hook or in the first test to ensure a clean setup.
690
719
691
720
### Session caching
692
721
@@ -880,7 +909,7 @@ that were run when creating and/or validating the session.
880
909
In this image, a saved session is restored, but when ` /personal ` is visited in
881
910
the ` validate ` function, the app redirects to ` /signin ` , which invalidates the
882
911
session. A new session is created by visiting ` /signin ` where the user is logged
883
- in, after which, validation succeeds, and the session is made active for the
912
+ in, after which, validation succeeds, and the session is available for the
884
913
remainder of the test.
885
914
886
915
<DocsImage src =" /img/api/session/session-expanded.png " alt =" Recreated session (expanded) " ></DocsImage >
0 commit comments