Set up a CI/CD pipeline for Cypress Automation

Here are the requirements and implementation steps:

  • Prerequisites:
    • GitHub repository with your Cypress tests
    • Node.js installed in your project
    • package.json with Cypress dependencies
    • Basic project structure for Cypress tests
  • Basic Project Structure
  • Implementation Steps:
    • 1. Update package.json – Add these dependencies:
    • 2. Configure Cypress for CI – In cypress.config.js:

Features of the CI/CD Pipeline:

  • Runs on push to main/develop branches and pull requests
  • Daily scheduled runs
  • Parallel test execution across multiple browsers
  • Caching for faster builds
  • Test artifacts and reports storage
  • Environment variable support
  • Cross-browser testing

Additional Setup Required:

a. Set up repository secrets in GitHub:

  • CYPRESS_BASE_URL
  • CYPRESS_RECORD_KEY (if using Cypress Dashboard)

b. Optional: Configure Cypress Dashboard

  • Sign up for Cypress Dashboard
  • Add your project
  • Get the record key
  • Enable recording in the workflow

Best Practices:

  • Keep tests independent
  • Use environment variables for sensitive data
  • Implement retry logic for flaky tests
  • Regular maintenance of dependencies
  • Monitor test execution times

Monitoring and Maintenance:

  • Check GitHub Actions tab for build status
  • Review test reports regularly
  • Monitor test execution times
  • Update dependencies periodically
  • Review and update test cases as needed

** GitHub Actions CI/CD Pipeline for Cypress

name: Cypress Test Automation

on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
schedule:
– cron: ‘0 0 * * *’ # Run daily at midnight UTC

env:
NODE_VERSION: ’18’
CYPRESS_CACHE_FOLDER: ~/.cache/Cypress

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

strategy:
matrix:
# Add browsers you want to test with
browsers: [chrome, firefox]
fail-fast: false

steps:
– name: Checkout repository
uses: actions/checkout@v3

– name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: ‘npm’

– name: Cache Cypress binary
uses: actions/cache@v3
with:
path: ${{ env.CYPRESS_CACHE_FOLDER }}
key: cypress-binary-${{ hashFiles(‘**/package-lock.json’) }}
restore-keys: |
cypress-binary-

– name: Install dependencies
run: npm ci

– name: Verify Cypress installation
run: npx cypress verify

– name: Run Cypress tests
uses: cypress-io/github-action@v6
with:
browser: ${{ matrix.browsers }}
headed: false
record: true # Enable if you have Cypress Dashboard
parallel: true # Enable parallel test execution
env:
# Add your environment variables here
CYPRESS_BASE_URL: ${{ secrets.CYPRESS_BASE_URL }}
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}

– name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: cypress-results-${{ matrix.browsers }}
path: |
cypress/videos/
cypress/screenshots/
cypress/results/

– name: Generate test report
if: always()
run: npx mochawesome-merge cypress/results/*.json > cypress/results/report.json

– name: Upload test report
if: always()
uses: actions/upload-artifact@v3
with:
name: test-report-${{ matrix.browsers }}
path: cypress/results/report.json
Share the Knowledge

You May Also Like

About the Author: Sariful I.

Leave a Reply

Your email address will not be published. Required fields are marked *