This repo contains various recipes for testing common scenarios using Cypress.
| 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 |
| E2E Snapshots | Blogs | End-to-End Snapshot Testing |
| Codepen.io Testing | Blogs | Test a HyperApp Codepen demo |
| Vue + Vuex + REST Testing | Blogs | Test an application that uses central 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 |
Stubbing 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 app |
| Adding Chai Assertions | Extending Cypress | Add new or custom chai assertions |
| Bootstrapping your App | Server Communication | Seed your application with test data |
- This repo is structured similar to how other "Monorepos" work.
- Each
example projecthas it's own Cypress configuration, tests, backend and frontend assets. - Each of these
example projectsshare a single "root" Cypress that is installed in the rootnode_modulesfolder. - This structure looks different from normal projects, but its the easiest way to manage multiple projects without installing Cypress independently for each one.
## install all dependencies
npm install
## this will call 'npm start' on
## each example project's package.json
## which boots all of the webservers
npm start
## if you want to make modifications
## to the node server code and have
## the servers automatically restart
npm run dev## this opens the cypress test runner
## in the GUI mode. because this project
## is a monorepo - we've opened the test
## runner in 'global' mode.
##
## so to run a specific project you'll
## need to manually add the folder to Cypress.
npm run cypress:open
## alternatively, to open a specific
## example without running in global mode
cd ./examples/drag-n-drop
npm run cypress:open## runs all example projects and
## exits with the total number of
## failures across all projects
npm run cypress:run
### runs all example projects in specific browser
### similar to cypress run --browser <name>
npm run cypress:run -- --browser chrome
### sends test results, videos, screenshots
### to Cypress dashboard
npm run cypress:run -- --record
### run single example by name
npm run cypress:run -- --example blogs__codepen-demo
## switch the browser to chrome instead
## of the default headless Electron browser
npm run cypress:run:chrome
## alternatively, to run a specific
## example without running all projects
cd ./examples/drag-n-drop
npm run cypress:run- Import ES2015 modules.
- Require CommonJS modules.
- Organize reusable utility functions.
- Import 3rd party
node_modules.
- 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.
- Test a standard
username/passwordHTML 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().
- Test an AJAX backed
username/passwordform. - 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().
- 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.
- 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.
- Interact with elements that are hidden by CSS.
- Use
.invoke()and.trigger()to simulate hovering. - Trigger
mouseover,mouseout,mouseenter,mouseleaveevents. Get around the lack of a.hover()command.
- Use
.invoke()and.trigger()to test a range input (slider).
- Use
.trigger()to test drag-n-drop that uses mouse events. - Use
.trigger()to test drag-n-drop that uses drag events.
- Use
@cypress/browserify-preprocessorto write Cypress tests in Typescript
- Use
@cypress/webpack-preprocessorto write Cypress tests in Typescript - Lint TypeScript spec code against Cypress type definitions
- Blog article written here
- Programmatically control AngularJS
- Bypass the DOM, update scopes directly
- Create custom command for controlling services
- Blog article written here
- Use
cy.request()to perform API Testing - Use the Cypress GUI to help debug requests
- Adding
.snapshot()command by requiring 3rd party module - Capturing and saving snapshots of primitive values
- Testing central data Vuex store using snapshots
- Making assertions against a DOM element with
cy.get('...').snapshot()
- 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.
- 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
- Use
cy.stub()to stub dependencies in a unit test. - Handle promises returned by stubbed functions.
- Handle callbacks in stubbed functions.
- 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()andcy.tick()to control time. - Stub
window.fetchto control server responses.
- Use
cy.spy()to testwindow.openbehavior.
- Use
blacklistHoststo block Google Analytics from receiving requests. - Use
cy.stub()to verify thatwindow.ga(...)was called with the correct arguments
- Unit test your own application code libraries.
- Import modules using ES2015.
- Test simple math functions.
- Test the canonical fizzbuzz test.
- Unit test a React JSX Component using Enzyme.
- Import
enzymefromnode_modules. - Extend chai assertions with
chai-enzyme.
- Passing synthetic test file to upload via an
.trigger('change')event - Stub remote server using
cy.route() - Alternatively stub
axios.postmethod usingcy.stub()
- Extend
chaiwith thechai-date-stringassertion plugin. - Extend
chaiwith thechai-colorsassertion plugin. - Globally extend
chaifor all specs.
- Use
cy.visit()onBeforeLoadcallback. - Start your application with test data.
- Stub an XHR to seed with test data.
- Wait on an XHR to finish.