codeceptjs 3.6.7 → 4.0.0-beta.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 (149) hide show
  1. package/bin/codecept.js +81 -84
  2. package/lib/actor.js +13 -13
  3. package/lib/ai.js +13 -10
  4. package/lib/assert/empty.js +21 -20
  5. package/lib/assert/equal.js +39 -37
  6. package/lib/assert/error.js +14 -14
  7. package/lib/assert/include.js +47 -46
  8. package/lib/assert/throws.js +11 -13
  9. package/lib/assert/truth.js +22 -19
  10. package/lib/assert.js +2 -4
  11. package/lib/cli.js +49 -57
  12. package/lib/codecept.js +155 -142
  13. package/lib/colorUtils.js +3 -3
  14. package/lib/command/configMigrate.js +52 -58
  15. package/lib/command/definitions.js +89 -88
  16. package/lib/command/dryRun.js +68 -71
  17. package/lib/command/generate.js +188 -197
  18. package/lib/command/gherkin/init.js +16 -27
  19. package/lib/command/gherkin/snippets.js +20 -20
  20. package/lib/command/gherkin/steps.js +8 -8
  21. package/lib/command/info.js +38 -40
  22. package/lib/command/init.js +288 -290
  23. package/lib/command/interactive.js +32 -32
  24. package/lib/command/list.js +26 -26
  25. package/lib/command/run-multiple/chunk.js +5 -5
  26. package/lib/command/run-multiple/collection.js +3 -3
  27. package/lib/command/run-multiple/run.js +2 -6
  28. package/lib/command/run-multiple.js +93 -113
  29. package/lib/command/run-rerun.js +25 -20
  30. package/lib/command/run-workers.js +66 -64
  31. package/lib/command/run.js +29 -26
  32. package/lib/command/utils.js +65 -80
  33. package/lib/command/workers/runTests.js +10 -10
  34. package/lib/config.js +9 -10
  35. package/lib/container.js +48 -40
  36. package/lib/data/context.js +59 -60
  37. package/lib/data/dataScenarioConfig.js +47 -47
  38. package/lib/data/dataTableArgument.js +29 -29
  39. package/lib/data/table.js +20 -26
  40. package/lib/dirname.js +5 -0
  41. package/lib/event.js +167 -163
  42. package/lib/heal.js +17 -13
  43. package/lib/helper/AI.js +41 -130
  44. package/lib/helper/ApiDataFactory.js +69 -73
  45. package/lib/helper/Appium.js +381 -412
  46. package/lib/helper/Expect.js +425 -0
  47. package/lib/helper/ExpectHelper.js +48 -40
  48. package/lib/helper/FileSystem.js +79 -80
  49. package/lib/helper/GraphQL.js +43 -44
  50. package/lib/helper/GraphQLDataFactory.js +50 -50
  51. package/lib/helper/JSONResponse.js +62 -65
  52. package/lib/helper/Mochawesome.js +28 -28
  53. package/lib/helper/MockServer.js +14 -12
  54. package/lib/helper/Nightmare.js +566 -662
  55. package/lib/helper/Playwright.js +1216 -1361
  56. package/lib/helper/Protractor.js +627 -663
  57. package/lib/helper/Puppeteer.js +1128 -1231
  58. package/lib/helper/REST.js +68 -159
  59. package/lib/helper/SoftExpectHelper.js +2 -2
  60. package/lib/helper/TestCafe.js +484 -490
  61. package/lib/helper/WebDriver.js +1156 -1297
  62. package/lib/helper/clientscripts/PollyWebDriverExt.js +1 -1
  63. package/lib/helper/errors/ConnectionRefused.js +1 -1
  64. package/lib/helper/errors/ElementAssertion.js +2 -2
  65. package/lib/helper/errors/ElementNotFound.js +2 -2
  66. package/lib/helper/errors/RemoteBrowserConnectionRefused.js +1 -1
  67. package/lib/helper/extras/Console.js +1 -1
  68. package/lib/helper/extras/PlaywrightPropEngine.js +2 -2
  69. package/lib/helper/extras/PlaywrightReactVueLocator.js +1 -1
  70. package/lib/helper/extras/PlaywrightRestartOpts.js +18 -21
  71. package/lib/helper/extras/Popup.js +1 -1
  72. package/lib/helper/extras/React.js +3 -3
  73. package/lib/helper/network/actions.js +7 -14
  74. package/lib/helper/network/utils.js +2 -3
  75. package/lib/helper/scripts/blurElement.js +1 -1
  76. package/lib/helper/scripts/focusElement.js +1 -1
  77. package/lib/helper/scripts/highlightElement.js +1 -1
  78. package/lib/helper/scripts/isElementClickable.js +1 -1
  79. package/lib/helper/testcafe/testControllerHolder.js +1 -1
  80. package/lib/helper/testcafe/testcafe-utils.js +7 -6
  81. package/lib/helper.js +3 -1
  82. package/lib/history.js +5 -6
  83. package/lib/hooks.js +6 -6
  84. package/lib/html.js +7 -7
  85. package/lib/index.js +41 -25
  86. package/lib/interfaces/bdd.js +64 -47
  87. package/lib/interfaces/featureConfig.js +19 -19
  88. package/lib/interfaces/gherkin.js +118 -124
  89. package/lib/interfaces/scenarioConfig.js +29 -29
  90. package/lib/listener/artifacts.js +9 -9
  91. package/lib/listener/config.js +24 -24
  92. package/lib/listener/exit.js +12 -12
  93. package/lib/listener/helpers.js +42 -42
  94. package/lib/listener/mocha.js +11 -11
  95. package/lib/listener/retry.js +30 -32
  96. package/lib/listener/steps.js +53 -50
  97. package/lib/listener/timeout.js +54 -54
  98. package/lib/locator.js +10 -6
  99. package/lib/mochaFactory.js +15 -18
  100. package/lib/output.js +10 -6
  101. package/lib/parser.js +12 -15
  102. package/lib/pause.js +33 -40
  103. package/lib/plugin/allure.js +15 -15
  104. package/lib/plugin/autoDelay.js +37 -29
  105. package/lib/plugin/autoLogin.js +65 -70
  106. package/lib/plugin/commentStep.js +18 -18
  107. package/lib/plugin/coverage.js +67 -115
  108. package/lib/plugin/customLocator.js +20 -21
  109. package/lib/plugin/debugErrors.js +24 -24
  110. package/lib/plugin/eachElement.js +38 -38
  111. package/lib/plugin/fakerTransform.js +6 -6
  112. package/lib/plugin/heal.js +108 -67
  113. package/lib/plugin/pauseOnFail.js +11 -11
  114. package/lib/plugin/retryFailedStep.js +39 -32
  115. package/lib/plugin/retryTo.js +40 -46
  116. package/lib/plugin/screenshotOnFail.js +87 -109
  117. package/lib/plugin/selenoid.js +118 -131
  118. package/lib/plugin/standardActingHelpers.js +8 -2
  119. package/lib/plugin/stepByStepReport.js +91 -110
  120. package/lib/plugin/stepTimeout.js +23 -24
  121. package/lib/plugin/subtitles.js +35 -34
  122. package/lib/plugin/tryTo.js +30 -40
  123. package/lib/plugin/wdio.js +75 -78
  124. package/lib/recorder.js +17 -14
  125. package/lib/rerun.js +10 -11
  126. package/lib/scenario.js +23 -25
  127. package/lib/secret.js +2 -4
  128. package/lib/session.js +10 -10
  129. package/lib/step.js +9 -12
  130. package/lib/store.js +3 -2
  131. package/lib/transform.js +1 -1
  132. package/lib/translation.js +8 -7
  133. package/lib/ui.js +14 -12
  134. package/lib/utils.js +72 -70
  135. package/lib/within.js +10 -10
  136. package/lib/workerStorage.js +25 -27
  137. package/lib/workers.js +32 -29
  138. package/package.json +53 -51
  139. package/translations/de-DE.js +1 -1
  140. package/translations/fr-FR.js +1 -1
  141. package/translations/index.js +13 -9
  142. package/translations/it-IT.js +1 -1
  143. package/translations/ja-JP.js +1 -1
  144. package/translations/pl-PL.js +1 -1
  145. package/translations/pt-BR.js +1 -1
  146. package/translations/ru-RU.js +1 -1
  147. package/translations/zh-CN.js +1 -1
  148. package/translations/zh-TW.js +1 -1
  149. package/typings/index.d.ts +65 -415
@@ -1,9 +1,8 @@
1
- const axios = require('axios').default
2
- const Helper = require('@codeceptjs/helper')
3
- const { Agent } = require('https')
4
- const Secret = require('../secret')
5
-
6
- const { beautify } = require('../utils')
1
+ import { default as axios } from 'axios';
2
+ import Helper from '@codeceptjs/helper';
3
+ import Secret from '../secret.js';
4
+ import { beautify } from '../utils.js';
5
+ import { output } from '../output.js';
7
6
 
8
7
  /**
9
8
  * ## Configuration
@@ -11,16 +10,14 @@ const { beautify } = require('../utils')
11
10
  * @typedef RESTConfig
12
11
  * @type {object}
13
12
  * @prop {string} [endpoint] - API base URL
14
- * @prop {boolean} [prettyPrintJson=false] - pretty print json for response/request on console logs.
15
- * @prop {boolean} [printCurl=false] - print cURL request on console logs. False by default.
16
- * @prop {number} [timeout=1000] - timeout for requests in milliseconds. 10000ms by default.
17
- * @prop {object} [defaultHeaders] - a list of default headers.
18
- * @prop {object} [httpAgent] - create an agent with SSL certificate
19
- * @prop {function} [onRequest] - an async function which can update request object.
20
- * @prop {function} [onResponse] - an async function which can update response object.
13
+ * @prop {boolean} [prettyPrintJson=false] - pretty print json for response/request on console logs
14
+ * @prop {number} [timeout=1000] - timeout for requests in milliseconds. 10000ms by default
15
+ * @prop {object} [defaultHeaders] - a list of default headers
16
+ * @prop {function} [onRequest] - a async function which can update request object.
17
+ * @prop {function} [onResponse] - a async function which can update response object.
21
18
  * @prop {number} [maxUploadFileSize] - set the max content file size in MB when performing api calls.
22
19
  */
23
- const config = {}
20
+ const config = {};
24
21
 
25
22
  /**
26
23
  * REST helper allows to send additional requests to the REST API during acceptance tests.
@@ -44,41 +41,6 @@ const config = {}
44
41
  *}
45
42
  * ```
46
43
  *
47
- * With httpAgent
48
- *
49
- * ```js
50
- * {
51
- * helpers: {
52
- * REST: {
53
- * endpoint: 'http://site.com/api',
54
- * prettyPrintJson: true,
55
- * httpAgent: {
56
- * key: fs.readFileSync(__dirname + '/path/to/keyfile.key'),
57
- * cert: fs.readFileSync(__dirname + '/path/to/certfile.cert'),
58
- * rejectUnauthorized: false,
59
- * keepAlive: true
60
- * }
61
- * }
62
- * }
63
- * }
64
- * ```
65
- *
66
- * ```js
67
- * {
68
- * helpers: {
69
- * REST: {
70
- * endpoint: 'http://site.com/api',
71
- * prettyPrintJson: true,
72
- * httpAgent: {
73
- * ca: fs.readFileSync(__dirname + '/path/to/ca.pem'),
74
- * rejectUnauthorized: false,
75
- * keepAlive: true
76
- * }
77
- * }
78
- * }
79
- * }
80
- * ```
81
- *
82
44
  * ## Access From Helpers
83
45
  *
84
46
  * Send REST requests by accessing `_executeRequest` method:
@@ -94,7 +56,7 @@ const config = {}
94
56
  */
95
57
  class REST extends Helper {
96
58
  constructor(config) {
97
- super(config)
59
+ super(config);
98
60
  this.options = {
99
61
  timeout: 10000,
100
62
  defaultHeaders: {},
@@ -102,55 +64,39 @@ class REST extends Helper {
102
64
  prettyPrintJson: false,
103
65
  onRequest: null,
104
66
  onResponse: null,
105
- }
67
+ };
106
68
 
107
69
  if (this.options.maxContentLength) {
108
- const maxContentLength = this.options.maxUploadFileSize * 1024 * 1024
109
- this.options.maxContentLength = maxContentLength
110
- this.options.maxBodyLength = maxContentLength
70
+ const maxContentLength = this.options.maxUploadFileSize * 1024 * 1024;
71
+ this.options.maxContentLength = maxContentLength;
72
+ this.options.maxBodyLength = maxContentLength;
111
73
  }
112
74
 
113
75
  // override defaults with config
114
- this._setConfig(config)
115
-
116
- this.headers = { ...this.options.defaultHeaders }
117
-
118
- // Create an agent with SSL certificate
119
- if (this.options.httpAgent) {
120
- // if one of those keys is there, all good to go
121
- if (this.options.httpAgent.ca || this.options.httpAgent.key || this.options.httpAgent.cert) {
122
- this.httpsAgent = new Agent(this.options.httpAgent)
123
- } else {
124
- // otherwise, throws an error of httpAgent config
125
- throw Error('Please recheck your httpAgent config!')
126
- }
127
- }
76
+ this._setConfig(config);
128
77
 
129
- this.axios = this.httpsAgent ? axios.create({ httpsAgent: this.httpsAgent }) : axios.create()
78
+ this.headers = { ...this.options.defaultHeaders };
79
+ this.axios = axios.create();
130
80
  // @ts-ignore
131
- this.axios.defaults.headers = this.options.defaultHeaders
81
+ this.axios.defaults.headers = this.options.defaultHeaders;
132
82
  }
133
83
 
134
84
  static _config() {
135
85
  return [
136
- {
137
- name: 'endpoint',
138
- message: 'Endpoint of API you are going to test',
139
- default: 'http://localhost:3000/api',
140
- },
141
- ]
86
+ { name: 'endpoint', message: 'Endpoint of API you are going to test', default: 'http://localhost:3000/api' },
87
+ ];
142
88
  }
143
89
 
144
90
  static _checkRequirements() {
145
91
  try {
146
- require('axios')
92
+ require('axios');
147
93
  } catch (e) {
148
- return ['axios']
94
+ return ['axios'];
149
95
  }
150
96
  }
151
97
 
152
98
  _before() {
153
- this.headers = { ...this.options.defaultHeaders }
99
+ this.headers = { ...this.options.defaultHeaders };
154
100
  }
155
101
 
156
102
  /**
@@ -159,7 +105,7 @@ class REST extends Helper {
159
105
  * @param {object} headers headers list
160
106
  */
161
107
  haveRequestHeaders(headers) {
162
- this.headers = { ...this.headers, ...headers }
108
+ this.headers = { ...this.headers, ...headers };
163
109
  }
164
110
 
165
111
  /**
@@ -173,7 +119,7 @@ class REST extends Helper {
173
119
  * @param {string | CodeceptJS.Secret} accessToken Bearer access token
174
120
  */
175
121
  amBearerAuthenticated(accessToken) {
176
- this.haveRequestHeaders({ Authorization: `Bearer ${accessToken}` })
122
+ this.haveRequestHeaders({ Authorization: `Bearer ${accessToken}` });
177
123
  }
178
124
 
179
125
  /**
@@ -185,65 +131,54 @@ class REST extends Helper {
185
131
  */
186
132
  async _executeRequest(request) {
187
133
  // Add custom headers. They can be set by amBearerAuthenticated() or haveRequestHeaders()
188
- request.headers = { ...this.headers, ...request.headers }
134
+ request.headers = { ...this.headers, ...request.headers };
189
135
 
190
- const _debugRequest = { ...request }
191
- this.axios.defaults.timeout = request.timeout || this.options.timeout
136
+ const _debugRequest = { ...request };
137
+ this.axios.defaults.timeout = request.timeout || this.options.timeout;
192
138
 
193
139
  if (this.headers && this.headers.auth) {
194
- request.auth = this.headers.auth
140
+ request.auth = this.headers.auth;
195
141
  }
196
142
 
197
143
  if (typeof request.data === 'object') {
198
- const returnedValue = {}
144
+ const returnedValue = {};
199
145
  for (const [key, value] of Object.entries(request.data)) {
200
- returnedValue[key] = value
201
- if (value instanceof Secret) returnedValue[key] = value.getMasked()
146
+ returnedValue[key] = value;
147
+ if (value instanceof Secret) returnedValue[key] = value.getMasked();
202
148
  }
203
- _debugRequest.data = returnedValue
149
+ _debugRequest.data = returnedValue;
204
150
  }
205
151
 
206
152
  if (request.data instanceof Secret) {
207
- _debugRequest.data = '*****'
208
- request.data =
209
- typeof request.data === 'object' && !(request.data instanceof Secret)
210
- ? { ...request.data.toString() }
211
- : request.data.toString()
153
+ _debugRequest.data = '*****';
154
+ request.data = (typeof request.data === 'object' && !(request.data instanceof Secret)) ? { ...request.data.toString() } : request.data.toString();
212
155
  }
213
156
 
214
- if (typeof request.data === 'string') {
157
+ if ((typeof request.data) === 'string') {
215
158
  if (!request.headers || !request.headers['Content-Type']) {
216
- request.headers = { ...request.headers, ...{ 'Content-Type': 'application/x-www-form-urlencoded' } }
159
+ request.headers = { ...request.headers, ...{ 'Content-Type': 'application/x-www-form-urlencoded' } };
217
160
  }
218
161
  }
219
162
 
220
163
  if (this.config.onRequest) {
221
- await this.config.onRequest(request)
164
+ await this.config.onRequest(request);
222
165
  }
223
166
 
224
- this.options.prettyPrintJson
225
- ? this.debugSection('Request', beautify(JSON.stringify(_debugRequest)))
226
- : this.debugSection('Request', JSON.stringify(_debugRequest))
167
+ this.options.prettyPrintJson ? output.debugSection('Request', beautify(JSON.stringify(_debugRequest))) : output.debugSection('Request', JSON.stringify(_debugRequest));
227
168
 
228
- if (this.options.printCurl) {
229
- this.debugSection('CURL Request', curlize(request))
230
- }
231
-
232
- let response
169
+ let response;
233
170
  try {
234
- response = await this.axios(request)
171
+ response = await this.axios(request);
235
172
  } catch (err) {
236
- if (!err.response) throw err
237
- this.debugSection('Response', `Response error. Status code: ${err.response.status}`)
238
- response = err.response
173
+ if (!err.response) throw err;
174
+ output.debugSection('Response', `Response error. Status code: ${err.response.status}`);
175
+ response = err.response;
239
176
  }
240
177
  if (this.config.onResponse) {
241
- await this.config.onResponse(response)
178
+ await this.config.onResponse(response);
242
179
  }
243
- this.options.prettyPrintJson
244
- ? this.debugSection('Response', beautify(JSON.stringify(response.data)))
245
- : this.debugSection('Response', JSON.stringify(response.data))
246
- return response
180
+ this.options.prettyPrintJson ? output.debugSection('Response', beautify(JSON.stringify(response.data))) : output.debugSection('Response', JSON.stringify(response.data));
181
+ return response;
247
182
  }
248
183
 
249
184
  /**
@@ -252,7 +187,7 @@ class REST extends Helper {
252
187
  * @param {*} url
253
188
  */
254
189
  _url(url) {
255
- return /^\w+\:\/\//.test(url) ? url : this.options.endpoint + url
190
+ return /^\w+\:\/\//.test(url) ? url : this.options.endpoint + url;
256
191
  }
257
192
 
258
193
  /**
@@ -265,7 +200,7 @@ class REST extends Helper {
265
200
  * @param {number} newTimeout - timeout in milliseconds
266
201
  */
267
202
  setRequestTimeout(newTimeout) {
268
- this.options.timeout = newTimeout
203
+ this.options.timeout = newTimeout;
269
204
  }
270
205
 
271
206
  /**
@@ -284,8 +219,8 @@ class REST extends Helper {
284
219
  const request = {
285
220
  baseURL: this._url(url),
286
221
  headers,
287
- }
288
- return this._executeRequest(request)
222
+ };
223
+ return this._executeRequest(request);
289
224
  }
290
225
 
291
226
  /**
@@ -311,14 +246,14 @@ class REST extends Helper {
311
246
  method: 'POST',
312
247
  data: payload,
313
248
  headers,
314
- }
249
+ };
315
250
 
316
251
  if (this.options.maxContentLength) {
317
- request.maxContentLength = this.options.maxContentLength
318
- request.maxBodyLength = this.options.maxContentLength
252
+ request.maxContentLength = this.options.maxContentLength;
253
+ request.maxBodyLength = this.options.maxContentLength;
319
254
  }
320
255
 
321
- return this._executeRequest(request)
256
+ return this._executeRequest(request);
322
257
  }
323
258
 
324
259
  /**
@@ -344,14 +279,14 @@ class REST extends Helper {
344
279
  method: 'PATCH',
345
280
  data: payload,
346
281
  headers,
347
- }
282
+ };
348
283
 
349
284
  if (this.options.maxContentLength) {
350
- request.maxContentLength = this.options.maxContentLength
351
- request.maxBodyLength = this.options.maxBodyLength
285
+ request.maxContentLength = this.options.maxContentLength;
286
+ request.maxBodyLength = this.options.maxBodyLength;
352
287
  }
353
288
 
354
- return this._executeRequest(request)
289
+ return this._executeRequest(request);
355
290
  }
356
291
 
357
292
  /**
@@ -377,14 +312,14 @@ class REST extends Helper {
377
312
  method: 'PUT',
378
313
  data: payload,
379
314
  headers,
380
- }
315
+ };
381
316
 
382
317
  if (this.options.maxContentLength) {
383
- request.maxContentLength = this.options.maxContentLength
384
- request.maxBodyLength = this.options.maxBodyLength
318
+ request.maxContentLength = this.options.maxContentLength;
319
+ request.maxBodyLength = this.options.maxBodyLength;
385
320
  }
386
321
 
387
- return this._executeRequest(request)
322
+ return this._executeRequest(request);
388
323
  }
389
324
 
390
325
  /**
@@ -404,35 +339,9 @@ class REST extends Helper {
404
339
  baseURL: this._url(url),
405
340
  method: 'DELETE',
406
341
  headers,
407
- }
408
-
409
- return this._executeRequest(request)
410
- }
411
- }
412
-
413
- module.exports = REST
414
-
415
- function curlize(request) {
416
- if (request.data?.constructor.name.toLowerCase() === 'formdata')
417
- return 'cURL is not printed as the request body is not a JSON'
418
- let curl =
419
- `curl --location --request ${request.method ? request.method.toUpperCase() : 'GET'} ${request.baseURL} `.replace(
420
- "'",
421
- '',
422
- )
423
-
424
- if (request.headers) {
425
- Object.entries(request.headers).forEach(([key, value]) => {
426
- curl += `-H "${key}: ${value}" `
427
- })
428
- }
429
-
430
- if (!curl.toLowerCase().includes('content-type: application/json')) {
431
- curl += '-H "Content-Type: application/json" '
432
- }
342
+ };
433
343
 
434
- if (request.data) {
435
- curl += `-d '${JSON.stringify(request.data)}'`
344
+ return this._executeRequest(request);
436
345
  }
437
- return curl
438
346
  }
347
+ export default REST;
@@ -1,4 +1,4 @@
1
- const ExpectHelper = require('./ExpectHelper')
1
+ import ExpectHelper from './ExpectHelper.js'
2
2
 
3
3
  /**
4
4
  * SoftAssertHelper is a utility class for performing soft assertions.
@@ -378,4 +378,4 @@ class SoftAssertHelper extends ExpectHelper {
378
378
  this.softAssert(() => this.expectMatchesPattern(actualValue, expectedPattern, customErrorMsg), customErrorMsg)
379
379
  }
380
380
  }
381
- module.exports = SoftAssertHelper
381
+ export default SoftAssertHelper