Earlier to 9.6 , Cypress enforced a strict single-origin policy, making cross-domain testing difficult. We can handle multiple domains in cypress with latest versions from 9.6. Â We can use cy.origin() method to handle multiple domain during the execution with cypress. This will solve the issue with handling multiple domains with cypress. Following is the example code for automation.
Example Script:
describe(‘Multi-domain test’, () => {
it(‘Handles login and external link navigation’, () => {
// Visit the first domain
cy.visit(‘https://example.com’);
cy.get(‘#login’).click();
// Perform actions on the second domain
cy.origin(‘https://seconddomain.com’, () => {
cy.get(‘#search’).type(‘Cypress{enter}’);
cy.get(‘.result’).should(‘contain’, ‘Cypress’);
});
// Validate back on the first domain
cy.get(‘#profile’).should(‘be.visible’);
});
});