|
4 | 4 |
|
5 | 5 | This repo contains various recipes for testing common scenarios using Cypress.
|
6 | 6 |
|
7 |
| -# Contents |
8 |
| - |
9 |
| -- [Overview](#overview) |
10 |
| -- [Installation](#installation) |
11 |
| -- [Recipes](#recipes) |
12 |
| - - [ES2015 / CommonJS Modules](#es2015--commonjs-modules) |
13 |
| - - [Unit Test - Application Code](#unit-test---application-code) |
14 |
| - - [Unit Test - React w/Enzyme](#unit-test---react-wenzyme) |
15 |
| - - [Unit Test - Stubbing Dependencies](#unit-test---stubbing-dependencies) |
16 |
| - - [Logging In - HTML Web Form](#logging-in---html-web-form) |
17 |
| - - [Logging In - XHR Web Form](#logging-in---xhr-web-form) |
18 |
| - - [Logging In - CSRF Tokens](#logging-in---csrf-tokens) |
19 |
| - - [Logging In - Single Sign On](#logging-in---single-sign-on) |
20 |
| - - [Extending Chai with Assertion Plugins](#extending-chai-with-assertion-plugins) |
21 |
| - - [Tab Handling and Anchor Links](#tab-handling-and-anchor-links) |
22 |
| - - [Dealing with Hover and Hidden Elements](#dealing-with-hover-and-hidden-elements) |
23 |
| - - [Bootstrapping your App with Test Data](#bootstrapping-your-app-with-test-data) |
24 |
| - - [Controlling Behavior with Spies, Stubs, and Clocks](#controlling-behavior-with-spies-stubs-and-clocks) |
25 |
| - - [Form Interactions](#form-interactions) |
26 |
| - - [Drag 'n Drop](#drag-n-drop) |
27 |
| - |
28 |
| -# Overview |
29 |
| - |
30 |
| -- This is still a WIP, and we'll be adding recipes often. |
31 |
| -- All of the tests are found in the [`cypress/integration`](./cypress/integration) folder. |
32 |
| -- We boot a separate node server per recipe. |
33 |
| -- Each [`example`](./examples) has all of its own backend and frontend assets. |
34 |
| - |
35 |
| -# Installation |
| 7 | +Recipe | Category | Description |
| 8 | +--- | --- | --- |
| 9 | +[Node Modules](#node-modules) | Fundamentals | Import your own node modules |
| 10 | +[Single Sign On](#single-sign-on) | Logging In | Log in across multiple servers or providers |
| 11 | +[HTML Web Forms](#html-web-forms) | Logging In | Log in with a basic HTML form |
| 12 | +[XHR Web Forms](#xhr-web-forms) | Logging In | Log in using an XHR |
| 13 | +[CSRF Tokens](#csrf-tokens) | Logging In | Log in with a required CSRF token |
| 14 | +[Tab Handling and Links](#tab-handling-and-links) | Testing the DOM | Links that open in a new tab |
| 15 | +[Hover and Hidden Elements](#hover-and-hidden-elements) | Testing the DOM | Test hidden elements requiring hover |
| 16 | +[Form Interactions](#form-interactions) | Testing the DOM | Test form elements like input type `range` |
| 17 | +[Drag and Drop](#drag-and-drop) | Testing the DOM | Use `.trigger()` to test drag and drop |
| 18 | +[Stubbing Functions](#stubbing-functions) | Stubbing, Spying | Use `cy.stub()` to test function calls |
| 19 | +[Stubbing `window.fetch`](#stubbing-windowfetch) | Stubbing, Spying | Use `cy.stub()` to control fetch requests |
| 20 | +[Application Code](#application-code) | Unit Testing | Import and test your own application code |
| 21 | +[React with Enzyme](#react-with-enzyme) | Unit Testing | Test your react components in isolation |
| 22 | +[Adding Chai Assertions](#adding-chai-assertions) | Extending Cypress | Add new or custom chai assertions |
| 23 | +[Bootstrapping your App](#bootstrapping-your-app) | Server Communication | Seed your application with test data |
| 24 | + |
| 25 | +## Overview |
| 26 | + |
| 27 | +- This repo is structured similar to how other "Monorepos" work. |
| 28 | +- Each [`example project`](./examples) has it's own Cypress configuration, tests, backend and frontend assets. |
| 29 | +- Each of these [`example projects`](./examples) share a single "root" Cypress that is installed in the root `node_modules` folder. |
| 30 | +- This structure looks different from normal projects, but its the easiest way to manage multiple projects without installing Cypress independently for each one. |
| 31 | + |
| 32 | +## Installation |
36 | 33 |
|
37 | 34 | ```bash
|
38 | 35 | ## install all dependencies
|
39 | 36 | npm install
|
40 | 37 |
|
41 |
| -## boot the various node servers |
42 |
| -## to use in the tests |
| 38 | +## this will call 'npm start' on |
| 39 | +## each example project's package.json |
| 40 | +## which boots all of the webservers |
43 | 41 | npm start
|
44 | 42 |
|
45 |
| -## or if you want to make modifications |
| 43 | +## if you want to make modifications |
46 | 44 | ## to the node server code and have
|
47 | 45 | ## the servers automatically restart
|
48 | 46 | npm run dev
|
| 47 | +``` |
| 48 | + |
| 49 | +## Opening Cypress GUI |
| 50 | + |
| 51 | +```bash |
| 52 | +## this opens the cypress test runner |
| 53 | +## in the GUI mode. because this project |
| 54 | +## is a monorepo - we've opened the test |
| 55 | +## runner in 'global' mode. |
| 56 | +## |
| 57 | +## so to run a specific project you'll |
| 58 | +## need to manually add the folder to Cypress. |
| 59 | +npm run cypress:open |
49 | 60 |
|
50 |
| -## opens the cypress desktop app |
51 |
| -## to run tests in the interactive GUI |
| 61 | +## alternatively, to open a specific |
| 62 | +## example without running in global mode |
| 63 | +cd ./examples/drag-n-drop |
52 | 64 | npm run cypress:open
|
53 | 65 | ```
|
54 | 66 |
|
55 |
| -# Running from the CLI |
| 67 | +## Running from the CLI |
56 | 68 |
|
57 | 69 | ```bash
|
58 |
| -## runs all cypress tests from the CLI |
| 70 | +## runs all example projects and |
| 71 | +## exits with the total number of |
| 72 | +## failures across all projects |
59 | 73 | npm run cypress:run
|
60 | 74 |
|
61 | 75 | ## switch the browser to chrome instead
|
62 | 76 | ## of the default headless Electron browser
|
63 |
| -npm run cypress:run -- --browser chrome |
64 |
| -``` |
65 |
| - |
66 |
| -# Recipes |
67 |
| - |
68 |
| -### [ES2015 / CommonJS Modules](./cypress/integration/es2015_commonjs_modules_spec.js) |
69 |
| - |
70 |
| -**This recipe shows you how to:** |
71 |
| - |
72 |
| -- Import ES2015 modules |
73 |
| -- Require CommonJS modules |
74 |
| -- Organize reusable utility functions |
75 |
| -- Import 3rd party `node_modules` |
76 |
| - |
77 |
| -*** |
78 |
| - |
79 |
| -### [Unit Test - Application Code](./cypress/integration/unit_test_application_code_spec.js) |
80 |
| - |
81 |
| -**This recipe shows you how to:** |
82 |
| - |
83 |
| -- Unit test your own application code libraries |
84 |
| -- Import modules using ES2015 |
85 |
| -- Test simple math functions |
86 |
| -- Test the canonical *fizzbuzz* test |
87 |
| - |
88 |
| -*** |
89 |
| - |
90 |
| -### [Unit Test - React w/Enzyme](./cypress/integration/unit_test_react_enzyme_spec.js) |
91 |
| - |
92 |
| -**This recipe shows you how to:** |
93 |
| - |
94 |
| -- Unit test a React JSX Component using [Enzyme](http://airbnb.io/enzyme/) |
95 |
| -- Import `enzyme` from `node_modules` |
96 |
| -- Extend chai assertions with [`chai-enzyme`](https://github.com/producthunt/chai-enzyme) |
97 |
| - |
98 |
| -*** |
| 77 | +npm run cypress:run:chrome |
99 | 78 |
|
100 |
| -### [Unit Test - Stubbing Dependencies](./cypress/integration/unit_test_stubbing_dependencies_spec.js) |
101 |
| - |
102 |
| -**This recipe shows you how to:** |
103 |
| - |
104 |
| -- Use [`cy.stub`](https://on.cypress.io/api/stub) to stub dependencies in a unit test |
105 |
| -- Handle promises returned by stubbed functions |
106 |
| -- Handle callbacks in stubbed functions |
107 |
| - |
108 |
| -*** |
109 |
| - |
110 |
| -### [Logging In - HTML Web Form](./cypress/integration/logging_in_html_web_form_spec.js) |
111 |
| - |
112 |
| -**This recipe shows you how to:** |
113 |
| - |
114 |
| -- Test a standard `username/password` HTML form |
115 |
| -- Test errors submitting invalid data |
116 |
| -- Test unauthenticated redirects |
117 |
| -- Authenticate users with cookies |
118 |
| -- Create a custom `login` test command |
119 |
| -- Bypass needing to use your actual UI |
120 |
| -- Increase speed of testing with [`cy.request`](https://on.cypress.io/api/request) |
121 |
| - |
122 |
| -*** |
123 |
| - |
124 |
| -### [Logging In - XHR Web Form](./cypress/integration/logging_in_xhr_web_form_spec.js) |
125 |
| - |
126 |
| -**This recipe shows you how to:** |
127 |
| - |
128 |
| -- Test an AJAX backed `username/password` form |
129 |
| -- Test errors submitting invalid data |
130 |
| -- Stub JSON based XHR requests |
131 |
| -- Stub application functions |
132 |
| -- Create a custom `login` test command |
133 |
| -- Bypass needing to use your actual UI |
134 |
| -- Increase speed of testing with [`cy.request`](https://on.cypress.io/api/request) |
135 |
| - |
136 |
| -*** |
137 |
| - |
138 |
| -### [Logging In - CSRF Tokens](./cypress/integration/logging_in_csrf_tokens_spec.js) |
139 |
| - |
140 |
| -**This recipe shows you how to:** |
141 |
| - |
142 |
| -- Use [`cy.request`](https://on.cypress.io/api/request) to get around CSRF protections |
143 |
| -- Parse CSRF tokens out of HTML |
144 |
| -- Parse CSRF tokens out of response headers |
145 |
| -- Expose CSRF via a route |
146 |
| -- Disable CSRF when not in production |
147 |
| - |
148 |
| -*** |
149 |
| - |
150 |
| -### [Logging In - Single Sign On](./cypress/integration/logging_in_single_sign_on_spec.js) |
| 79 | +## alternatively, to run a specific |
| 80 | +## example without running all projects |
| 81 | +cd ./examples/drag-n-drop |
| 82 | +npm run cypress:run |
| 83 | +``` |
151 | 84 |
|
152 |
| -**This recipe shows you how to:** |
| 85 | +## Recipes |
153 | 86 |
|
154 |
| -- Login when authentication is done on a 3rd party server |
155 |
| -- Parse tokens using [`cy.request`](https://on.cypress.io/api/request) |
156 |
| -- Manually set tokens on local storage |
157 |
| -- Map external hosts and point to local servers |
| 87 | +### [Node Modules](./examples/fundamentals__node-modules) |
158 | 88 |
|
159 |
| -*** |
| 89 | +- Import ES2015 modules. |
| 90 | +- Require CommonJS modules. |
| 91 | +- Organize reusable utility functions. |
| 92 | +- Import 3rd party `node_modules`. |
160 | 93 |
|
161 |
| -### [Extending Chai with Assertion Plugins](./cypress/integration/extending_chai_assertion_plugins_spec.js) |
| 94 | +### [Single Sign On](./examples/logging-in__single-sign-on) |
162 | 95 |
|
163 |
| -**This recipe shows you how to:** |
| 96 | +- Login when authentication is done on a 3rd party server. |
| 97 | +- Parse tokens using [`cy.request()`](https://on.cypress.io/request). |
| 98 | +- Manually set tokens on local storage. |
| 99 | +- Map external hosts and point to local servers. |
164 | 100 |
|
165 |
| -- Extend [`chai`](http://chaijs.com/) with the [`chai-date-string`](http://chaijs.com/plugins/chai-date-string/) assertion plugin |
166 |
| -- Extend [`chai`](http://chaijs.com/) with the [`chai-colors`](http://chaijs.com/plugins/chai-colors/) assertion plugin |
167 |
| -- Globally extend [`chai`](http://chaijs.com/) for all specs |
| 101 | +### [HTML Web Forms](./examples/logging-in__html-web-forms) |
168 | 102 |
|
169 |
| -*** |
| 103 | +- Test a standard `username/password` HTML form. |
| 104 | +- Test errors submitting invalid data. |
| 105 | +- Test unauthenticated redirects. |
| 106 | +- Authenticate users with cookies. |
| 107 | +- Create a custom `cy.login()` test command. |
| 108 | +- Bypass needing to use your actual UI. |
| 109 | +- Increase speed of testing with [`cy.request()`](https://on.cypress.io/request). |
170 | 110 |
|
171 |
| -### [Tab Handling and Anchor Links](./cypress/integration/tab_handling_anchor_links_spec.js) |
| 111 | +### [XHR Web Forms](./examples/logging-in__xhr-web-forms) |
172 | 112 |
|
173 |
| -**This recipe shows you how to:** |
| 113 | +- Test an AJAX backed `username/password` form. |
| 114 | +- Test errors submitting invalid data. |
| 115 | +- Stub JSON based XHR requests. |
| 116 | +- Stub application functions. |
| 117 | +- Create a custom `cy.login()` test command. |
| 118 | +- Bypass needing to use your actual UI. |
| 119 | +- Increase speed of testing with [`cy.request()`](https://on.cypress.io/request). |
174 | 120 |
|
175 |
| -- Test anchor links opening in new tabs: `<a target="_blank">` |
176 |
| -- Test anchor links that link to external domains: `<a href="...">` |
177 |
| -- Prevent content from opening in a new tab |
178 |
| -- Request external content that would open in a new tab |
179 |
| -- Speed up tests by reducing loading times |
| 121 | +### [CSRF Tokens](./examples/logging-in__csrf-tokens) |
180 | 122 |
|
181 |
| -*** |
| 123 | +- Use [`cy.request()`](https://on.cypress.io/request) to get around CSRF protections. |
| 124 | +- Parse CSRF tokens out of HTML. |
| 125 | +- Parse CSRF tokens out of response headers. |
| 126 | +- Expose CSRF via a route. |
| 127 | +- Disable CSRF when not in production. |
182 | 128 |
|
183 |
| -### [Dealing with Hover and Hidden Elements](./cypress/integration/hover_hidden_elements_spec.js) |
| 129 | +### [Tab Handling and Links](./examples/testing-dom__tab-handling-links) |
184 | 130 |
|
185 |
| -**This recipe shows you how to:** |
| 131 | +- Test anchor links opening in new tabs: `<a target="_blank">`. |
| 132 | +- Test anchor links that link to external domains: `<a href="...">`. |
| 133 | +- Prevent content from opening in a new tab. |
| 134 | +- Request external content that would open in a new tab using [`cy.request()`](https://on.cypress.io/request). |
| 135 | +- Speed up tests by reducing loading times. |
186 | 136 |
|
187 |
| -- Interact with elements which are hidden by CSS |
188 |
| -- Use [`.invoke`](https://on.cypress.io/invoke) and [`.trigger`](https://on.cypress.io/trigger) to simulate hovering |
189 |
| -- Trigger `mouseover`, `mouseout`, `mouseenter`, `mouseleave` events |
190 |
| -- Get around the lack of a `cy.hover` command |
| 137 | +### [Hover and Hidden Elements](./examples/testing-dom__hover-hidden-elements) |
191 | 138 |
|
192 |
| -*** |
| 139 | +- Interact with elements that are hidden by CSS. |
| 140 | +- Use [`.invoke()`](https://on.cypress.io/invoke) and [`.trigger()`](https://on.cypress.io/trigger) to simulate hovering. |
| 141 | +- Trigger `mouseover`, `mouseout`, `mouseenter`, `mouseleave` events. |
| 142 | +Get around the lack of a `.hover()` command. |
193 | 143 |
|
194 |
| -### [Bootstrapping your App with Test Data](./cypress/integration/bootstrapping_app_test_data_spec.js) |
| 144 | +### [Form Interactions](./examples/testing-dom__form-interactions) |
195 | 145 |
|
196 |
| -**This recipe shows you how to:** |
| 146 | +- Use [`.invoke()`](https://on.cypress.io/invoke) and [`.trigger()`](https://on.cypress.io/trigger) to test a range input (slider). |
197 | 147 |
|
198 |
| -- Use [`cy.visit`](https://on.cypress.io/api/visit) `onBeforeLoad` callback |
199 |
| -- Start your application with test data |
200 |
| -- Stub an XHR to seed with test data |
201 |
| -- Wait on an XHR to finish |
| 148 | +### [Drag and Drop](./examples/testing-dom__drag-drop) |
202 | 149 |
|
203 |
| -*** |
| 150 | +- Use [`.trigger()`](https://on.cypress.io/trigger) to test drag-n-drop that uses mouse events. |
| 151 | +- Use [`.trigger()`](https://on.cypress.io/trigger) to test drag-n-drop that uses drag events. |
204 | 152 |
|
205 |
| -### [Controlling Behavior with Spies, Stubs and Clocks](./cypress/integration/spy_stub_clock_spec.js) |
| 153 | +### [Stubbing Functions](./examples/stubbing-spying__functions) |
206 | 154 |
|
207 |
| -**This recipe shows you how to:** |
| 155 | +- Use [`cy.stub()`](https://on.cypress.io/stub) to stub dependencies in a unit test. |
| 156 | +- Handle promises returned by stubbed functions. |
| 157 | +- Handle callbacks in stubbed functions. |
208 | 158 |
|
209 |
| -- Use [`cy.spy`](https://on.cypress.io/spy) to verify the behavior of a function |
210 |
| -- Use [`cy.stub`](https://on.cypress.io/stub) to verify and control the behavior of a function |
211 |
| -- Use [`cy.clock`](https://on.cypress.io/clock) and [`cy.tick`](https://on.cypress.io/tick) to control time |
212 |
| -- Stub `window.fetch` to control server responses |
| 159 | +### [Stubbing `window.fetch`](./examples/stubbing-spying__window-fetch) |
213 | 160 |
|
214 |
| -*** |
| 161 | +- Use [`cy.spy()`](https://on.cypress.io/spy) to verify the behavior of a function. |
| 162 | +- Use [`cy.stub()`](https://on.cypress.io/stub) to verify and control the behavior of a function. |
| 163 | +- Use [`cy.clock()`](https://on.cypress.io/clock) and [`cy.tick()`](https://on.cypress.io/tick) to control time. |
| 164 | +- Stub `window.fetch` to control server responses. |
215 | 165 |
|
216 |
| -### [Form Interactions](./cypress/integration/form_interactions_spec.js) |
| 166 | +### [Application Code](./examples/unit-testing__application-code) |
217 | 167 |
|
218 |
| -This recipe shows you how to: |
| 168 | +- Unit test your own application code libraries. |
| 169 | +- Import modules using ES2015. |
| 170 | +- Test simple math functions. |
| 171 | +- Test the canonical *fizzbuzz* test. |
219 | 172 |
|
220 |
| -- Use [`.invoke`](https://on.cypress.io/invoke) and [`.trigger`](https://on.cypress.io/trigger) to test a range input (slider) |
| 173 | +### [React with Enzyme](./examples/unit-testing__react-enzyme) |
221 | 174 |
|
222 |
| -*** |
| 175 | +- Unit test a React JSX Component using [Enzyme](http://airbnb.io/enzyme/). |
| 176 | +- Import `enzyme` from `node_modules`. |
| 177 | +- Extend chai assertions with [`chai-enzyme`](https://github.com/producthunt/chai-enzyme). |
223 | 178 |
|
224 |
| -### [Drag 'n Drop](./cypress/integration/drag_n_drop_spec.js) |
| 179 | +### [Adding Chai Assertions](./examples/extending-cypres__chai-assertions) |
225 | 180 |
|
226 |
| -This recipe shows you how to: |
| 181 | +- Extend [`chai`](http://chaijs.com/) with the [`chai-date-string`](http://chaijs.com/plugins/chai-date-string/) assertion plugin. |
| 182 | +- Extend [`chai`](http://chaijs.com/) with the [`chai-colors`](http://chaijs.com/plugins/chai-colors/) assertion plugin. |
| 183 | +- Globally extend [`chai`](http://chaijs.com/) for all specs. |
227 | 184 |
|
228 |
| -- Use [`.trigger`](https://on.cypress.io/trigger) to test drag-n-drop that uses mouse events |
229 |
| -- Use [`.trigger`](https://on.cypress.io/trigger) to test drag-n-drop that uses drag events |
| 185 | +### [Bootstrapping your App](./examples/server-communication__bootstrapping-your-app) |
230 | 186 |
|
231 |
| -*** |
| 187 | +- Use [`cy.visit()`](https://on.cypress.io/visit) `onBeforeLoad` callback. |
| 188 | +- Start your application with test data. |
| 189 | +- Stub an XHR to seed with test data. |
| 190 | +- Wait on an XHR to finish. |
0 commit comments