which subdomains to add to your allowlist for Cypress Cloud and your GitLab integration to work properly (Test Automation)
Learn which subdomains to add to your allowlist for Cypress Cloud and your GitLab integration to work properly (Test Automation) step by step with clear examples and exercises.
Title: Test Automation - Configuring Cypress Cloud and GitLab Integration for Seamless Execution (JavaScript)
Why This Matters
In test automation, integrating tools like Cypress Cloud and GitLab can significantly streamline your testing process, providing valuable insights into test runs, reducing flaky tests, and enhancing overall efficiency. Understanding how to properly configure the allowlist for these tools is crucial for a smooth workflow in your JavaScript-based test automation projects.
By integrating Cypress Cloud with GitLab, you can use the cloud service's recording, running, and debugging capabilities while benefiting from GitLab's continuous integration and deployment features. Proper configuration of the allowlist ensures that tests are executed against the correct domains during test runs.
Prerequisites
Before diving into the configuration process, ensure you have:
- Node.js installed on your system (https://nodejs.org/)
- A GitLab account and repository set up with your test automation project
- Cypress installed in your project (
npm install cypress) - Basic understanding of JavaScript, Git, and test automation principles
- Familiarity with the Cypress Cloud documentation (https://docs.cypress.io/)
- Access to your GitLab project's CI/CD settings
- A working test automation project with at least one test spec file in the
cypress/integrationfolder
Core Concept
Cypress Cloud allows you to record, run, and debug tests within a web browser, while GitLab provides continuous integration and deployment capabilities for your test automation project. To ensure seamless integration between the two tools, you need to properly configure the allowlist for Cypress Cloud in your GitLab CI/CD pipeline.
The allowlist is a list of subdomains that Cypress will target during test execution. By default, Cypress targets the domain from which it was launched. However, when using GitLab Runner, tests are typically executed on a different domain than the one used for manual testing or local development. To address this issue, you must add the appropriate subdomains to the allowlist in your cypress.json configuration file.
Worked Example
Let's assume your GitLab project is hosted at gitlab.example.com, and your application under test (AUT) is accessible at my-app.example.com. To configure the allowlist, follow these steps:
- Navigate to your project directory in the terminal.
- Create a
cypress.jsonfile if it doesn't exist, or update it if already present. - Add the following content to the
cypress.jsonfile:
{
"baseUrl": "https://my-app.example.com",
"viewportWidth": 1280,
"viewportHeight": 720,
"video": false,
"screenshotsFolder": "./cypress/screenshots",
"reporter": "mochawesome",
"reporterOptions": {
"reportDir": "./cypress/reports/mochawesome",
"overwrite": false,
"html": false,
"json": true
},
"env": {
"allow_origins": ["gitlab.example.com", "my-app.example.com"]
}
}
In the example above, we've set the baseUrl to point to your AUT, configured the viewport size, disabled video recording, specified a custom screenshots folder, chosen Mochawesome as the reporter, and added both GitLab and your AUT's domains to the allowlist.
Common Mistakes
- Forgetting to add the
allow_originsentry in thecypress.jsonconfiguration file. - Incorrectly specifying the subdomain(s) in the
allow_originsarray. Ensure that you include the exact domain or subdomain where your AUT is hosted. - Not disabling video recording, which can cause large amounts of data to be uploaded and slow down your GitLab pipeline.
- Failing to update the
baseUrlin thecypress.jsonfile when switching between different environments (e.g., development, staging, production). - Neglecting to configure the reporter options, which can lead to issues with test reports not being generated correctly.
- Not properly setting up GitLab Runner and configuring the CI/CD pipeline to use the
cypress runcommand for test execution. - Failing to log in to Cypress Cloud and associate your GitLab project with the account, which is necessary for test results to be uploaded to the cloud service.
Practice Questions
- If your GitLab project is hosted at
gitlab.example.comand your AUT is accessible atmy-app.example.org, what changes would you make to thecypress.jsonfile? - Explain why it's important to disable video recording when using Cypress Cloud with GitLab Runner.
- Your teammate has reported that their tests are failing intermittently in the GitLab pipeline. What steps can you suggest to diagnose and address this issue?
- Why is it essential to update the
baseUrlin thecypress.jsonfile when switching between different environments (e.g., development, staging, production)? - How would you set up GitLab Runner for your test automation project, and what configuration options should be considered?
- What are some best practices for managing and organizing test spec files in the
cypress/integrationfolder to ensure maintainability and scalability of your test suite?
FAQ
- Why do I need to configure the allowlist for Cypress Cloud when using GitLab Runner?
- When running tests through GitLab Runner, the domain used may differ from the one used during manual testing or local development. Adding the appropriate subdomains to the allowlist ensures that Cypress can target the correct domains during test execution.
- What happens if I omit the
allow_originsentry in thecypress.jsonfile?
- If you omit the
allow_originsentry, Cypress will only be able to target the domain from which it was launched, potentially causing tests to fail when executed through GitLab Runner.
- Why is it important to update the
baseUrlin thecypress.jsonfile when switching between different environments?
- Updating the
baseUrlensures that Cypress targets the correct URL for your AUT, regardless of the environment (e.g., development, staging, production) you're testing against. This helps maintain test consistency and accuracy across various environments.
- How do I set up GitLab Runner for my test automation project?
- Follow the instructions provided in the GitLab Runner documentation to install and configure GitLab Runner on your machine or a dedicated server. Once installed, create a new runner and configure it to use the
cypress runcommand for test execution.
- What are some best practices for managing and organizing test spec files in the
cypress/integrationfolder?
- Organize test spec files by feature or module, using descriptive names that clearly indicate their purpose. Consider creating separate folders for different features or modules, and use subfolders if necessary to further organize your tests. Additionally, consider writing tests in a way that they can be easily maintained and extended as your application grows.