Cypress Tests in Azure DevOps: Setup Guide
How to run Cypress tests in Azure DevOps CI/CD pipelines. Covers Cypress installation on CI agents, YAML configuration, video and screenshot artifacts.
Cypress has first-class CI support and runs well on Azure DevOps with minimal configuration. This guide covers everything from basic setup to production-grade parallel execution.
Basic Cypress pipeline
YAML1trigger: 2 branches: 3 include: [main] 4 5pool: 6 vmImage: ubuntu-latest 7 8steps: 9 - task: NodeTool@0 10 inputs: 11 versionSpec: '20.x' 12 13 - script: npm ci 14 displayName: Install packages 15 16 - script: npx cypress verify 17 displayName: Verify Cypress install 18 19 - script: | 20 npx cypress run \ 21 --browser chrome \ 22 --reporter junit \ 23 --reporter-options "mochaFile=cypress/results/results-[hash].xml,toConsole=true" 24 displayName: Run Cypress tests 25 env: 26 CYPRESS_BASE_URL: $(STAGING_URL) 27 CYPRESS_TEST_EMAIL: $(TEST_EMAIL) 28 CYPRESS_TEST_PASSWORD: $(TEST_PASSWORD) 29 30 - task: PublishTestResults@2 31 displayName: Publish results 32 inputs: 33 testResultsFormat: JUnit 34 testResultsFiles: 'cypress/results/*.xml' 35 mergeTestResults: true 36 testRunTitle: Cypress — $(Build.BuildNumber) 37 condition: always() 38 39 - task: PublishPipelineArtifact@1 40 displayName: Upload screenshots and videos 41 inputs: 42 targetPath: cypress/screenshots 43 artifact: cypress-screenshots 44 condition: failed() 45 46 - task: PublishPipelineArtifact@1 47 displayName: Upload videos 48 inputs: 49 targetPath: cypress/videos 50 artifact: cypress-videos 51 condition: always()
Cypress configuration for CI
JAVASCRIPT1// cypress.config.js 2const { defineConfig } = require('cypress') 3 4module.exports = defineConfig({ 5 e2e: { 6 baseUrl: process.env.CYPRESS_BASE_URL || 'http://localhost:3000', 7 video: true, 8 videoCompression: 32, 9 screenshotOnRunFailure: true, 10 defaultCommandTimeout: process.env.CI ? 10000 : 4000, 11 requestTimeout: process.env.CI ? 15000 : 5000, 12 pageLoadTimeout: process.env.CI ? 30000 : 10000, 13 retries: { 14 runMode: 2, // Retry twice in CI 15 openMode: 0, 16 }, 17 }, 18})
Using environment variables in tests
Cypress environment variables set with CYPRESS_ prefix are available as Cypress.env():
JAVASCRIPT1// In pipeline YAML 2- script: npx cypress run 3 env: 4 CYPRESS_BASE_URL: $(STAGING_URL) 5 CYPRESS_API_KEY: $(API_KEY) 6 7// In test code 8cy.request({ 9 url: `${Cypress.env('BASE_URL')}/api/health`, 10 headers: { 'X-API-Key': Cypress.env('API_KEY') } 11})
Parallel execution with Cypress Cloud
For large test suites, use Cypress Cloud (formerly Cypress Dashboard) to distribute tests across agents:
YAML1strategy: 2 matrix: 3 Agent1: { MACHINE_NUM: 1 } 4 Agent2: { MACHINE_NUM: 2 } 5 Agent3: { MACHINE_NUM: 3 } 6 maxParallel: 3 7 8steps: 9 - script: npm ci 10 - script: | 11 npx cypress run \ 12 --record \ 13 --parallel \ 14 --ci-build-id $(Build.BuildId) \ 15 --group "Azure CI" \ 16 --key $(CYPRESS_RECORD_KEY) 17 env: 18 CYPRESS_RECORD_KEY: $(CYPRESS_RECORD_KEY) 19 CYPRESS_BASE_URL: $(STAGING_URL)
Cypress Cloud distributes tests across the 3 agents automatically based on historical execution times.
Adding the junit reporter
BASH1npm install --save-dev cypress-multi-reporters mocha-junit-reporter
JSON1// reporter-config.json 2{ 3 "reporterEnabled": "spec, mocha-junit-reporter", 4 "mochaJunitReporterReporterOptions": { 5 "mochaFile": "cypress/results/results-[hash].xml" 6 } 7}
YAML1- script: | 2 npx cypress run \ 3 --reporter cypress-multi-reporters \ 4 --reporter-options configFile=reporter-config.json
Common errors and fixes
Error: Cypress failed to start: The Cypress App could not be started
Fix: Add DISPLAY=:99 or use npx cypress run (not cypress open). On Linux CI agents without a display server, Cypress must run in headless mode — cypress run is headless by default.
Error: Videos are recorded but empty (0 bytes)
Fix: Add ffmpeg to the agent or use Microsoft-hosted Ubuntu agents which have it pre-installed. Video recording requires ffmpeg.
Error: Timeout of 4000ms exceeded for page loads
Fix: Increase pageLoadTimeout in cypress.config.js for CI runs. Staging environments on CI agents are often accessed over slower network connections.
Error: Tests fail with cy.intercept not intercepting requests
Fix: cy.intercept() must be set up before the request is made. Move intercepts to beforeEach hooks that run before cy.visit().
Error: Cypress install hangs in pipeline
Fix: Set CYPRESS_INSTALL_BINARY=0 if you don't need the GUI binary (CI-only runs). Or cache the Cypress binary: use Cache@2 task with key based on package-lock.json.
Share this article
Follow for more
Follow me on social media for more developer tips, tricks, and tutorials. Let's connect and build something great together!