Running Cypress Tests in Parallel on Multiple GitHub Actions Containers Recorded on Cypress Cloud

| 4 min read

This post walks through configuring your existing Cypress tests to execute in parallel on multiple GitHub Actions containers recorded on Cypress Cloud.

Cypress Cloud is a paid tool which supports useful test reports and debugging. Check here for the pricing. If you are just looking for a way to run tests in parallel you may want to consider a free Cypress plugin called cypress-split.

TL;DR

See this repo for an example solution running Cypress tests in parallel on multiple GitHub Actions containers recorded on Cypress Cloud.

For these steps I will be referencing this example Cypress repo which runs tests from 3 files.

Cypress Cloud Account

If you do not have an account yet head to cloud.cypress.io and create your free account.

Once you account has been completed add the project ID created into your cypress.config.js file. You can find your project ID in the project settings on Cypress Cloud -- also take note of the Record Keys from this page.

module.exports = {
...
  "projectId": "YOUR_PROJECT_ID"
...
}

Create GitHub Workflow

Below is an example of a GitHub Action Workflow which uses the marketplace cypress-io action.

You can see the strategy is set as 2 contains and has inputs to record the test run on Cypress Cloud and to run the tests in parallel.

Cypress Cloud will recognise each container are from the same test run by the environment variable GITHUB_TOKEN which is a unique token for the action build.

The environment variable CYPRESS_RECORD_KEY you need to set manually as a secret in your GitHub repo settings. It's the record key you can get from your Cypress Cloud project settings.

If you don't have a workflow created yet save the below snippet in .github/workflows/main.yml.

name: Cypress Tests
on:
  push:
    branches:
      - main
  schedule:
    - cron: '0 13 * * 0'
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2]
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
      - name: Cypress Tests
        uses: cypress-io/github-action@v6
        with:
          record: true
          parallel: true
        env:
          CYPRESS_RECORD_KEY: # environment variable called CYPRESS_RECORD_KEY see https://github.com/testingnotebook/cypress-cloud-example/blob/main/.github/workflows/main.yml
          GITHUB_TOKEN: # environment variable called GITHUB_TOKEN see https://github.com/testingnotebook/cypress-cloud-example/blob/main/.github/workflows/main.yml

If you are working with GitHub Actions on VSCode you can install an extension like GitHub Actions by Mathieu Dutour to help with the validation. Or you can edit the workflow file directly from GitHubs online editor.

Test Your Workflow

In the e2e directory there are 3 spec files called about.cy.ts, homepage.cy.ts and post.cy.ts so one container should run 2 tests and the other should run 1 test.

Below is the output from both containers. This shows the first container ran the homepage.cy.ts file and the second ran the about.cy.ts and post.cy.ts files which is as expected.

Build 1

====================================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        13.3.0                                                                         │
  │ Browser:        Electron 114 (headless)                                                        │
  │ Node Version:   v20.5.0 (/home/runner/runners/2.309.0/externals/node20/bin/node)               │
  │ Specs:          3 found (about.cy.js, homepage.cy.js, post.cy.js)                              │
  │ Searched:       cypress/e2e/**/*.cy.{js,jsx,ts,tsx}                                            │
  │ Params:         Tag: false, Group: false, Parallel: true                                       │
  │ Run URL:        https://cloud.cypress.io/projects/testingnotebook/runs/1                                │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  homepage.cy.js                                                                  (2 of 3)


  home page - recent posts
    ✓ should contain recent posts elements (1680ms)


  1 passing (2s)


  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        1                                                                                │
  │ Passing:      1                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  0                                                                                │
  │ Video:        false                                                                            │
  │ Duration:     1 second                                                                         │
  │ Spec Ran:     homepage.cy.js                                                                   │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


  (Uploading Cloud Artifacts)

  - Video - Nothing to upload
  - Screenshot - Nothing to upload
  - Test Replay - 919 kB

  (Uploaded Cloud Artifacts)

  - Test Replay - Done Uploading 919 kB 1/1

====================================================================================================

  (Run Finished)


       Spec                                              Tests  Passing  Failing  Pending  Skipped
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ ✔  homepage.cy.js                           00:01        1        1        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    ✔  All specs passed!                        00:01        1        1        -        -        -


───────────────────────────────────────────────────────────────────────────────────────────────────────

  Recorded Run: https://cloud.cypress.io/projects/testingnotebook/runs/1

Build 2

====================================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        13.3.0                                                                         │
  │ Browser:        Electron 114 (headless)                                                        │
  │ Node Version:   v20.5.0 (/home/runner/runners/2.309.0/externals/node20/bin/node)               │
  │ Specs:          3 found (about.cy.js, homepage.cy.js, post.cy.js)                              │
  │ Searched:       cypress/e2e/**/*.cy.{js,jsx,ts,tsx}                                            │
  │ Params:         Tag: false, Group: false, Parallel: true                                       │
  │ Run URL:        https://cloud.cypress.io/projects/testingnotebook/runs/1                                │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  about.cy.js                                                                     (1 of 3)


  about
    ✓ should avatar and author (2545ms)


  1 passing (3s)


  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        1                                                                                │
  │ Passing:      1                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  0                                                                                │
  │ Video:        false                                                                            │
  │ Duration:     2 seconds                                                                        │
  │ Spec Ran:     about.cy.js                                                                      │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


  (Uploading Cloud Artifacts)

  - Video - Nothing to upload
  - Screenshot - Nothing to upload
  - Test Replay - 1.02 MB

  (Uploaded Cloud Artifacts)

  - Test Replay - Done Uploading 1.02 MB 1/1

────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  post.cy.js                                                                      (3 of 3)


  post
    ✓ should contain recent posts elements (1236ms)


  1 passing (1s)


  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        1                                                                                │
  │ Passing:      1                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  0                                                                                │
  │ Video:        false                                                                            │
  │ Duration:     1 second                                                                         │
  │ Spec Ran:     post.cy.js                                                                       │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


  (Uploading Cloud Artifacts)

  - Video - Nothing to upload
  - Screenshot - Nothing to upload
  - Test Replay - 949 kB

  (Uploaded Cloud Artifacts)

  - Test Replay - Done Uploading 949 kB 1/1

====================================================================================================

  (Run Finished)


       Spec                                              Tests  Passing  Failing  Pending  Skipped
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ ✔  about.cy.js                              00:02        1        1        -        -        - │
  ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  │ ✔  post.cy.js                               00:01        1        1        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    ✔  All specs passed!                        00:04        2        2        -        -        -


───────────────────────────────────────────────────────────────────────────────────────────────────────

  Recorded Run: https://cloud.cypress.io/projects/testingnotebook/runs/1

On the GitHub action summary page there is also a test report for each container and of course the test results are also recorded on Cypress Cloud where you can replay the failing tests for debugging.

You can see the output from this GitHub Action Workflow here .

Running Test on Cypress Cloud from Local Machine

If you would like to run the tests locally and record on Cypress Cloud you can do this by running the following command in your terminal.

Set the ci-build-id switch value as something unique for each test run. Note you can run the same exact command in multiple terminals for parallel execution.

npx cypress run --key YOUR_PROJECT_RECORD_KEY --parallel --record --ci-build-id 1

For more information on setting up Cypress Cloud see their official documentation here.

The code used for this post can be found here.