codeceptjs 2.4.3 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +117 -0
  2. package/README.md +32 -7
  3. package/bin/codecept.js +3 -0
  4. package/docs/basics.md +11 -5
  5. package/docs/bdd.md +4 -4
  6. package/docs/build/MockRequest.js +3 -0
  7. package/docs/build/Nightmare.js +10 -2
  8. package/docs/build/Playwright.js +3187 -0
  9. package/docs/build/Protractor.js +16 -2
  10. package/docs/build/Puppeteer.js +126 -19
  11. package/docs/build/REST.js +3 -1
  12. package/docs/build/TestCafe.js +11 -3
  13. package/docs/build/WebDriver.js +361 -28
  14. package/docs/changelog.md +116 -0
  15. package/docs/configuration.md +2 -2
  16. package/docs/custom-helpers.md +55 -10
  17. package/docs/helpers/Appium.md +81 -1
  18. package/docs/helpers/MockRequest.md +281 -38
  19. package/docs/helpers/Nightmare.md +10 -2
  20. package/docs/helpers/Playwright.md +1770 -0
  21. package/docs/helpers/Protractor.md +15 -3
  22. package/docs/helpers/Puppeteer-firefox.md +32 -1
  23. package/docs/helpers/Puppeteer.md +126 -76
  24. package/docs/helpers/TestCafe.md +10 -2
  25. package/docs/helpers/WebDriver.md +208 -118
  26. package/docs/locators.md +2 -0
  27. package/docs/playwright.md +306 -0
  28. package/docs/plugins.md +103 -0
  29. package/docs/reports.md +12 -0
  30. package/docs/shadow.md +68 -0
  31. package/docs/visual.md +0 -73
  32. package/docs/webapi/forceClick.mustache +27 -0
  33. package/docs/webapi/seeInPopup.mustache +7 -0
  34. package/docs/webapi/setCookie.mustache +10 -2
  35. package/docs/webapi/type.mustache +12 -0
  36. package/docs/webdriver.md +7 -3
  37. package/lib/codecept.js +1 -1
  38. package/lib/command/definitions.js +2 -2
  39. package/lib/command/generate.js +4 -4
  40. package/lib/command/gherkin/snippets.js +4 -4
  41. package/lib/command/init.js +1 -1
  42. package/lib/command/interactive.js +3 -0
  43. package/lib/command/run-multiple.js +2 -2
  44. package/lib/command/run-rerun.js +2 -0
  45. package/lib/command/run-workers.js +22 -8
  46. package/lib/command/run.js +2 -0
  47. package/lib/command/workers/runTests.js +1 -0
  48. package/lib/container.js +1 -1
  49. package/lib/event.js +2 -0
  50. package/lib/helper/MockRequest.js +3 -0
  51. package/lib/helper/Playwright.js +2422 -0
  52. package/lib/helper/Protractor.js +1 -2
  53. package/lib/helper/Puppeteer.js +84 -19
  54. package/lib/helper/REST.js +3 -1
  55. package/lib/helper/TestCafe.js +1 -1
  56. package/lib/helper/WebDriver.js +313 -26
  57. package/lib/helper/extras/PlaywrightPropEngine.js +53 -0
  58. package/lib/helper/scripts/isElementClickable.js +54 -14
  59. package/lib/interfaces/gherkin.js +1 -1
  60. package/lib/listener/helpers.js +3 -0
  61. package/lib/locator.js +5 -0
  62. package/lib/mochaFactory.js +12 -10
  63. package/lib/plugin/allure.js +8 -1
  64. package/lib/plugin/autoDelay.js +1 -8
  65. package/lib/plugin/commentStep.js +133 -0
  66. package/lib/plugin/screenshotOnFail.js +3 -10
  67. package/lib/plugin/selenoid.js +2 -2
  68. package/lib/plugin/standardActingHelpers.js +13 -0
  69. package/lib/plugin/stepByStepReport.js +1 -8
  70. package/lib/plugin/wdio.js +10 -1
  71. package/lib/reporter/cli.js +30 -1
  72. package/lib/session.js +7 -4
  73. package/package.json +13 -10
  74. package/typings/Mocha.d.ts +567 -16
  75. package/typings/index.d.ts +9 -5
  76. package/typings/types.d.ts +1634 -74
@@ -1,45 +1,166 @@
1
1
  ---
2
2
  permalink: /helpers/MockRequest
3
- editLink: false
4
3
  sidebar: auto
5
4
  title: MockRequest
6
5
  ---
7
6
 
7
+ # MockRequest
8
+
9
+
8
10
  <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
9
11
 
10
12
  ## MockRequest
11
13
 
12
- **Extends Helper**
13
-
14
14
  This helper allows to **mock requests while running tests in Puppeteer or WebDriver**.
15
15
  For instance, you can block calls to 3rd-party services like Google Analytics, CDNs.
16
16
  Another way of using is to emulate requests from server by passing prepared data.
17
17
 
18
- ### Installations
18
+ MockRequest helper works in these [modes][1]:
19
19
 
20
- Requires [Polly.js][1] library by Netflix installed
20
+ - passthrough (default) - mock prefefined HTTP requests
21
+ - record - record all requests into a file
22
+ - replay - replay all recorded requests from a file
23
+
24
+ Combining record/replay modes allows testing websites with large datasets.
25
+
26
+ To use in passthrough mode set rules to mock requests and they will be automatically intercepted and replaced:
27
+
28
+ ```js
29
+ // default mode
30
+ I.mockRequest('GET', '/api/users', '[]');
31
+ ```
32
+
33
+ In record-replay mode start mocking to make HTTP requests recorded/replayed, and stop when you don't need to block requests anymore:
34
+
35
+ ```js
36
+ // record or replay all XHR for /users page
37
+ I.startMocking();
38
+ I.amOnPage('/users');
39
+ I.stopMocking();
40
+ ```
41
+
42
+ ### Installations
21
43
 
22
- npm i @pollyjs/core @pollyjs/adapter-puppeteer --save-dev
44
+ npm i @codeceptjs/mock-request --save-dev
23
45
 
24
46
  Requires Puppeteer helper or WebDriver helper enabled
25
47
 
26
48
  ### Configuration
27
49
 
28
- Just enable helper in config file:
50
+ #### With Puppeteer
51
+
52
+ Enable helper in config file:
29
53
 
30
54
  ```js
31
55
  helpers: {
32
56
  Puppeteer: {
33
57
  // regular Puppeteer config here
34
58
  },
35
- MockRequest: {}
59
+ MockRequestHelper: {
60
+ require: '@codeceptjs/mock-request',
61
+ }
36
62
  }
37
63
  ```
38
64
 
39
- > Partially works with WebDriver helper
40
-
41
65
  [Polly config options][2] can be passed as well:
42
66
 
67
+ ```js
68
+ // sample options
69
+ helpers: {
70
+ MockRequestHelper: {
71
+ require: '@codeceptjs/mock-request',
72
+ mode: record,
73
+ recordIfMissing: true,
74
+ recordFailedRequests: false,
75
+ expiresIn: null,
76
+ persisterOptions: {
77
+ keepUnusedRequests: false
78
+ fs: {
79
+ recordingsDir: './data/requests',
80
+ },
81
+ },
82
+ }
83
+ }
84
+ ```
85
+
86
+ * * *
87
+
88
+ **TROUBLESHOOTING**: Puppeteer does not mock requests in headless mode:
89
+
90
+ Problem: request mocking does not work and in debug mode you see this in output:
91
+
92
+ Access to fetch at {url} from origin {url} has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
93
+
94
+ Solution: update Puppeteer config to include `--disable-web-security` arguments:
95
+
96
+ ```js
97
+ Puppeteer: {
98
+ show: false,
99
+ chrome: {
100
+ args: [
101
+ '--disable-web-security',
102
+ ],
103
+ },
104
+ },
105
+ ```
106
+
107
+ * * *
108
+
109
+ #### With WebDriver
110
+
111
+ This helper partially works with WebDriver. It can intercept and mock requests **only on already loaded page**.
112
+
113
+ ```js
114
+ helpers: {
115
+ WebDriver: {
116
+ // regular WebDriver config here
117
+ },
118
+ MockRequestHelper: {
119
+ require: '@codeceptjs/mock-request',
120
+ }
121
+ }
122
+ ```
123
+
124
+ > Record/Replay mode is not tested in WebDriver but technically can work with [REST Persister][3]
125
+
126
+ ## Usage
127
+
128
+ ### 👻 Mock Requests
129
+
130
+ To intercept API requests and mock them use following API
131
+
132
+ - [startMocking()][4] - to enable request interception
133
+ - [mockRequest()][5] - to define mock in a simple way
134
+ - [mockServer()][6] - to use PollyJS server API to define complex mocks
135
+ - [stopMocking()][7] - to stop intercepting requests and disable mocks.
136
+
137
+ Calling `mockRequest` or `mockServer` will start mocking, if it was not enabled yet.
138
+
139
+ ```js
140
+ I.startMocking(); // optionally
141
+ I.mockRequest('/google-analytics/*path', 200);
142
+ // return an empty successful response
143
+ I.mockRequest('GET', '/api/users', 200);
144
+ // mock users api
145
+ I.mockServer(server => {
146
+ server.get('https://server.com/api/users*').
147
+ intercept((req, res) => { res.status(200).json(users);
148
+ });
149
+ });
150
+ I.click('Get users);
151
+ I.stopMocking();
152
+ ```
153
+
154
+ ### 📼 Record & Replay
155
+
156
+ > At this moment works only with Puppeteer
157
+
158
+ Record & Replay mode allows you to record all xhr & fetch requests and save them to file.
159
+ On next runs those requests can be replayed.
160
+ By default, it stores all passed requests, but this behavior can be customized with `I.mockServer`
161
+
162
+ Set mode via enironment variable, `replay` mode by default:
163
+
43
164
  ```js
44
165
  // enable replay mode
45
166
  helpers: {
@@ -47,39 +168,52 @@ helpers: {
47
168
  // regular Puppeteer config here
48
169
  },
49
170
  MockRequest: {
50
- mode: 'replay',
171
+ require: '@codeceptjs/mock-request',
172
+ mode: process.env.MOCK_MODE || 'replay',
51
173
  },
52
174
  }
53
175
  ```
54
176
 
55
- ### Usage
177
+ Interactions between `I.startMocking()` and `I.stopMocking()` will be recorded and saved to `data/requests` directory.
56
178
 
57
- Use `I.mockRequest` to intercept and mock requests.
179
+ ```js
180
+ I.startMocking() // record requests under 'Test' name
181
+ I.startMocking('users') // record requests under 'users' name
182
+ ```
58
183
 
59
- ### Parameters
184
+ Use `I.mockServer()` to customize which requests should be recorded and under which name:
60
185
 
61
- - `config`
186
+ ```js
187
+ I.startMocking();
188
+ I.mockServer((server) => {
189
+ // mock request only from ap1.com and api2.com and
190
+ // store recording into two different files
191
+ server.any('https://api1.com/*').passthrough(false).recordingName('api1');
192
+ server.any('https://api2.com/*').passthrough(false).recordingName('api2');
193
+ });
194
+ ```
62
195
 
63
- ### _checkAndStartMocking
196
+ To stop request recording/replaying use `I.stopMocking()`.
64
197
 
65
- Starts mocking if it's not started yet.
198
+ 🎥 To record HTTP interactions execute tests with MOCK_MODE environment variable set as "record":
66
199
 
67
- ### _connectPuppeteer
200
+ MOCK_MODE=record npx codeceptjs run --debug
68
201
 
69
- Creates a polly instance by registering puppeteer adapter with the instance
202
+ 📼 To replay them launch tests without environment variable:
70
203
 
71
- #### Parameters
204
+ npx codeceptjs run --debug
72
205
 
73
- - `title` **any**
206
+ ### Parameters
74
207
 
75
- ### _connectWebDriver
208
+ - `config`
76
209
 
77
- Creates polly object in the browser window context using xhr and fetch adapters,
78
- after loading PollyJs and adapter scripts.
210
+ ### flushMocking
79
211
 
80
- #### Parameters
212
+ Waits for all requests handled by MockRequests to be resolved:
81
213
 
82
- - `title` **any**
214
+ ```js
215
+ I.flushMocking();
216
+ ```
83
217
 
84
218
  ### mockRequest
85
219
 
@@ -100,33 +234,142 @@ I.mockRequest('GET', ['/secrets', '/v2/secrets'], 403);
100
234
 
101
235
  #### Parameters
102
236
 
103
- - `method` **[string][3]** request method. Can be `GET`, `POST`, `PUT`, etc or `ANY`.
104
- - `oneOrMoreUrls` **([string][3] | [Array][4]&lt;[string][3]>)** url(s) to mock. Can be exact URL, a pattern, or an array of URLs.
105
- - `dataOrStatusCode` **([number][5] | [string][3] | [object][6])** status code when number provided. A response body otherwise
106
- - `additionalData` **([string][3] | [object][6])** response body when a status code is set by previous parameter.
237
+ - `method` **[string][8]** request method. Can be `GET`, `POST`, `PUT`, etc or `ANY`.
238
+ - `oneOrMoreUrls` **([string][8] \| [Array][9]&lt;[string][8]>)** url(s) to mock. Can be exact URL, a pattern, or an array of URLs.
239
+ - `dataOrStatusCode` **([number][10] \| [string][8] \| [object][11])** status code when number provided. A response body otherwise
240
+ - `additionalData` **([string][8] \| [object][11])** response body when a status code is set by previous parameter. (optional, default `null`)
241
+
242
+ ### mockServer
243
+
244
+ Use PollyJS [Server Routes API][12] to declare mocks via callback function:
245
+
246
+ ```js
247
+ // basic usage
248
+ server.get('/api/v2/users').intercept((req, res) => {
249
+ res.sendStatus(200).json({ users });
250
+ });
251
+
252
+ // passthrough requests to "/api/v2"
253
+ server.get('/api/v1').passthrough();
254
+ ```
255
+
256
+ In record replay mode you can define which routes should be recorded and where to store them:
257
+
258
+ ```js
259
+ I.startMocking('mock');
260
+ I.mockServer((server) => {
261
+
262
+ // record requests from cdn1.com and save them to data/recording/xml
263
+ server.any('https://cdn1.com/*').passthrough(false).recordingName('xml');
264
+
265
+ // record requests from cdn2.com and save them to data/recording/svg
266
+ server.any('https://cdn2.com/*').passthrough(false).recordingName('svg');
267
+
268
+ // record requests from /api and save them to data/recording/mock (default)
269
+ server.any('/api/*').passthrough(false);
270
+ });
271
+ ```
272
+
273
+ #### Parameters
274
+
275
+ - `configFn`
276
+
277
+ ### passthroughMocking
278
+
279
+ Forces passthrough mode for mocking.
280
+ Requires mocking to be started.
281
+
282
+ ```js
283
+ I.passthroughMocking();
284
+ ```
285
+
286
+ ### recordMocking
287
+
288
+ Forces record mode for mocking.
289
+ Requires mocking to be started.
290
+
291
+ ```js
292
+ I.recordMocking();
293
+ ```
294
+
295
+ ### replayMocking
296
+
297
+ Forces replay mode for mocking.
298
+ Requires mocking to be started.
299
+
300
+ ```js
301
+ I.replayMocking();
302
+ ```
107
303
 
108
304
  ### startMocking
109
305
 
110
306
  Starts mocking of http requests.
111
- Used by mockRequest method to automatically start
112
- mocking requests.
307
+ In record mode starts recording of all requests.
308
+ In replay mode blocks all requests and replaces them with saved.
309
+
310
+ If inside one test you plan to record/replay requests in several places, provide [recording name][13] as the parameter.
311
+
312
+ ```js
313
+ // start mocking requests for a test
314
+ I.startMocking();
315
+
316
+ // start mocking requests for main page
317
+ I.startMocking('main-page');
318
+ // do actions
319
+ I.stopMocking();
320
+ I.startMocking('login-page');
321
+ ```
322
+
323
+ To update [PollyJS configuration][14] use secon argument:
324
+
325
+ ```js
326
+ // change mode
327
+ I.startMocking('comments', { mode: 'replay' });
328
+
329
+ // override config
330
+ I.startMocking('users-loaded', {
331
+ recordFailedRequests: true
332
+ })
333
+ ```
113
334
 
114
335
  #### Parameters
115
336
 
116
- - `title` **any**
337
+ - `title` **any** (optional, default `'Test'`)
338
+ - `config` (optional, default `{}`)
117
339
 
118
340
  ### stopMocking
119
341
 
120
342
  Stops mocking requests.
343
+ Must be called to save recorded requests into faile.
121
344
 
122
- [1]: https://netflix.github.io/pollyjs/#/
345
+ ```js
346
+ I.stopMocking();
347
+ ```
348
+
349
+ [1]: https://netflix.github.io/pollyjs/#/configuration?id=mode
123
350
 
124
351
  [2]: https://netflix.github.io/pollyjs/#/configuration?id=configuration
125
352
 
126
- [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
353
+ [3]: https://netflix.github.io/pollyjs/#/examples?id=rest-persister
354
+
355
+ [4]: #startMocking
356
+
357
+ [5]: #mockRequest
358
+
359
+ [6]: #mockServer
360
+
361
+ [7]: #stopMocking
362
+
363
+ [8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
364
+
365
+ [9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
366
+
367
+ [10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
368
+
369
+ [11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
127
370
 
128
- [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
371
+ [12]: https://netflix.github.io/pollyjs/#/server/overview
129
372
 
130
- [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
373
+ [13]: https://netflix.github.io/pollyjs/#/api?id=recordingname
131
374
 
132
- [6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
375
+ [14]: https://netflix.github.io/pollyjs/#/configuration
@@ -876,15 +876,23 @@ I.selectOption('Which OS do you use?', ['Android', 'iOS']);
876
876
 
877
877
  ### setCookie
878
878
 
879
- Sets a cookie.
879
+ Sets cookie(s).
880
+
881
+ Can be a single cookie object or an array of cookies:
880
882
 
881
883
  ```js
882
884
  I.setCookie({name: 'auth', value: true});
885
+
886
+ // as array
887
+ I.setCookie([
888
+ {name: 'auth', value: true},
889
+ {name: 'agree', value: true}
890
+ ]);
883
891
  ```
884
892
 
885
893
  #### Parameters
886
894
 
887
- - `cookie` **[object][4]** a cookie object.Wrapper for `.cookies.set(cookie)`.
895
+ - `cookie` **([object][4] | [array][11])** a cookie object or array of cookie objects.Wrapper for `.cookies.set(cookie)`.
888
896
  [See more][14]
889
897
 
890
898
  ### triggerMouseEvent