</>

Technology

Cypress

Difficulty

Intermediate

Interview Question

How do you run Cypress tests in CI/CD pipelines like GitHub Actions?

Answer

Cypress in CI/CD — GitHub Actions

Basic GitHub Actions Workflow

YAML
# .github/workflows/cypress.yml
name: Cypress E2E Tests

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  cypress-tests:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run Cypress Tests
        uses: cypress-io/github-action@v6
        with:
          start: npm start        # start your app
          wait-on: 'http://localhost:3000'  # wait for app to be ready
          wait-on-timeout: 60
          browser: chrome
          headless: true
        env:
          CYPRESS_BASE_URL: ${{ secrets.CYPRESS_BASE_URL }}
          CYPRESS_USERNAME:  ${{ secrets.TEST_USERNAME }}
          CYPRESS_PASSWORD:  ${{ secrets.TEST_PASSWORD }}

      - name: Upload Screenshots on Failure
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: cypress-screenshots
          path: cypress/screenshots

      - name: Upload Videos
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: cypress-videos
          path: cypress/videos

Parallel Execution (Cypress Cloud)

YAML
# Run tests in parallel across 4 machines
jobs:
  cypress-parallel:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        containers: [1, 2, 3, 4]  # 4 parallel runners

    steps:
      - uses: actions/checkout@v4
      - uses: cypress-io/github-action@v6
        with:
          start: npm start
          wait-on: 'http://localhost:3000'
          record: true
          parallel: true
          group: 'E2E Tests'
        env:
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}

cypress.config.js — Environment Variables

JavaScript
// cypress.config.js
const { defineConfig } = require('cypress');

module.exports = defineConfig({
  e2e: {
    baseUrl: process.env.CYPRESS_BASE_URL || 'http://localhost:3000',
    viewportWidth:  1280,
    viewportHeight: 720,
    video: true,
    screenshotOnRunFailure: true,
    defaultCommandTimeout: 8000,
  },
  env: {
    username: process.env.CYPRESS_USERNAME,
    password: process.env.CYPRESS_PASSWORD,
    apiUrl:   process.env.CYPRESS_API_URL,
  },
});

Access env vars in tests

JavaScript
cy.get('#email').type(Cypress.env('username'))
cy.get('#password').type(Cypress.env('password'))

Run Specific Tests / Tags

Bash
# Run only specific spec files
npx cypress run --spec "cypress/e2e/login.cy.js"

# Run by grep (needs @cypress/grep plugin)
npx cypress run --env grep="@smoke"

# Run headlessly
npx cypress run --browser chrome --headless

Follow AutomateQA

Related Topics