@@ -13,36 +13,6 @@ limitation determined by standard web security features of the browser. The
13
13
14
14
<Alert type =" warning " >
15
15
16
- <strong class =" alert-header " ><Icon name =" exclamation-triangle " ></Icon >
17
- Experimental</strong >
18
-
19
- The ` session ` API is currently experimental, and can be enabled by setting the
20
- [ ` experimentalSessionAndOrigin ` ] ( /guides/references/experiments ) option to
21
- ` true ` in the Cypress config.
22
-
23
- Enabling this flag does the following:
24
-
25
- - It adds the ` cy.session() ` and [ ` cy.origin() ` ] ( /api/commands/origin ) commands,
26
- and [ ` Cypress.session ` ] ( /api/cypress-api/session ) API.
27
- - It adds the concept of
28
- [ ` testIsolation ` ] ( /guides/core-concepts/writing-and-organizing-tests#Test-Isolation )
29
- which defaults to ` on ` , such that:
30
- - The page is cleared (by setting it to ` about:blank ` ).
31
- - Cookies, local storage and session storage in all domains are cleared.
32
- - It supersedes the
33
- [ ` Cypress.Cookies.preserveOnce() ` ] ( /api/cypress-api/cookies#Preserve-Once ) and
34
- [ ` Cypress.Cookies.defaults() ` ] ( /api/cypress-api/cookies#Defaults ) methods.
35
- - Cross-origin requests will now succeed, however, to interact with a
36
- cross-origin page you must use a ` cy.origin ` block.
37
-
38
- Because the page is cleared at the beginning of each test by default,
39
- [ ` cy.visit() ` ] ( /api/commands/visit ) must be explicitly called at the beginning
40
- of each test.
41
-
42
- </Alert >
43
-
44
- <Alert type =" warning " >
45
-
46
16
<strong class =" alert-header " ><Icon name =" exclamation-triangle " ></Icon >
47
17
Obstructive Third Party Code</strong >
48
18
@@ -340,9 +310,8 @@ performant. Up until now you could get around this problem by putting login code
340
310
in the first test of your file, then performing subsequent tests reusing the
341
311
same session.
342
312
343
- However, once the ` experimentalSessionAndOrigin ` flag is activated, this is no
344
- longer possible, since all session state is now cleared between tests. So to
345
- avoid this overhead we recommend you leverage the
313
+ However, this is no longer possible, since all session state is now cleared
314
+ between tests. So to avoid this overhead we recommend you leverage the
346
315
[ ` cy.session() ` ] ( /api/commands/session ) command, which allows you to easily
347
316
cache session information and reuse it across tests. So now let's enhance our
348
317
custom login command with ` cy.session() ` for a complete syndicated login flow
@@ -385,81 +354,6 @@ and reuse it across tests.
385
354
386
355
## Notes
387
356
388
- ### Migrating existing tests
389
-
390
- Enabling the ` experimentalSessionAndOrigin ` flag makes the test-runner work
391
- slightly differently, and some test suites that rely on the existing behaviour
392
- may have to be updated. The most important of these changes is
393
- [ ** test isolation** ] ( ) which defaults to ` on ` . This means that after every test,
394
- the current page is reset to ` about:blank ` and cookies, local storage and
395
- session storage in all domains are cleared before each test. This change is
396
- opt-in for now, but will be standardized in a future major release of Cypress,
397
- so eventually all tests will need to be isolated.
398
-
399
- Before this change, it was possible to write tests such that you could, for
400
- example, log in to a CMS in the first test, change some content in the second
401
- test, verify the new version is displayed on a different URL in the third, and
402
- log out in the fourth. Here's a simplified example of such a test strategy.
403
-
404
- <Badge type =" danger " >Before</Badge > Multiple small tests against different
405
- origins
406
-
407
- ``` js
408
- it (' logs in' , () => {
409
- cy .visit (' https://supersecurelogons.com' )
410
- cy .get (' input#password' ).type (' Password123!' )
411
- cy .get (' button#submit' ).click ()
412
- })
413
-
414
- it (' updates the content' , () => {
415
- cy .get (' #current-user' ).contains (' logged in' )
416
- cy .get (' button#edit-1' ).click ()
417
- cy .get (' input#title' ).type (' Updated title' )
418
- cy .get (' button#submit' ).click ()
419
- cy .get (' .toast' ).type (' Changes saved!' )
420
- })
421
-
422
- it (' validates the change' , () => {
423
- cy .visit (' /items/1' )
424
- cy .get (' h1' ).contains (' Updated title' )
425
- })
426
- ```
427
-
428
- After switching on ` experimentalSessionAndOrigin ` , this flow would need to be
429
- contained within a single test. While this practice has always been
430
- [ discouraged] ( /guides/references/best-practices#Having-tests-rely-on-the-state-of-previous-tests )
431
- we know some users have historically written tests this way, often to get around
432
- the same-origin restrictions. But with ` cy.origin() ` you no longer need these
433
- kind of brittle hacks, as your multi-origin logic can all reside in a single
434
- test, like the following.
435
-
436
- <Badge type =" success " >After</Badge > One big test using ` cy.origin() `
437
-
438
- ``` js
439
- it (' securely edits content' , () => {
440
- cy .origin (' supersecurelogons.com' , () => {
441
- cy .visit (' https://supersecurelogons.com' )
442
- cy .get (' input#password' ).type (' Password123!' )
443
- cy .get (' button#submit' ).click ()
444
- })
445
-
446
- cy .origin (' mycms.com' , () => {
447
- cy .url ().should (' contain' , ' cms' )
448
- cy .get (' #current-user' ).contains (' logged in' )
449
- cy .get (' button#edit-1' ).click ()
450
- cy .get (' input#title' ).type (' Updated title' )
451
- cy .get (' button#submit' ).click ()
452
- cy .get (' .toast' ).type (' Changes saved!' )
453
- })
454
-
455
- cy .visit (' /items/1' )
456
- cy .get (' h1' ).contains (' Updated title' )
457
- })
458
- ```
459
-
460
- Always remember,
461
- [ Cypress tests are not unit tests] ( https://docs.cypress.io/guides/references/best-practices#Creating-tiny-tests-with-a-single-assertion ) .
462
-
463
357
### Serialization
464
358
465
359
When entering a ` cy.origin() ` block, Cypress injects itself at runtime, with all
@@ -642,9 +536,6 @@ following Cypress commands will throw errors if used in the callback:
642
536
- ` cy.origin() `
643
537
- [ ` cy.intercept() ` ] ( /api/commands/intercept )
644
538
- [ ` cy.session() ` ] ( /api/commands/session )
645
- - [ ` cy.server() ` ] ( /api/commands/server )
646
- - [ ` cy.route() ` ] ( /api/commands/route )
647
- - [ ` Cypress.Cookies.preserveOnce() ` ] ( /api/cypress-api/cookies )
648
539
649
540
### Other limitations
650
541
0 commit comments