How to run Cypress test cases across multiple domains
How to run Cypress test cases across multiple domains

How to run Cypress test cases across multiple domains

Cypress test cases across multiple domains

Cypress test cases across multiple domains

Problem Statement

Before cypress version 9.6.0 if we want to run test cases in two domains under the same test case it gives us a different origin error.

In Version 9.6.0 this problem is solved, Now we can easily execute our test cases across multi-domain by using cy.origin() command.

How to solve the problem

Before coming to the solution let’s see if we run the below test cases (Before Cypress Version 9.6.0) what the error comes

In the below screenshot there are two described blocks in the first described block we are opening two domain URLs in different test cases and in the second described block we are opening two domain URLs under the SAME test case /or it() block to run our test cases.

What Error come at present?

When we run the above test cases, the first describe block run successfully but in the second describe block as we have given two domains under the same test cases it throws an error see below. Error: The new URL is considered a different origin because the following parts of the URL are different

Solution for multi-domain testing

With cy.origin() we can execute commands against any number of domains, all in the context of a single test case. Here’s a trivial example that fixes the failing test we introduced in the above section

Code attached below

Test Result after using cy.origin()

#1 First describe block
describe("Two Domain urls in different Test case", function () {
it("Open the Url", () => {
cy.visit("https://www.cypress.io/");
});
it("Visit the url and Login to new origin", () => {
cy.visit("https://www.saucedemo.com/");
cy.wait(3000);
cy.get('[id="user-name"]').clear().type("xxxxx");
cy.get('[id="password"]').clear().type("xxxx");
cy.get('[name="login-button"]').click();
cy.contains("Products").should("be.visible");
});
});
- -- - - - - --- --- - - - - - - - - - - - - - -
#2 Second describe block
describe("Two Domain urls under Same Test case", function () {
it("Open the Url", () => {
cy.visit("https://www.cypress.io/"); # Url -01
cy.contains("Home");
cy.origin("http://automationpractice.com/", () => { # Url -02
cy.visit("/index.php?controller=authentication&back=my-account");
cy.wait(2000);
cy.get('[id="email"]').clear().type("[email protected]");
cy.get('[id="passwd"]').clear().type("xxxx");
cy.get('[id="SubmitLogin"]').click();
});
});
});

In the below screenshot we can see when we use cy.origin() all test cases are passed without any different origin error

Cypress test cases across multiple domains

How to enable cy.origin command

In Cypress.json set the below command

{
"experimentalSessionAndOrigin": true
}

When experimentalSessionAndOrigin is enabled, Cypress will no longer wait on page loads between hooks before moving on to the next test.

Conclusion

We can use cy.origin() command to test the application across the different domains without any failure.

Resources

For further reading about cy.origin() please follow the below link

https://www.cypress.io/blog/2022/04/25/cypress-9-6-0-easily-test-multi-domain-workflows-with-cy-origin/#enabling-the-cyorigin-command

Leave a Reply

Your email address will not be published.