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