Recipes

Improve this doc

Recipes show you how to test common scenarios in Cypress.

https://github.com/cypress-io/cypress-example-recipes
Recipe Category Description
Node Modules Fundamentals Import your own Node modules
Single Sign On Logging In Log in across multiple servers or providers
HTML Web Forms Logging In Log in with a basic HTML form
XHR Web Forms Logging In Log in using an XHR
CSRF Tokens Logging In Log in with a required CSRF token
Tab Handling and Links Testing the DOM Links that open in a new tab
Hover and Hidden Elements Testing the DOM Test hidden elements requiring hover
Form Interactions Testing the DOM Test form elements like input type range
Drag and Drop Testing the DOM Use .trigger() to test drag and drop
TypeScript with Browserify Preprocessors Add TypeScript support with browserify
TypeScript with Webpack Preprocessors Add TypeScript support with webpack
Direct Control of AngularJS Blogs Bypass the DOM and control AngularJS
E2E API Testing Blogs Run your API Tests with a GUI
Codepen.io Testing Blogs Test a HyperApp Codepen demo
Redux Testing Blogs Test an application that uses central Redux data store
Vue + Vuex + REST Testing Blogs Test an application that uses central Vuex data store
Stubbing Functions Stubbing, Spying Use cy.stub() to test function calls
Stubbing `window.fetch` Stubbing, Spying Use cy.stub() to control fetch requests
Stub methods called on `window` Stubbing, Spying Use cy.stub() for methods called on window
Stubbing Google Analytics Stubbing, Spying Use cy.stub() to test Google Analytics calls
Application Code Unit Testing Import and test your own application code
React with Enzyme Unit Testing Test your react components in isolation
File Upload in React Unit Testing Test file upload in React application
Adding Chai Assertions Extending Cypress Add new or custom chai assertions
Bootstrapping your App Server Communication Seed your application with test data
Seeding your Database in Node Server Communication Seed your database with test data
Environment Variables Server Communication Pass environment variables to your tests
Cypress CircleCI Orb Continuous Integration Install, cache and run Cypress.io tests on CircleCI with minimal configuration.

Node Modules

  • Import ES2015 modules.
  • Require CommonJS modules.
  • Organize reusable utility functions.
  • Import 3rd party node_modules.

Single Sign On

  • Login when authentication is done on a 3rd party server.
  • Parse tokens using cy.request().
  • Manually set tokens on local storage.
  • Map external hosts and point to local servers.

HTML Web Forms

  • Test a standard username/password HTML form.
  • Test errors submitting invalid data.
  • Test unauthenticated redirects.
  • Authenticate users with cookies.
  • Create a custom cy.login() test command.
  • Bypass needing to use your actual UI.
  • Increase speed of testing with cy.request().

XHR Web Forms

  • Test an Ajax backed username/password form.
  • Test errors submitting invalid data.
  • Stub JSON based XHR requests.
  • Stub application functions.
  • Create a custom cy.login() test command.
  • Bypass needing to use your actual UI.
  • Increase speed of testing with cy.request().

CSRF Tokens

  • Use cy.request() to get around CSRF protections.
  • Parse CSRF tokens out of HTML.
  • Parse CSRF tokens out of response headers.
  • Expose CSRF via a route.
  • Disable CSRF when not in production.

Tab Handling and Links

  • Test anchor links opening in new tabs: <a target="_blank">.
  • Test anchor links that link to external domains: <a href="...">.
  • Prevent content from opening in a new tab.
  • Request external content that would open in a new tab using cy.request().
  • Speed up tests by reducing loading times.

Hover and Hidden Elements

  • Interact with elements that are hidden by CSS.
  • Use .invoke() and .trigger() to simulate hovering.
  • Trigger mouseover, mouseout, mouseenter, mouseleave events.
    Get around the lack of a .hover() command.

Form Interactions

Drag and Drop

  • Use .trigger() to test drag-n-drop that uses mouse events.
  • Use .trigger() to test drag-n-drop that uses drag events.

TypeScript with Browserify

TypeScript with Webpack

Direct Control of AngularJS

  • Blog article written here
  • Programmatically control AngularJS
  • Bypass the DOM, update scopes directly
  • Create custom command for controlling services

E2E API Testing

Codepen Testing

  • Load Codepen and get around iframe security restrictions.
  • Use cy.request() to load a document into test iframe.
  • Test HyperApp.js application through the DOM and through actions.

Redux Testing

  • control application via DOM and check the Redux store
  • use Redux actions directly from tests
  • load initial Redux state from a fixture file
  • use cypress-pipe plugin
  • use cypress-plugin-snapshots plugin

Vue + Vuex + REST Testing

  • Test a Vue.js web application that uses central data store
  • Mock REST calls to the server
  • Dispatch actions to the Vuex store
  • Test text file upload

Stubbing Functions

  • Use cy.stub() to stub dependencies in a unit test.
  • Handle promises returned by stubbed functions.
  • Handle callbacks in stubbed functions.

Stubbing window.fetch

  • Use cy.spy() to verify the behavior of a function.
  • Use cy.stub() to verify and control the behavior of a function.
  • Use cy.clock() and cy.tick() to control time.
  • Stub window.fetch to control server responses.

Stubbing methods called on window

  • Use cy.spy() to test window.open behavior.

Stubbing Google Analytics

  • Use blacklistHosts to block Google Analytics from receiving requests.
  • Use cy.stub() to verify that window.ga(...) was called with the correct arguments

Application Code

  • Unit test your own application code libraries.
  • Import modules using ES2015.
  • Test simple math functions.
  • Test the canonical fizzbuzz test.

React with Enzyme

  • Unit test a React JSX Component using Enzyme.
  • Import enzyme from node_modules.
  • Extend chai assertions with chai-enzyme.

File Upload in React

Adding Chai Assertions

Bootstrapping your App

  • Use cy.visit() onBeforeLoad callback.
  • Start your application with test data.
  • Stub an XHR to seed with test data.
  • Wait on an XHR to finish.

Seeding your Database in Node

  • Use cy.task() to communicate with Node via the pluginsFile.
  • Seed your database with test data.
  • Wrap your pluginsFile so you can require files that use ES modules (import/export).

Environment Variables

  • Pass values via env object in cypress.json.
  • Pass any variable that starts with CYPRESS_.
  • Extract any other variable from process.env using cypress/plugins/index.js callback.

Cypress CircleCI Orb

  • Installs npm dependencies
  • Run Cypress tests
  • Record the output to the Cypress Dashboard