Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents
minLevel1
maxLevel6
outlinefalse
styledecimal
typelist
printabletrue

Link to Cypress Documents for DST tools:

Testing with Cypress - Seeding Rate Calculator

Testing with Cypress - Species Selector

Testing with Cypress - PlantMap3D

Introduction

Why use Cypress?

  • Cypress allows us to write the following tests, while giving developers access to its interactive GUI

    • End-to-end tests

    • Component tests

    • Integration tests

    • Unit tests

  • Grab a coffee while you go through the features of Cypress.

Setup and Installation

For a quick start and detailed guidance on using Cypress, please refer to the documentation.

Installation Steps

Expand
titleRun the following command in your terminal

npm install cypress

...

Expand
titleTo open Cypress, run the following command

npx cypress open

image-20240822-161258.pngImage Added

Expand
titleFollow these steps in the cypress GUI
  1. Check for a new cypress window popup

image-20240822-161356.pngImage Added

  1. Select component testing

image-20240822-161422.pngImage Added

  1. For the Project Setup screen, do not change the detected framework (should be create react app) and select Next Step.

  2. Install all the dev dependencies (should be already installed, if not just npm install all the required dependencies) and select continue

  3. Next screen should show all the Configuration files, select continue

image-20240822-161636.pngImage Added
  1. Choose chrome as the browser and Start Component Testing in Chrome

image-20240822-161809.pngImage Added
Note

Check VSCODE for the project directory, you should see a new cypress folder created in the root folder. If not then the installation was not correct.

...

Code Block
languagejs
// code for adding a custom Cypress command
// @Params: [test-id]: string
// usecase: cy.assertByTestId("<test-id>");
// result: gives assertion for element existance
 
Cypress.Commands.add('assertByTestId', (testId) => {
  cy.get(`[data-cy=${testId}]`).should('exist');
});

Intercepting Network Calls

Whenever we write tests and find API calls that render same results, we should use cy.intercept() to intercept these calls and send back the a stored result. This can also be used when an API call has a large response body and the client may be using only a portion of it.

All such responses from API calls must be stored in the fixtures folder located at cypress/fixtures and used in the intercept command as follows:

Code Block
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData');

Running Tests

  • Open tests in GUI by using the command

  • Code Block
    languagebash
    npx cypress open --config-file={env}.config.js
  • To open tests in headless mode (run in terminal without the GUI) run the following command

  • Code Block
    languagebash
    npx cypress run --config-file dev.config.js // runs e2e tests
    npx cypress run --component --config-file=dev.config.js // runs component tests

...