Free discovery callFree discovery call

Testing Across Multiple Domains in Cypress? Here's How to Do It Right

A QA Engineer's Guide to Multi-Domain Testing - Part 2

QALast updated: 28 Nov 20245 min read

By Sunčica Kojić

Your Cypress tests work great within a single domain, but what happens when your application spans multiple domains?

Whether you're dealing with microservices, third-party integrations, or localized versions of your site, testing across domains can quickly become complicated.

Why Multi-Domain Testing in Cypress Can Be Tricky

If you're a QA engineer working with modern web applications, you've likely encountered these scenarios:

  • Your tests break when crossing domain boundaries
  • Session management becomes unreliable between different domains
  • CORS issues keep popping up in your test runs
  • Your configuration becomes unwieldy when handling multiple domains

In Part 1 of this series, we covered how to set up Cypress for different environments. Now in Part 2, we'll tackle the specific challenges of testing applications that span multiple domains. You'll learn practical approaches using cy.visit(), cy.origin(), and environment variables to create a robust, maintainable test suite that works seamlessly across domains.

Let's explore the different solutions and find the right approach for your testing needs.


Simplifying Multi-Domain Tests with cy.visit()

When testing an application across multiple domains, Cypress's cy.visit() allows you to navigate to different URLs, ensuring consistent application behavior. This is particularly valuable for applications that interact with external services, APIs, or localized versions.

In the example below, cy.visit() is used in separate it blocks to load various domains within a Cypress test suite. Each test case interacts with a specific domain, performing assertions to validate functionality independently. This method facilitates multi-domain testing, ensuring consistent behavior across different websites within a single test file.

//test.cy.js


describe('Multi-Domain Testing with cy.visit()', () => {
  it('Testing functionality on https://example.eu', () => {
    cy.visit('https://example.eu');

   //Your tests here
  });

  it('Testing functionality on https://example.us', () => {
    cy.visit('https://example.us');

    //Your tests here
  });
});

Pros:

  • Simplified Test Writing — You can streamline the process of writing tests by managing domain navigation with a single command.
  • Early Issue Detection: — Helps identify domain-specific issues early in the testing phase.
  • Simplified Navigation — Enables easy transitions between domains in a single command.

Cons:

  • CORS Issues — Cross-origin requests can be blocked, causing functionality issues between domains.
  • Increased Complexity — Jumping between domains in tests can introduce more complexity, making tests harder to maintain.
  • Slower Test Execution — Multi-domain navigation can slow down test execution.
  • Harder Debugging — The added complexity can make pinpointing issues and debugging more challenging.
  • More Maintenance Effort— Any changes to the configuration might require updates across multiple test specs, increasing maintenance work.
  • Session Management Complexity — Managing authentication tokens and cookies across domains is challenging.

In conclusion, although cy.visit() simplifies multi-domain testing in Cypress, it’s important to consider the challenges that may emerge during the process.


How to Test Multiple Domains with cy.origin()

The cy.origin() command in Cypress is essential for testing applications that span multiple domains. This command enables you to run test commands within the context of different origins, making it particularly useful for applications with varied behaviors based on the domain accessed.

The example demonstrates how to use cy.origin() to run tests across two different domains: https://example.eu and https://example.us. Each block begins with cy.visit('/') to navigate to the root of the specified domain, allowing you to add test code to interact with elements on each site.

This approach ensures that tests remain isolated by domain, promoting clear and organized multi-origin testing.

//test.cy.js

describe('Multi-Domain Testing', () => {
  it('should navigate and perform actions on example.eu', () => {
    cy.origin('https://example.eu', () => {
      cy.visit('/');
      // your test code for the EU site
    });
  });

  it('should navigate and perform actions on example.us', () => {
    cy.origin('https://example.us', () => {
      cy.visit('/');
      // your test code for the US site
    });
  });

Pros:

  • Session Persistence — Maintains user sessions across different domains, ensuring seamless interactions.
  • Realistic Testing — Reflects real-world user behavior by allowing actions like logging in or retrieving user data without losing authentication.
  • Context-Specific Execution — Executes commands within the relevant domain's context, improving the precision and reliability of test results.

Cons:

  • Added Complexity — Each test block runs independently, which can make the overall test structure harder to follow and manage.
  • Debugging Difficulties — A more complex setup can make identifying and fixing issues more challenging.
  • Time-Consuming Updates — Requires individual updates across multiple test specs, increasing maintenance time and the risk of inconsistencies.

In summary, while cy.origin() in Cypress offers significant advantages for testing multi-domain applications, such as session persistence and context-specific execution, it also introduces complexities that can complicate the testing process and increase maintenance efforts. Careful consideration of these trade-offs is essential for effective multi-domain testing strategies.

Using Environment Variables to Manage Multiple Domains

After reviewing a few different approaches to multi-domain testing in Cypress, it becomes clear that managing different configurations is essential for ensuring accurate and efficient test execution. Cypress provides a straightforward way to handle this through environment variables. By using environment variables, you can control which domain or API your tests interact with, making it easier to manage various scenarios and requirements.

Setting Environment Variables

//cypress.env.json

const envConfig = {
  development: {
    DOMAIN_EU: "https://example.dev.eu",
    DOMAIN_US: "https://example.dev.us",
  },
  staging: {
    DOMAIN_EU: "https://example.staging.eu",
    DOMAIN_US: "https://example.staging.us",
  },
  production: {
    DOMAIN_EU: "https://example.eu",
    DOMAIN_US: "https://example.us",
  },
};

module.exports = { envConfig };

The above setup allows you to seamlessly connect specific domains with their respective environments, ensuring that your tests run against the correct URLs based on the active configuration. By managing both domains and environments together, you create a flexible and scalable testing framework that can easily adapt to changing requirements.

Once you have defined your environment variables, you can easily access them within your test cases. Here’s how to incorporate environment variables in your tests:

//test.cy.js

describe('Multi-Domain Testing with Environment Variables', () => {
  it('should navigate to the specified EU domain', () => {
    const domain = Cypress.env('DOMAIN_EU');
    cy.visit(domain);
    // your test code for the EU site
  });

  it('should navigate to the specified US domain', () => {
    const domain = Cypress.env('DOMAIN_US');
    cy.visit(domain);
    // your test code for the US site
  });
});

Pros:

  • Simplified Management — Environment variables allow a single location to manage URLs and API endpoints, simplifying updates and reducing hard-coded values in test scripts.
  • Easy Updates — Updating a value in the configuration file automatically reflects across all tests, saving time and making your tests more adaptable.
  • Reliable Testing By reducing manual changes, you ensure more consistent and accurate test results across different environments.
  • Flexible Configurations — You can easily set up different environment variables for development, staging, and production, making it seamless to switch between environments.
  • CI/CD Friendly — CI/CD pipelines can easily integrate environment variables, ensuring consistent configurations across automated testing and deployment, which reduces the chances of environment-specific problems.
  • Version Control Friendly — Sensitive information (like API keys) can be stored in environment variables instead of being hard-coded, making it safer to use version control systems without exposing sensitive data.
  • Dynamic Configuration — Environment variables allow for runtime configuration changes, which can be useful for tests that require different setups based on varying conditions.

Cons:

  • Security Concerns — Sensitive data in environment variables can be at risk if not handled carefully, especially if it appears in logs or error messages.
  • Managing Complexity — As projects grow, managing a large number of environment variables can get tricky, requiring good organization and documentation.
  • Setup Dependencies — Tests may fail if the necessary environment variables are not set correctly, leading to potential issues in test execution.

Leveraging environment variables in Cypress provides a flexible and maintainable approach to managing multiple domains during testing. By centralizing your configurations and allowing for easy updates, you can enhance the efficiency of your testing process while minimizing errors associated with manual changes. This practice is especially valuable in multi-domain scenarios where different domains require unique settings.


What We Learned About Multi-Domain Testing with Cypress

In conclusion, effectively managing multiple domains in Cypress is crucial for ensuring a seamless user experience in modern web applications. A well-planned approach helps you overcome common challenges in multi-domain testing, such as handling session management, resolving CORS issues, and addressing the unique behaviors of different domains.

By applying the right strategies, you can achieve more reliable test execution across multiple domains, ultimately improving application quality and user satisfaction. Adopting these best practices will enable you to build robust applications that consistently meet user expectations and deliver dependable performance.

Related ArticlesTechnology x Design

View all articlesView all articles
( 01 )Get started

Start a Project