How to handle multiple domains in cypress test

Starting from Cypress version 9.6, it is possible to handle multiple domains within a single test. Prior to version 9.6, Cypress only supported testing within a single domain.

You can set the initial domain in the Cypress configuration (for example, using the baseUrl setting) and then use the cy.visit() command in your test script to navigate to the website.

To handle multiple domains during test execution, you can use the cy.origin() command to switch between different domains as part of your test flow.

Make sure to enable the experimentalSessionAndOrigin flag in your Cypress configuration file (e.g., cypress.config.js or cypress.config.ts) to allow multi-domain testing.

Example Configuration:

// cypress.config.js
module.exports = {
experimentalSessionAndOrigin: true,
e2e: {
baseUrl: ‘https://example.com’,
},
};

Example usage in Script:

// Visit the main website (base URL defined in Cypress configuration)
cy.visit('/'); 
// Switch to a different domain using cy.origin()
cy.origin('https://another-domain.com', () => {
  cy.visit('/');
  cy.get('h1').should('contain', 'Welcome');
});
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Subscribe to our YouTube Channel: Testing Talks Latest
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x