Cypress announced on 25th April 2022 a feature that many Cypress users have been requesting: the ability to test many superdomains in a single test. Users may simply switch between origins with the experimental cy.origin() command, which is new in Cypress 9.6.0, to test syndicated authentication, cross-site CMS workflows, and more.
What was the issue before?
Until now, using Cypress to test multi-domain processes necessitated some difficult compromises. Because Cypress runs in the browser, tests that run commands on several superdomains run into the same-origin rule. Users of Cypress who wished to write tests against syndicated login services like Auth0 or GitHub ran into issues.
The suggested method has always been to log in programmatically, which eliminates the need to engage with third-party login pages. Users had no choice but to work around the same-domain barrier by separating the user story into numerous tests if they wanted to explicitly test the whole login flow like a real user. However, this goes against the test isolation idea and can result in hard-to-debug failures, strange edge instances, and test flake.
The Solution to the issue
Now, users may avoid cumbersome workarounds and brittle, implementation-specific login code by using cy.origin(). Users can use cy.origin() to run commands against any number of superdomains within a single test case.
Cypress injects the test runtime into the secondary origin, sends it the text of the requested callback function, runs it in the secondary domain, and then returns control to the original test origin. Furthermore, users do not need to understand any of this to use cy.origin(); all they have to do is write cross-origin test commands within the block. The cy.origin() command can, of course, be used in a custom Cypress command.
It’s important to note that when the args option is used, the callback is sent to the secondary origin as text, and users must manually put in any arguments the callback requires.
What’s new in Cypress 9.6.0?
- Even a moderately large test suite might gain a lot of overhead by going through a complete login sequence before each test. Because of this, the newly built cy.origin() may be used in conjunction with cy.session() to create a fully integrated solution for testing current syndicated authentication operations.
- Users can cache session information between tests by modifying the previous section’s example with cy.session(), boosting efficiency and reducing needless network clutter.
How to enable cy.origin command?
Users can check out the new capability after upgrading to Cypress 9.6.0 by setting the new experimentalSessionAndOrigin configuration option to true. This flag also enables the cy.session() command, and it substitutes the experimentalSessionSupport flag, which has been deprecated and will result in an error. Toggling this setting imposes test isolation and may cause other issues, so read the upgrade information in the cy.origin() API docs before using it.