codeceptjs 4.1.0 → 4.2.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 (49) hide show
  1. package/docs/advanced.md +1 -1
  2. package/docs/alternative-browsers.md +153 -0
  3. package/docs/basics.md +9 -1
  4. package/docs/configuration.md +2 -0
  5. package/docs/helpers/CDPBrowser.md +2138 -0
  6. package/docs/helpers/Kitesurf.md +118 -0
  7. package/docs/helpers/Obscura.md +210 -0
  8. package/docs/helpers/Playwright.md +5 -2
  9. package/docs/migration-4.md +3 -1
  10. package/docs/parallel.md +10 -0
  11. package/docs/plugins/screencast.md +18 -13
  12. package/docs/plugins.md +1 -1
  13. package/lib/command/info.js +11 -3
  14. package/lib/command/workers/runTests.js +14 -20
  15. package/lib/container.js +6 -0
  16. package/lib/data/context.js +7 -5
  17. package/lib/element/WebElement.js +5 -0
  18. package/lib/helper/Appium.js +14 -2
  19. package/lib/helper/CDPBrowser.js +3004 -0
  20. package/lib/helper/Kitesurf.js +139 -0
  21. package/lib/helper/Obscura.js +344 -0
  22. package/lib/helper/Playwright.js +41 -10
  23. package/lib/helper/Puppeteer.js +30 -7
  24. package/lib/helper/WebDriver.js +27 -6
  25. package/lib/helper/clientscripts/cdpBrowserClient.js +486 -0
  26. package/lib/helper/clientscripts/xpathPolyfill.js +31 -0
  27. package/lib/helper/errors/MultipleElementsFound.js +60 -3
  28. package/lib/helper/extras/CDPConnection.js +92 -0
  29. package/lib/helper/extras/CDPElementHandle.js +27 -0
  30. package/lib/helper/extras/PlaywrightLocator.js +2 -2
  31. package/lib/helper/extras/apngAssembler.js +156 -0
  32. package/lib/html.js +9 -2
  33. package/lib/listener/retryEnhancer.js +2 -1
  34. package/lib/listener/steps.js +8 -0
  35. package/lib/locator.js +7 -2
  36. package/lib/mocha/asyncWrapper.js +7 -1
  37. package/lib/mocha/hooks.js +10 -0
  38. package/lib/parser.js +14 -2
  39. package/lib/plugin/junitReporter.js +17 -1
  40. package/lib/plugin/screencast.js +116 -24
  41. package/lib/step/base.js +15 -3
  42. package/lib/step/config.js +1 -0
  43. package/lib/store.js +6 -0
  44. package/lib/utils/loaderCheck.js +6 -0
  45. package/lib/utils.js +1 -1
  46. package/lib/workers.js +17 -0
  47. package/package.json +5 -2
  48. package/typings/promiseBasedTypes.d.ts +1835 -0
  49. package/typings/types.d.ts +1848 -0
@@ -20,16 +20,21 @@ const defaultConfig = {
20
20
  }
21
21
 
22
22
  /**
23
- * Records WebM video of tests using Playwright's screencast API.
23
+ * Records a video of tests. Uses Playwright's `page.screencast` API (WebM) when the active
24
+ * helper is Playwright, or raw CDP `Page.startScreencast` (APNG, assembled in-process) when the
25
+ * active helper is `CDPBrowser` or a subclass (`Obscura`, `Kitesurf`, ...). Which path is used is
26
+ * detected automatically per test run; nothing in the config changes between them.
24
27
  *
25
- * When `captions` is enabled, action annotations are burned into the video;
26
- * when `subtitles` is enabled, a standalone `.srt` is also produced. Default
27
- * `on=fail` keeps videos for failed tests only; `on=test` keeps every test's
28
- * video.
28
+ * When `captions` is enabled, action annotations are burned into the video — Playwright only,
29
+ * via `page.screencast.showActions()`/`showChapter()`; silently absent on the CDP path, since CDP
30
+ * screencast frames are raw, uncomposited page captures with no overlay mechanism. `subtitles`
31
+ * (a standalone `.srt`) works identically on both paths, since it's driven by step events, not by
32
+ * the video API. Default `on=fail` keeps videos for failed tests only; `on=test` keeps every
33
+ * test's video.
29
34
  *
30
- * Note: enabling Playwright's helper-level `video: true` together with this
31
- * plugin produces two independent recordings (`output/videos/*.webm` from the
32
- * helper, `output/screencast/*.webm` from this plugin).
35
+ * Note: enabling Playwright's helper-level `video: true` together with this plugin produces two
36
+ * independent recordings (`output/videos/*.webm` from the helper, `output/screencast/*.webm` from
37
+ * this plugin).
33
38
  *
34
39
  * #### Configuration
35
40
  *
@@ -49,11 +54,11 @@ const defaultConfig = {
49
54
  *
50
55
  * Other config options:
51
56
  *
52
- * * `captions`: burn-in action overlays via `page.screencast.showActions()`. Default: true.
57
+ * * `captions`: burn-in action overlays via `page.screencast.showActions()`. Playwright only. Default: true.
53
58
  * * `subtitles`: also write a standalone `.srt` file alongside the video. Default: false.
54
59
  * * `video`: record a video. With `video=false, subtitles=true`, only the `.srt` is produced. Default: true.
55
- * * `size`: pass-through `{ width, height }` for `screencast.start`.
56
- * * `quality`: pass-through 0–100 for `screencast.start`.
60
+ * * `size`: pass-through `{ width, height }` `screencast.start`'s `size` on Playwright, `maxWidth`/`maxHeight` on the CDP path.
61
+ * * `quality`: pass-through 0–100 for `screencast.start` (Playwright) or CDP `Page.startScreencast` (CDPBrowser family).
57
62
  *
58
63
  * CLI examples:
59
64
  *
@@ -64,7 +69,7 @@ const defaultConfig = {
64
69
  * ```
65
70
  */
66
71
  export default function (config = {}) {
67
- const helper = getBrowserHelper()
72
+ const helper = getScreencastHelper()
68
73
  if (!helper) return
69
74
 
70
75
  const cliArgs = parsePluginArgs(config._args)
@@ -86,7 +91,9 @@ function wireScreencast(mode, options) {
86
91
  const state = {
87
92
  test: null,
88
93
  webmPath: null,
94
+ apngPath: null,
89
95
  srtPath: null,
96
+ kind: null,
90
97
  steps: null,
91
98
  startedAt: null,
92
99
  failed: false,
@@ -99,7 +106,9 @@ function wireScreencast(mode, options) {
99
106
  state.test = test
100
107
  state.failed = false
101
108
  state.webmPath = null
109
+ state.apngPath = null
102
110
  state.srtPath = null
111
+ state.kind = null
103
112
  state.startQueued = false
104
113
  state.started = false
105
114
  state.steps = options.subtitles ? {} : null
@@ -141,7 +150,9 @@ function wireScreencast(mode, options) {
141
150
  recorder.add('screencast:stop', async () => finalizeScreencast({
142
151
  test: state.test,
143
152
  webmPath: state.webmPath,
153
+ apngPath: state.apngPath,
144
154
  srtPath: state.srtPath,
155
+ kind: state.kind,
145
156
  steps: state.steps,
146
157
  failed: state.failed,
147
158
  started: state.started,
@@ -152,15 +163,25 @@ function wireScreencast(mode, options) {
152
163
  }
153
164
 
154
165
  async function startScreencast(test, options, state) {
155
- const helper = getBrowserHelper()
156
- if (!helper?.page?.screencast) {
157
- if (!state.warnedNoApi) {
158
- output.plugin('screencast', 'page.screencast not available — requires Playwright >= 1.59. Skipping.')
159
- state.warnedNoApi = true
160
- }
161
- return
166
+ const helper = getScreencastHelper()
167
+
168
+ if (helper?.page?.screencast) {
169
+ state.kind = 'playwright'
170
+ return startPlaywrightScreencast(helper, test, options, state)
162
171
  }
163
172
 
173
+ if (typeof helper?.startScreencast === 'function') {
174
+ state.kind = 'cdp'
175
+ return startCdpScreencast(helper, test, options, state)
176
+ }
177
+
178
+ if (!state.warnedNoApi) {
179
+ output.plugin('screencast', 'No screencast API available on the active helper — requires Playwright >= 1.59, or a CDPBrowser-family helper (CDPBrowser/Obscura/Kitesurf). Skipping.')
180
+ state.warnedNoApi = true
181
+ }
182
+ }
183
+
184
+ async function startPlaywrightScreencast(helper, test, options, state) {
164
185
  const baseDir = path.join(store.outputDir || '_output', 'screencast')
165
186
  mkdirp.sync(baseDir)
166
187
  const baseName = testToFileName(test, { suffix: '', unique: true })
@@ -192,12 +213,42 @@ async function startScreencast(test, options, state) {
192
213
  }
193
214
  }
194
215
 
216
+ async function startCdpScreencast(helper, test, options, state) {
217
+ const baseDir = path.join(store.outputDir || '_output', 'screencast')
218
+ mkdirp.sync(baseDir)
219
+ const baseName = testToFileName(test, { suffix: '', unique: true })
220
+ state.apngPath = path.join(baseDir, `${baseName}.apng`)
221
+ state.srtPath = path.join(baseDir, `${baseName}.srt`)
222
+
223
+ const startOpts = {}
224
+ if (options.size) {
225
+ startOpts.maxWidth = options.size.width
226
+ startOpts.maxHeight = options.size.height
227
+ }
228
+ if (options.quality != null) startOpts.quality = options.quality
229
+
230
+ try {
231
+ await helper.startScreencast(startOpts)
232
+ state.started = true
233
+ } catch (err) {
234
+ output.plugin('screencast', `Failed to start: ${err.message}`)
235
+ state.apngPath = null
236
+ state.srtPath = null
237
+ state.started = false
238
+ }
239
+
240
+ // captions/chapter burn-in (showActions/showChapter) is Playwright-only — CDP screencast
241
+ // frames are raw page captures with no overlay mechanism, so this is silently absent here.
242
+ }
243
+
195
244
  async function finalizeScreencast(snapshot) {
196
- const { test, options, mode, steps } = snapshot
197
- let { webmPath, srtPath } = snapshot
245
+ const { test, options, mode, steps, kind } = snapshot
246
+ let { webmPath, apngPath, srtPath } = snapshot
198
247
 
199
- const helper = getBrowserHelper()
200
- if (snapshot.started && helper?.page?.screencast) {
248
+ const helper = getScreencastHelper()
249
+ let apngBuffer = null
250
+
251
+ if (kind === 'playwright' && snapshot.started && helper?.page?.screencast) {
201
252
  try {
202
253
  await helper.page.screencast.stop()
203
254
  } catch (err) {
@@ -205,6 +256,14 @@ async function finalizeScreencast(snapshot) {
205
256
  }
206
257
  }
207
258
 
259
+ if (kind === 'cdp' && snapshot.started && typeof helper?.stopScreencast === 'function') {
260
+ try {
261
+ apngBuffer = await helper.stopScreencast()
262
+ } catch (err) {
263
+ output.plugin('screencast', `stop failed: ${err.message}`)
264
+ }
265
+ }
266
+
208
267
  const shouldKeep = mode === 'test' || (mode === 'fail' && snapshot.failed)
209
268
 
210
269
  if (options.video && webmPath) {
@@ -218,6 +277,22 @@ async function finalizeScreencast(snapshot) {
218
277
  }
219
278
  }
220
279
 
280
+ if (options.video && apngPath) {
281
+ if (!shouldKeep || !apngBuffer) {
282
+ apngPath = null
283
+ } else {
284
+ try {
285
+ await fs.promises.writeFile(apngPath, apngBuffer)
286
+ ensureArtifactsObject(test)
287
+ test.artifacts.screencast = apngPath
288
+ attachJUnitArtifact(test, apngPath)
289
+ } catch (err) {
290
+ output.plugin('screencast', `failed to write APNG: ${err.message}`)
291
+ apngPath = null
292
+ }
293
+ }
294
+ }
295
+
221
296
  if (options.subtitles && steps) {
222
297
  if (options.video && !shouldKeep) {
223
298
  try { srtPath && fs.unlinkSync(srtPath) } catch { /* nothing to delete */ }
@@ -275,8 +350,25 @@ function buildSrt(steps) {
275
350
  return out
276
351
  }
277
352
 
353
+ // `getBrowserHelper` (from `pluginParser.js`) only recognizes `Container.STANDARD_ACTING_HELPERS`
354
+ // (`Playwright`/`WebDriver`/`Puppeteer`/`Appium`), so it never finds `CDPBrowser`/`Obscura`/
355
+ // `Kitesurf`. Rather than widen that shared list — used by several other plugins with their own,
356
+ // Playwright/WebDriver-specific assumptions — this plugin does its own duck-typed fallback lookup,
357
+ // scoped to itself: any active helper exposing a `startScreencast` function qualifies, with no
358
+ // hardcoded class names, so any future CDP-family helper picks this up automatically too.
359
+ function getScreencastHelper() {
360
+ const standard = getBrowserHelper()
361
+ if (standard) return standard
362
+ const helpers = Container.helpers()
363
+ for (const name of Object.keys(helpers)) {
364
+ if (typeof helpers[name]?.startScreencast === 'function') return helpers[name]
365
+ }
366
+ return null
367
+ }
368
+
278
369
  function ensureArtifactsObject(test) {
279
- if (!test.artifacts || Array.isArray(test.artifacts)) test.artifacts = {}
370
+ if (!test.artifacts) test.artifacts = {}
371
+ else if (Array.isArray(test.artifacts)) test.artifacts = Object.assign({}, test.artifacts)
280
372
  }
281
373
 
282
374
  function attachJUnitArtifact(test, filePath) {
package/lib/step/base.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import color from 'chalk'
2
+ import { pathToFileURL } from 'url'
2
3
  import Secret from '../secret.js'
3
4
  import { getCurrentTimeout } from '../timeout.js'
4
5
  import { ucfirst, humanizeString, serializeError } from '../utils.js'
@@ -149,8 +150,6 @@ class Step {
149
150
  const lines = this.stack.split('\n')
150
151
  if (lines[STACK_LINE]) {
151
152
  let line = lines[STACK_LINE].trim()
152
- .replace(store.codeceptDir || '', '.')
153
- .trim()
154
153
 
155
154
  // Map .temp.mjs back to original .ts files using container's tsFileMapping
156
155
  const fileMapping = store.tsFileMapping
@@ -160,10 +159,23 @@ class Step {
160
159
  line = line.replace(mjsFile, tsFile)
161
160
  break
162
161
  }
162
+
163
+ const mjsFileUrl = pathToFileURL(mjsFile).href
164
+ if (line.includes(mjsFileUrl)) {
165
+ line = line.replace(mjsFileUrl, pathToFileURL(tsFile).href)
166
+ break
167
+ }
163
168
  }
164
169
  }
165
170
 
166
- return line
171
+ const codeceptDir = store.codeceptDir || ''
172
+ if (codeceptDir) {
173
+ line = line
174
+ .replace(pathToFileURL(codeceptDir).href, '.')
175
+ .replace(codeceptDir, '.')
176
+ }
177
+
178
+ return line.trim()
167
179
  }
168
180
  return ''
169
181
  }
@@ -4,6 +4,7 @@
4
4
  * @property {boolean} [exact] - Enable strict mode for this step. Throws if multiple elements match.
5
5
  * @property {boolean} [strictMode] - Alias for exact.
6
6
  * @property {boolean} [ignoreCase] - Perform case-insensitive text matching.
7
+ * @property {boolean} [visibleLocator] - Match only visible elements. Overrides the Playwright helper `visibleLocator` config option for this step.
7
8
  */
8
9
 
9
10
  /**
package/lib/store.js CHANGED
@@ -93,6 +93,12 @@ const store = {
93
93
  /** @type {CodeceptJS.Suite | null} */
94
94
  currentSuite: null,
95
95
 
96
+ /**
97
+ * Locators match only visible elements, resolved per step
98
+ * @type {boolean}
99
+ */
100
+ visibleLocator: false,
101
+
96
102
  /** @type {Map<string, string> | null} */
97
103
  tsFileMapping: null,
98
104
 
@@ -6,10 +6,16 @@
6
6
  * Check if a TypeScript loader is available for test files
7
7
  * Note: This checks if loaders are in the require array, not if packages are installed
8
8
  * Package installation is checked when actually requiring modules
9
+ * Always true under Bun, which transpiles TypeScript itself
9
10
  * @param {string[]} requiredModules - Array of required modules from config
10
11
  * @returns {boolean}
11
12
  */
12
13
  export function checkTypeScriptLoader(requiredModules = []) {
14
+ // Bun transpiles TypeScript natively, so no loader is needed.
15
+ // Node is not treated the same way: its native type stripping rejects enums
16
+ // and does not resolve extensionless relative imports.
17
+ if (process.versions.bun) return true
18
+
13
19
  // Check if a loader is configured in the require array
14
20
  return (
15
21
  requiredModules.includes('tsx/esm') ||
package/lib/utils.js CHANGED
@@ -108,7 +108,7 @@ export const methodsOfObject = function (obj, className) {
108
108
  export const template = function (template, data) {
109
109
  return template.replace(/{{([^{}]*)}}/g, (a, b) => {
110
110
  const r = data[b]
111
- if (r === undefined) return ''
111
+ if (r === undefined || r === null) return ''
112
112
  return r.toString()
113
113
  })
114
114
  }
package/lib/workers.js CHANGED
@@ -508,10 +508,27 @@ class Workers extends EventEmitter {
508
508
  // Create workers and set up message handlers immediately (not in recorder queue)
509
509
  // This prevents a race condition where workers start sending messages before handlers are attached
510
510
  const workerThreads = []
511
+ const staggerDelay = this.codecept.config.workerInitializationDelay !== undefined
512
+ ? this.codecept.config.workerInitializationDelay
513
+ : 200
514
+
515
+ // Maximum total stagger window (default 10 seconds). Allows capping total delay regardless of worker count.
516
+ const maxStagger = this.codecept.config.workerInitializationMaxDelay ?? 10000
517
+ // Compute effective per-worker delay to keep total stagger within maxStagger.
518
+ const effectiveDelay = staggerDelay > 0
519
+ ? Math.min(staggerDelay, Math.floor(maxStagger / Math.max(1, this.workers.length - 1)))
520
+ : 0
521
+
511
522
  for (const worker of this.workers) {
512
523
  const workerThread = createWorker(worker, this.isPoolMode)
513
524
  this._listenWorkerEvents(workerThread)
514
525
  workerThreads.push(workerThread)
526
+
527
+ // Stagger worker creation to prevent CPU spikes
528
+ // from massive V8 isolate creation and naturally stagger browser init
529
+ if (this.workers.length > 1 && effectiveDelay > 0) {
530
+ await new Promise(resolve => setTimeout(resolve, effectiveDelay))
531
+ }
515
532
  }
516
533
 
517
534
  recorder.add('workers started', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeceptjs",
3
- "version": "4.1.0",
3
+ "version": "4.2.0-beta.2",
4
4
  "type": "module",
5
5
  "description": "Supercharged End 2 End Testing Framework for NodeJS",
6
6
  "keywords": [
@@ -78,6 +78,8 @@
78
78
  "test:unit:webbapi:puppeteer": "mocha test/helper/Puppeteer_test.js --reporter @testomatio/reporter/mocha",
79
79
  "test:unit:webbapi:webDriver": "mocha test/helper/WebDriver_test.js --timeout 10000 --reporter @testomatio/reporter/mocha",
80
80
  "test:unit:webbapi:webDriver:noSeleniumServer": "mocha test/helper/WebDriver.noSeleniumServer_test.js --timeout 10000 --reporter @testomatio/reporter/mocha",
81
+ "test:unit:webbapi:cdpbrowser": "mocha test/helper/CDPBrowser_chrome_test.js --timeout 30000 --reporter @testomatio/reporter/mocha",
82
+ "test:unit:webbapi:obscura": "mocha test/helper/CDPBrowser_obscura_test.js --timeout 30000 --reporter @testomatio/reporter/mocha",
81
83
  "test:unit:expect": "mocha test/helper/Expect_test.js --reporter @testomatio/reporter/mocha",
82
84
  "test:plugin": "mocha test/plugin/plugin_test.js --reporter @testomatio/reporter/mocha",
83
85
  "def": "./runok.cjs def",
@@ -134,6 +136,7 @@
134
136
  "promise-retry": "1.1.1",
135
137
  "sprintf-js": "1.1.3",
136
138
  "uuid": "11.1.0",
139
+ "ws": "^8.21.2",
137
140
  "xpath": "0.0.34",
138
141
  "zod": "^4.1.11"
139
142
  },
@@ -174,7 +177,7 @@
174
177
  "jsdoc": "^3.6.11",
175
178
  "jsdoc-typeof-plugin": "1.0.0",
176
179
  "json-server": "0.17.4",
177
- "playwright": "^1.59.0",
180
+ "playwright": "^1.63.0",
178
181
  "prettier": "^3.3.2",
179
182
  "puppeteer": "24.36.0",
180
183
  "qrcode-terminal": "0.12.0",