0
0
Cypresstesting~15 mins

Cypress Dashboard (Cloud) service - Build an Automation Script

Choose your learning style9 modes available
Verify Cypress Dashboard records test run and shows correct test results
Preconditions (3)
Step 1: Run Cypress tests with the command 'npx cypress run --record --key <project-record-key>'
Step 2: Wait for tests to complete
Step 3: Log in to Cypress Dashboard at https://dashboard.cypress.io
Step 4: Navigate to the linked project
Step 5: Open the latest test run report
Step 6: Verify that the test run shows the correct number of tests passed and failed
Step 7: Verify that screenshots and videos (if enabled) are available for failed tests
✅ Expected Result: The Cypress Dashboard shows the test run with accurate pass/fail counts and displays screenshots/videos for failed tests.
Automation Requirements - Cypress
Assertions Needed:
Verify test run command completes successfully
Verify Dashboard API returns test run summary with correct pass/fail counts
Verify screenshots and videos URLs are present in Dashboard API response for failed tests
Best Practices:
Use Cypress environment variables for Dashboard record key
Use Cypress commands to run tests and capture results
Use API calls to Cypress Dashboard to verify test run data
Avoid hardcoding sensitive keys in code
Use explicit waits for API responses
Automated Solution
Cypress
/// <reference types="cypress" />

// This script runs Cypress tests with recording enabled and verifies Dashboard data via API

const dashboardProjectId = '<your-project-id>'
const dashboardRecordKey = Cypress.env('CYPRESS_RECORD_KEY')

// Step 1: Run tests with recording
// This is normally done outside test code, but we simulate here for demonstration

// Step 2: After tests run, verify Dashboard data via API

describe('Cypress Dashboard Verification', () => {
  it('should verify test run data on Cypress Dashboard', () => {
    // Call Cypress Dashboard API to get latest runs
    cy.request({
      method: 'GET',
      url: `https://dashboard.cypress.io/projects/${dashboardProjectId}/runs`,
      headers: {
        Authorization: `Bearer ${dashboardRecordKey}`
      }
    }).then((response) => {
      expect(response.status).to.eq(200)
      const runs = response.body.runs
      expect(runs).to.have.length.greaterThan(0)

      const latestRun = runs[0]
      expect(latestRun).to.have.property('stats')
      expect(latestRun.stats).to.have.property('tests')
      expect(latestRun.stats).to.have.property('passes')
      expect(latestRun.stats).to.have.property('failures')

      // Check that passes + failures equals total tests
      expect(latestRun.stats.passes + latestRun.stats.failures).to.eq(latestRun.stats.tests)

      // Verify screenshots and videos exist for failed tests
      if (latestRun.stats.failures > 0) {
        expect(latestRun).to.have.property('screenshots')
        expect(latestRun.screenshots.length).to.be.greaterThan(0)
        expect(latestRun).to.have.property('videos')
        expect(latestRun.videos.length).to.be.greaterThan(0)
      }
    })
  })
})

This test script uses Cypress to verify that the Cypress Dashboard service correctly records test runs.

First, it assumes tests have been run with the --record flag and a valid record key.

Then, it calls the Cypress Dashboard API to fetch the latest test runs for the project.

Assertions check that the API response is successful, that the latest run has test statistics, and that the sum of passes and failures equals total tests.

If there are failed tests, it verifies that screenshots and videos are present in the run data.

We use environment variables for sensitive keys and explicit assertions for clarity and reliability.

Common Mistakes - 3 Pitfalls
Hardcoding the Cypress Dashboard record key in the test code
Not waiting for the test run to complete before querying the Dashboard API
Using UI scraping to verify Dashboard data instead of API calls
Bonus Challenge

Now add data-driven testing to verify Dashboard test run data for 3 different projects with different record keys.

Show Hint