codeceptjs 4.2.3 → 4.2.4
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.
- package/docs/element-selection.md +27 -4
- package/docs/helpers/Appium.md +54 -54
- package/docs/helpers/Kitesurf.md +64 -64
- package/docs/helpers/Lightpanda.md +64 -64
- package/docs/helpers/Moli.md +64 -64
- package/docs/helpers/Obscura.md +64 -64
- package/docs/helpers/Playwright.md +125 -125
- package/docs/helpers/Puppeteer.md +85 -85
- package/docs/helpers/WebDriver.md +86 -86
- package/docs/locators.md +5 -7
- package/docs/migration-4.md +7 -3
- package/docs/retry.md +1 -1
- package/docs/timeouts.md +2 -2
- package/lib/actor.js +20 -2
- package/lib/data/context.js +24 -0
- package/lib/element/WebElement.js +5 -5
- package/lib/helper/Appium.js +1 -1
- package/lib/helper/Playwright.js +21 -11
- package/lib/helper/Puppeteer.js +2 -8
- package/lib/recorder.js +2 -1
- package/lib/step/base.js +8 -1
- package/lib/step/record.js +22 -17
- package/lib/step/retry.js +13 -6
- package/lib/utils/typescript.js +86 -12
- package/package.json +2 -1
- package/typings/index.d.ts +12 -0
- package/typings/promiseBasedTypes.d.ts +690 -345
- package/typings/types.d.ts +479 -346
package/docs/locators.md
CHANGED
|
@@ -158,17 +158,15 @@ I.tap('~submit') // mobile
|
|
|
158
158
|
|
|
159
159
|
## Picking a specific element
|
|
160
160
|
|
|
161
|
-
When a locator matches several elements on the page, CodeceptJS acts on the first one by default. To target a different match,
|
|
161
|
+
When a locator matches several elements on the page, CodeceptJS acts on the first one by default. To target a different match, chain `first()`, `last()` or `at()` to the action:
|
|
162
162
|
|
|
163
163
|
```js
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
I.
|
|
167
|
-
I.click('a', step.opts({ elementIndex: 'last' })) // the last link
|
|
168
|
-
I.fillField('.email-input', 'u@t.com', step.opts({ elementIndex: -1 }))
|
|
164
|
+
I.click('a').at(2) // the 2nd link
|
|
165
|
+
I.click('a').last() // the last link
|
|
166
|
+
I.fillField('.email-input', 'u@t.com').at(-1)
|
|
169
167
|
```
|
|
170
168
|
|
|
171
|
-
`
|
|
169
|
+
`at()` accepts positive numbers (1-based) and negative numbers (`-1` is last). It works with `click`, `fillField`, `selectOption`, `checkOption`, and other single-element actions. The same selection is available as `step.opts({ elementIndex: 2 })`.
|
|
172
170
|
|
|
173
171
|
To catch ambiguous locators during development rather than silently using the first match, enable `strict: true` in the helper config, or pass `step.opts({ exact: true })` on a single step:
|
|
174
172
|
|
package/docs/migration-4.md
CHANGED
|
@@ -534,6 +534,8 @@ I.retry(3).click('Submit') // I.click('Submit', step.retry(3))
|
|
|
534
534
|
I.limitTime(10).fillField('Email', 'a@b.c') // I.fillField('Email', 'a@b.c', step.timeout(10))
|
|
535
535
|
```
|
|
536
536
|
|
|
537
|
+
You can also chain them to the action: `I.click('Submit').retry(3)`, `I.fillField('Email', 'a@b.c').timeout(10)`. Pass a whole config with `I.click('Submit').stepConfig(step.retry(3).timeout(10))`.
|
|
538
|
+
|
|
537
539
|
`step.*` configs are also composable with the other step options:
|
|
538
540
|
|
|
539
541
|
```js
|
|
@@ -648,11 +650,13 @@ The error includes a `fetchDetails()` method that prints XPaths and HTML for eve
|
|
|
648
650
|
Pick a specific match without writing a more specific locator:
|
|
649
651
|
|
|
650
652
|
```js
|
|
651
|
-
I.click('a'
|
|
652
|
-
I.click('a'
|
|
653
|
-
I.fillField('input', 'x'
|
|
653
|
+
I.click('a').at(2)
|
|
654
|
+
I.click('a').last()
|
|
655
|
+
I.fillField('input', 'x').first()
|
|
654
656
|
```
|
|
655
657
|
|
|
658
|
+
`step.opts({ elementIndex: 2 })` does the same.
|
|
659
|
+
|
|
656
660
|
### Unfocused Element Detection
|
|
657
661
|
|
|
658
662
|
`I.type()` and `I.pressKey()` throw `NonFocusedType` if no element has focus. Click or focus the field first.
|
package/docs/retry.md
CHANGED
|
@@ -119,7 +119,7 @@ import step from 'codeceptjs/steps'
|
|
|
119
119
|
Scenario('checkout', ({ I }) => {
|
|
120
120
|
I.amOnPage('/cart')
|
|
121
121
|
I.click('Proceed to Checkout', step.retry(5)) // retry up to 5 times
|
|
122
|
-
I.see('Payment')
|
|
122
|
+
I.see('Payment').retry(3) // same, chained to the action
|
|
123
123
|
})
|
|
124
124
|
```
|
|
125
125
|
|
package/docs/timeouts.md
CHANGED
|
@@ -9,14 +9,14 @@ CodeceptJS provides multiple timeout configurations to control test execution ti
|
|
|
9
9
|
|
|
10
10
|
## Step Timeout
|
|
11
11
|
|
|
12
|
-
Set timeout for individual steps
|
|
12
|
+
Set timeout for individual steps by chaining `timeout()` to the action, or by passing `step.timeout()`:
|
|
13
13
|
|
|
14
14
|
```js
|
|
15
15
|
import step from 'codeceptjs/steps'
|
|
16
16
|
|
|
17
17
|
Scenario('test with step timeouts', ({ I }) => {
|
|
18
18
|
I.amOnPage('/')
|
|
19
|
-
I.click('Slow Button'
|
|
19
|
+
I.click('Slow Button').timeout(30)
|
|
20
20
|
I.fillField('Email', 'user@example.com', step.timeout(10))
|
|
21
21
|
})
|
|
22
22
|
```
|
package/lib/actor.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Step, { MetaStep } from './step.js'
|
|
2
|
-
import recordStep from './step/record.js'
|
|
2
|
+
import recordStep, { applyStepConfig } from './step/record.js'
|
|
3
|
+
import StepConfig from './step/config.js'
|
|
3
4
|
import { methodsOfObject } from './utils.js'
|
|
4
5
|
import event from './event.js'
|
|
5
6
|
import output from './output.js'
|
|
@@ -29,6 +30,23 @@ class Actor {
|
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
function addStepConfigChain(promise, step) {
|
|
34
|
+
promise.stepConfig = config => {
|
|
35
|
+
if (step.status !== 'queued') {
|
|
36
|
+
throw new Error(`Can't configure "${step.title}": the step was already executed. Chain it to the step call: I.${step.title}(...).first()`)
|
|
37
|
+
}
|
|
38
|
+
applyStepConfig(step, config)
|
|
39
|
+
return promise
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
promise.first = () => promise.stepConfig(new StepConfig({ elementIndex: 'first' }))
|
|
43
|
+
promise.last = () => promise.stepConfig(new StepConfig({ elementIndex: 'last' }))
|
|
44
|
+
promise.at = position => promise.stepConfig(new StepConfig({ elementIndex: position }))
|
|
45
|
+
promise.timeout = seconds => promise.stepConfig(new StepConfig().timeout(seconds))
|
|
46
|
+
promise.retry = retry => promise.stepConfig(new StepConfig().retry(retry))
|
|
47
|
+
return promise
|
|
48
|
+
}
|
|
49
|
+
|
|
32
50
|
/**
|
|
33
51
|
* Fetches all methods from all enabled helpers,
|
|
34
52
|
* and makes them available to use from I. object
|
|
@@ -63,7 +81,7 @@ export default function (obj = {}, container) {
|
|
|
63
81
|
step.actor = translation.I
|
|
64
82
|
}
|
|
65
83
|
// add methods to promise chain
|
|
66
|
-
return recordStep(step, Array.from(arguments))
|
|
84
|
+
return addStepConfigChain(recordStep(step, Array.from(arguments)), step)
|
|
67
85
|
}
|
|
68
86
|
}
|
|
69
87
|
})
|
package/lib/data/context.js
CHANGED
|
@@ -26,6 +26,30 @@ export default function (context) {
|
|
|
26
26
|
maskSecretInTitle(scenarios)
|
|
27
27
|
return new DataScenarioConfig(scenarios)
|
|
28
28
|
},
|
|
29
|
+
Before(fn) {
|
|
30
|
+
data.forEach(dataRow => {
|
|
31
|
+
if (!dataRow.skip) {
|
|
32
|
+
const wrappedFn = (args) => {
|
|
33
|
+
if (args && args.test && args.test.inject && args.test.inject.current === dataRow.data) {
|
|
34
|
+
return fn({ ...args, current: dataRow.data })
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
context.Before(wrappedFn)
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
},
|
|
41
|
+
After(fn) {
|
|
42
|
+
data.forEach(dataRow => {
|
|
43
|
+
if (!dataRow.skip) {
|
|
44
|
+
const wrappedFn = (args) => {
|
|
45
|
+
if (args && args.test && args.test.inject && args.test.inject.current === dataRow.data) {
|
|
46
|
+
return fn({ ...args, current: dataRow.data })
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
context.After(wrappedFn)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
},
|
|
29
53
|
only: {
|
|
30
54
|
Scenario(title, opts, fn) {
|
|
31
55
|
const scenarios = []
|
|
@@ -29,7 +29,7 @@ class WebElement {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Get the native element instance
|
|
32
|
-
* @returns {
|
|
32
|
+
* @returns {any} Native element (Playwright/Puppeteer ElementHandle or WebdriverIO element)
|
|
33
33
|
*/
|
|
34
34
|
getNativeElement() {
|
|
35
35
|
return this.element
|
|
@@ -220,7 +220,7 @@ class WebElement {
|
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
222
|
* Click the element
|
|
223
|
-
* @param {Object} options Click options
|
|
223
|
+
* @param {Object} [options] Click options
|
|
224
224
|
* @returns {Promise<void>}
|
|
225
225
|
*/
|
|
226
226
|
async click(options = {}) {
|
|
@@ -239,7 +239,7 @@ class WebElement {
|
|
|
239
239
|
/**
|
|
240
240
|
* Type text into the element
|
|
241
241
|
* @param {string} text Text to type
|
|
242
|
-
* @param {Object} options Type options
|
|
242
|
+
* @param {Object} [options] Type options
|
|
243
243
|
* @returns {Promise<void>}
|
|
244
244
|
*/
|
|
245
245
|
async type(text, options = {}) {
|
|
@@ -362,7 +362,7 @@ class WebElement {
|
|
|
362
362
|
* Treat this element as an iframe; invoke `fn` with a WebElement wrapping
|
|
363
363
|
* the iframe body. For WebDriver this switches the browser into the frame
|
|
364
364
|
* for the duration of the callback and switches back on exit.
|
|
365
|
-
* @param {(
|
|
365
|
+
* @param {function(WebElement): Promise<any>} fn
|
|
366
366
|
* @returns {Promise<any>} Return value of fn
|
|
367
367
|
*/
|
|
368
368
|
async inIframe(fn) {
|
|
@@ -530,7 +530,7 @@ class WebElement {
|
|
|
530
530
|
* @param {object} [opts]
|
|
531
531
|
* @param {number} [opts.maxHtmlLength=300] passed through to toSimplifiedHTML
|
|
532
532
|
* @param {string[]} [opts.attrs] attribute names to surface
|
|
533
|
-
* @returns {Promise<{text
|
|
533
|
+
* @returns {Promise<{text: (string|undefined), html: (string|undefined), visible: (boolean|undefined), enabled: (boolean|undefined), attrs: (Object|undefined)}>}
|
|
534
534
|
*/
|
|
535
535
|
async describe({ maxHtmlLength = 300, attrs = ['id', 'class', 'name', 'role', 'type', 'href', 'value', 'aria-label', 'placeholder', 'data-testid'] } = {}) {
|
|
536
536
|
const out = {}
|
package/lib/helper/Appium.js
CHANGED
|
@@ -1738,7 +1738,7 @@ class Appium extends Webdriver {
|
|
|
1738
1738
|
* ```
|
|
1739
1739
|
*
|
|
1740
1740
|
* @param {string} fileName file name to save.
|
|
1741
|
-
* @return {
|
|
1741
|
+
* @return {CodeceptJS.StepPromise}
|
|
1742
1742
|
*/
|
|
1743
1743
|
async saveScreenshot(fileName) {
|
|
1744
1744
|
return super.saveScreenshot(fileName, false)
|
package/lib/helper/Playwright.js
CHANGED
|
@@ -554,9 +554,10 @@ class Playwright extends Helper {
|
|
|
554
554
|
return
|
|
555
555
|
}
|
|
556
556
|
|
|
557
|
-
// Start browser if not manually started and not already running
|
|
558
|
-
//
|
|
559
|
-
|
|
557
|
+
// Start browser if not manually started and not already running.
|
|
558
|
+
// With the browser restart strategy, _before() starts a browser for each test,
|
|
559
|
+
// and a browser started here would never be closed (#5784)
|
|
560
|
+
if (!this.options.manualStart && !this.isRunning && !restartsBrowser()) {
|
|
560
561
|
this.debugSection('Session', 'Starting singleton browser session')
|
|
561
562
|
return this._startBrowser()
|
|
562
563
|
}
|
|
@@ -2140,7 +2141,7 @@ class Playwright extends Helper {
|
|
|
2140
2141
|
* @param {CodeceptJS.LocatorOrString|number} locator Element to click on or X coordinate if no element.
|
|
2141
2142
|
* @param {number} [x] X coordinate relative to element, or Y coordinate if locator is a number.
|
|
2142
2143
|
* @param {number} [y] Y coordinate relative to element.
|
|
2143
|
-
* @returns {
|
|
2144
|
+
* @returns {CodeceptJS.ElementStepPromise}
|
|
2144
2145
|
*/
|
|
2145
2146
|
async clickXY(locator, x, y) {
|
|
2146
2147
|
// If locator is a number, treat it as global X coordinate
|
|
@@ -3565,6 +3566,8 @@ class Playwright extends Helper {
|
|
|
3565
3566
|
|
|
3566
3567
|
/**
|
|
3567
3568
|
* {{> waitForText }}
|
|
3569
|
+
*
|
|
3570
|
+
* . **Note:** Text matching is case-sensitive. For case-insensitive matching, use XPath or CSS locators with normalized text.
|
|
3568
3571
|
*/
|
|
3569
3572
|
async waitForText(text, sec = null, context = null) {
|
|
3570
3573
|
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
|
|
@@ -3576,13 +3579,20 @@ class Playwright extends Helper {
|
|
|
3576
3579
|
const locator = new Locator(context, 'css')
|
|
3577
3580
|
try {
|
|
3578
3581
|
if (!locator.isXPath()) {
|
|
3579
|
-
|
|
3580
|
-
.
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3582
|
+
try {
|
|
3583
|
+
const deadline = Date.now() + waitTimeout
|
|
3584
|
+
do {
|
|
3585
|
+
const els = await this._locate(locator)
|
|
3586
|
+
for (const el of els) {
|
|
3587
|
+
const texts = await el.evaluateAll(nodes => nodes.map(node => node.innerText ?? node.textContent))
|
|
3588
|
+
if (texts.some(t => typeof t === 'string' && t.includes(text))) return
|
|
3589
|
+
}
|
|
3590
|
+
await new Promise(resolve => setTimeout(resolve, 100))
|
|
3591
|
+
} while (Date.now() < deadline)
|
|
3592
|
+
throw new Error(errorMessage)
|
|
3593
|
+
} catch (e) {
|
|
3594
|
+
throw new Error(errorMessage)
|
|
3595
|
+
}
|
|
3586
3596
|
}
|
|
3587
3597
|
|
|
3588
3598
|
if (locator.isXPath()) {
|
package/lib/helper/Puppeteer.js
CHANGED
|
@@ -1097,12 +1097,6 @@ class Puppeteer extends Helper {
|
|
|
1097
1097
|
return new WebElement(elements[0], this)
|
|
1098
1098
|
}
|
|
1099
1099
|
|
|
1100
|
-
async grabWebElement(locator) {
|
|
1101
|
-
const els = await this._locate(locator)
|
|
1102
|
-
assertElementExists(els, locator)
|
|
1103
|
-
return els[0]
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
1100
|
/**
|
|
1107
1101
|
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
|
|
1108
1102
|
*
|
|
@@ -1299,7 +1293,7 @@ class Puppeteer extends Helper {
|
|
|
1299
1293
|
} else {
|
|
1300
1294
|
assertElementExists(els, locator, 'Clickable element')
|
|
1301
1295
|
}
|
|
1302
|
-
const elem = els
|
|
1296
|
+
const elem = selectElement(els, locator, this)
|
|
1303
1297
|
return this.executeScript(el => {
|
|
1304
1298
|
if (document.activeElement instanceof HTMLElement) {
|
|
1305
1299
|
document.activeElement.blur()
|
|
@@ -2271,7 +2265,7 @@ class Puppeteer extends Helper {
|
|
|
2271
2265
|
const els = await this._locate(locator)
|
|
2272
2266
|
const array = []
|
|
2273
2267
|
for (let index = 0; index < els.length; index++) {
|
|
2274
|
-
const a = await this._evaluateHandeInContext((el, attr) => el[attr] || el.getAttribute(attr), els[index], attr)
|
|
2268
|
+
const a = await this._evaluateHandeInContext((el, attr) => (attr === 'style' ? el.getAttribute(attr) : el[attr] || el.getAttribute(attr)), els[index], attr)
|
|
2275
2269
|
array.push(a)
|
|
2276
2270
|
}
|
|
2277
2271
|
return array
|
package/lib/recorder.js
CHANGED
|
@@ -197,7 +197,7 @@ export default {
|
|
|
197
197
|
* undefined: `add(fn)` -> `false` and `add('step',fn)` -> `true`
|
|
198
198
|
* true: it will retries if `retryOpts` set.
|
|
199
199
|
* false: ignore `retryOpts` and won't retry.
|
|
200
|
-
* @param {number} [timeout]
|
|
200
|
+
* @param {number|function} [timeout] in milliseconds, or a function returning it when the task starts
|
|
201
201
|
* @return {Promise<*>}
|
|
202
202
|
* @inner
|
|
203
203
|
*/
|
|
@@ -215,6 +215,7 @@ export default {
|
|
|
215
215
|
debug(chalk.gray(`${currentQueue()} Queued | ${taskName}`))
|
|
216
216
|
|
|
217
217
|
return (promise = Promise.resolve(promise).then(res => {
|
|
218
|
+
if (typeof timeout === 'function') timeout = timeout()
|
|
218
219
|
debug(`${currentQueue()} Running | ${taskName} | Timeout: ${timeout || 'None'}`)
|
|
219
220
|
|
|
220
221
|
const runTask = () => {
|
package/lib/step/base.js
CHANGED
|
@@ -5,6 +5,7 @@ import { getCurrentTimeout } from '../timeout.js'
|
|
|
5
5
|
import { ucfirst, humanizeString, serializeError } from '../utils.js'
|
|
6
6
|
import recordStep from './record.js'
|
|
7
7
|
import store from '../store.js'
|
|
8
|
+
import { mapTranspiledStackFrame } from '../utils/typescript.js'
|
|
8
9
|
|
|
9
10
|
const STACK_LINE = 5
|
|
10
11
|
|
|
@@ -163,7 +164,13 @@ class Step {
|
|
|
163
164
|
if (lines[STACK_LINE]) {
|
|
164
165
|
let line = lines[STACK_LINE].trim()
|
|
165
166
|
|
|
166
|
-
// Map .temp.mjs back to original .ts
|
|
167
|
+
// Map .temp.mjs frames back to the original .ts source position using the
|
|
168
|
+
// inline source map emitted during transpilation (#5772)
|
|
169
|
+
if (line.includes('.temp.mjs')) {
|
|
170
|
+
line = mapTranspiledStackFrame(line) || line
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Fallback: map .temp.mjs back to original .ts files using container's tsFileMapping
|
|
167
174
|
const fileMapping = store.tsFileMapping
|
|
168
175
|
if (line.includes('.temp.mjs') && fileMapping) {
|
|
169
176
|
for (const [tsFile, mjsFile] of fileMapping.entries()) {
|
package/lib/step/record.js
CHANGED
|
@@ -4,28 +4,30 @@ import StepConfig from './config.js'
|
|
|
4
4
|
import output from '../output.js'
|
|
5
5
|
import store from '../store.js'
|
|
6
6
|
import { TIMEOUT_ORDER } from '../timeout.js'
|
|
7
|
-
import retryStep from './retry.js'
|
|
7
|
+
import retryStep, { releaseStepRetry } from './retry.js'
|
|
8
8
|
import { fixErrorStack } from '../utils/typescript.js'
|
|
9
|
+
|
|
10
|
+
function applyStepConfig(step, stepConfig) {
|
|
11
|
+
const { opts, timeout, retry } = stepConfig.getConfig()
|
|
12
|
+
|
|
13
|
+
if (opts) {
|
|
14
|
+
output.debug(`Step ${step.title}: options applied ${JSON.stringify(opts)}`)
|
|
15
|
+
step.opts = { ...step.opts, ...opts }
|
|
16
|
+
store.stepOptions = step.opts
|
|
17
|
+
}
|
|
18
|
+
if (timeout) {
|
|
19
|
+
output.debug(`Step ${step.title} timeout ${timeout}s`)
|
|
20
|
+
step.setTimeout(timeout * 1000, TIMEOUT_ORDER.codeLimitTime)
|
|
21
|
+
}
|
|
22
|
+
if (retry) retryStep(step, retry)
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
function recordStep(step, args) {
|
|
10
26
|
step.status = 'queued'
|
|
11
27
|
|
|
12
28
|
// apply step configuration
|
|
13
29
|
const lastArg = args[args.length - 1]
|
|
14
|
-
if (StepConfig.isStepConfig(lastArg))
|
|
15
|
-
const stepConfig = args.pop()
|
|
16
|
-
const { opts, timeout, retry } = stepConfig.getConfig()
|
|
17
|
-
|
|
18
|
-
if (opts) {
|
|
19
|
-
output.debug(`Step ${step.title}: options applied ${JSON.stringify(opts)}`)
|
|
20
|
-
store.stepOptions = opts
|
|
21
|
-
step.opts = opts
|
|
22
|
-
}
|
|
23
|
-
if (timeout) {
|
|
24
|
-
output.debug(`Step ${step.title} timeout ${timeout}s`)
|
|
25
|
-
step.setTimeout(timeout * 1000, TIMEOUT_ORDER.codeLimitTime)
|
|
26
|
-
}
|
|
27
|
-
if (retry) retryStep(retry)
|
|
28
|
-
}
|
|
30
|
+
if (StepConfig.isStepConfig(lastArg)) applyStepConfig(step, args.pop())
|
|
29
31
|
|
|
30
32
|
step.setArguments(args)
|
|
31
33
|
// run async before step hooks
|
|
@@ -47,18 +49,20 @@ function recordStep(step, args) {
|
|
|
47
49
|
},
|
|
48
50
|
false,
|
|
49
51
|
undefined,
|
|
50
|
-
step.timeout,
|
|
52
|
+
() => step.timeout,
|
|
51
53
|
)
|
|
52
54
|
|
|
53
55
|
event.emit(event.step.after, step)
|
|
54
56
|
|
|
55
57
|
recorder.add('step passed', () => {
|
|
58
|
+
releaseStepRetry(step)
|
|
56
59
|
step.endTime = +Date.now()
|
|
57
60
|
event.emit(event.step.passed, step, val)
|
|
58
61
|
event.emit(event.step.finished, step)
|
|
59
62
|
})
|
|
60
63
|
|
|
61
64
|
recorder.catch(err => {
|
|
65
|
+
releaseStepRetry(step)
|
|
62
66
|
step.status = 'failed'
|
|
63
67
|
step.endTime = +Date.now()
|
|
64
68
|
|
|
@@ -79,4 +83,5 @@ function recordStep(step, args) {
|
|
|
79
83
|
return recorder.promise()
|
|
80
84
|
}
|
|
81
85
|
|
|
86
|
+
export { applyStepConfig }
|
|
82
87
|
export default recordStep
|
package/lib/step/retry.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import recorder from '../recorder.js'
|
|
2
|
-
import event from '../event.js'
|
|
3
2
|
|
|
4
|
-
function retryStep(opts) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
recorder.
|
|
3
|
+
function retryStep(step, opts) {
|
|
4
|
+
releaseStepRetry(step)
|
|
5
|
+
let rule = opts
|
|
6
|
+
if (Number.isInteger(opts)) rule = { retries: opts }
|
|
7
|
+
recorder.retry(rule)
|
|
8
|
+
step.retryRule = rule
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
function releaseStepRetry(step) {
|
|
12
|
+
if (!step.retryRule) return
|
|
13
|
+
recorder.retries = recorder.retries.filter(rule => rule !== step.retryRule)
|
|
14
|
+
step.retryRule = null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { releaseStepRetry }
|
|
11
18
|
export default retryStep
|
package/lib/utils/typescript.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import path from 'path'
|
|
3
|
-
import { pathToFileURL } from 'url'
|
|
3
|
+
import { pathToFileURL, fileURLToPath } from 'url'
|
|
4
|
+
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Load tsconfig.json if it exists
|
|
@@ -65,8 +66,6 @@ function resolveTsPathAlias(importPath, tsConfig, configDir) {
|
|
|
65
66
|
* @returns {Promise<{tempFile: string, allTempFiles: string[], fileMapping: any}>} - Main temp file and all temp files created
|
|
66
67
|
*/
|
|
67
68
|
export async function transpileTypeScript(mainFilePath, typescript) {
|
|
68
|
-
const { transpile } = typescript
|
|
69
|
-
|
|
70
69
|
/**
|
|
71
70
|
* Transpile a single TypeScript file to JavaScript
|
|
72
71
|
* Injects CommonJS shims (require, module, exports, __dirname, __filename) as needed
|
|
@@ -74,17 +73,31 @@ export async function transpileTypeScript(mainFilePath, typescript) {
|
|
|
74
73
|
const transpileTS = (filePath) => {
|
|
75
74
|
const tsContent = fs.readFileSync(filePath, 'utf8')
|
|
76
75
|
|
|
77
|
-
// Transpile TypeScript to JavaScript with ES module output
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
// Transpile TypeScript to JavaScript with ES module output.
|
|
77
|
+
// transpileModule (unlike transpile) can emit a source map, which we inline
|
|
78
|
+
// so stack frames can later be mapped back to the original .ts positions.
|
|
79
|
+
const { outputText, sourceMapText } = typescript.transpileModule(tsContent, {
|
|
80
|
+
compilerOptions: {
|
|
81
|
+
module: 99, // ModuleKind.ESNext
|
|
82
|
+
target: 99, // ScriptTarget.ESNext
|
|
83
|
+
esModuleInterop: true,
|
|
84
|
+
allowSyntheticDefaultImports: true,
|
|
85
|
+
lib: ['lib.esnext.d.ts'], // Enable latest features including top-level await
|
|
86
|
+
suppressOutputPathCheck: true,
|
|
87
|
+
skipLibCheck: true,
|
|
88
|
+
sourceMap: true,
|
|
89
|
+
inlineSources: true,
|
|
90
|
+
},
|
|
91
|
+
fileName: path.resolve(filePath),
|
|
86
92
|
})
|
|
87
93
|
|
|
94
|
+
// transpileModule appends an external sourceMappingURL comment; we inline the map instead
|
|
95
|
+
let jsContent = outputText.replace(/\/\/# sourceMappingURL=.*(\r?\n)?$/, '')
|
|
96
|
+
const sourceMap = JSON.parse(sourceMapText)
|
|
97
|
+
// transpileModule records only the base file name; store the absolute path
|
|
98
|
+
// so mapped stack frames can be resolved back to the source file
|
|
99
|
+
sourceMap.sources = [path.resolve(filePath)]
|
|
100
|
+
|
|
88
101
|
// Check if the code uses CommonJS globals
|
|
89
102
|
const usesCommonJSGlobals = /__dirname|__filename/.test(jsContent)
|
|
90
103
|
const usesRequire = /\brequire\s*\(/.test(jsContent)
|
|
@@ -150,12 +163,22 @@ const __dirname = __dirname_fn(__filename);
|
|
|
150
163
|
|
|
151
164
|
jsContent = esmGlobals + jsContent
|
|
152
165
|
|
|
166
|
+
// The injected shim shifts every generated line down; reflect that in the
|
|
167
|
+
// source map by prepending one empty mapping entry per shim line.
|
|
168
|
+
const shimLineCount = esmGlobals.split('\n').length - 1
|
|
169
|
+
sourceMap.mappings = `${';'.repeat(shimLineCount)}${sourceMap.mappings}`
|
|
170
|
+
|
|
153
171
|
// If module.exports is used, we need to export it as default
|
|
154
172
|
if (usesModuleExports) {
|
|
155
173
|
jsContent += `\nexport default module.exports;\n`
|
|
156
174
|
}
|
|
157
175
|
}
|
|
158
176
|
|
|
177
|
+
// Append the source map inline so stack frames in the temp file can be
|
|
178
|
+
// mapped back to the original .ts positions (see mapTranspiledStackFrame)
|
|
179
|
+
const inlineMap = Buffer.from(JSON.stringify(sourceMap), 'utf8').toString('base64')
|
|
180
|
+
jsContent += `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${inlineMap}\n`
|
|
181
|
+
|
|
159
182
|
return jsContent
|
|
160
183
|
}
|
|
161
184
|
|
|
@@ -407,6 +430,57 @@ const __dirname = __dirname_fn(__filename);
|
|
|
407
430
|
return { tempFile: tempFileUrl, allTempFiles, fileMapping: transpiledFiles }
|
|
408
431
|
}
|
|
409
432
|
|
|
433
|
+
const sourceMapCache = new Map()
|
|
434
|
+
|
|
435
|
+
function getInlineSourceMap(tempFilePath) {
|
|
436
|
+
if (sourceMapCache.has(tempFilePath)) return sourceMapCache.get(tempFilePath)
|
|
437
|
+
|
|
438
|
+
let traceMap = null
|
|
439
|
+
try {
|
|
440
|
+
const content = fs.readFileSync(tempFilePath, 'utf8')
|
|
441
|
+
const match = /\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,([A-Za-z0-9+/=]+)\s*$/.exec(content)
|
|
442
|
+
if (match) {
|
|
443
|
+
traceMap = new TraceMap(JSON.parse(Buffer.from(match[1], 'base64').toString('utf8')))
|
|
444
|
+
}
|
|
445
|
+
} catch {
|
|
446
|
+
// No usable inline source map; callers fall back to path-only mapping
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
sourceMapCache.set(tempFilePath, traceMap)
|
|
450
|
+
return traceMap
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Map a stack frame pointing into a transpiled `.temp.mjs` file back to the
|
|
455
|
+
* original TypeScript source position, using the inline source map emitted
|
|
456
|
+
* during transpilation.
|
|
457
|
+
*
|
|
458
|
+
* @param {string} frame - Single stack frame line, e.g. `at Object.open (file:///path/foo.123.temp.mjs:4:11)`
|
|
459
|
+
* @returns {string|null} - Rewritten frame with the original source position, or null when mapping is not possible
|
|
460
|
+
*/
|
|
461
|
+
export function mapTranspiledStackFrame(frame) {
|
|
462
|
+
const match = /\((file:\/\/)?([^()]*?\.temp\.mjs):(\d+):(\d+)\)/.exec(frame)
|
|
463
|
+
if (!match) return null
|
|
464
|
+
|
|
465
|
+
const [, isFileUrl, fileRef, genLine, genColumn] = match
|
|
466
|
+
let tempFilePath = fileRef
|
|
467
|
+
if (isFileUrl) {
|
|
468
|
+
try {
|
|
469
|
+
tempFilePath = fileURLToPath(`${isFileUrl}${fileRef}`)
|
|
470
|
+
} catch {
|
|
471
|
+
return null
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const traceMap = getInlineSourceMap(tempFilePath)
|
|
475
|
+
if (!traceMap) return null
|
|
476
|
+
|
|
477
|
+
// V8 reports 1-based columns, trace-mapping expects 0-based
|
|
478
|
+
const pos = originalPositionFor(traceMap, { line: Number(genLine), column: Number(genColumn) - 1 })
|
|
479
|
+
if (!pos || pos.line == null || !pos.source) return null
|
|
480
|
+
|
|
481
|
+
return frame.replace(match[0], `(${pos.source}:${pos.line}:${pos.column + 1})`)
|
|
482
|
+
}
|
|
483
|
+
|
|
410
484
|
/**
|
|
411
485
|
* Map error stack traces from temp .mjs files back to original .ts files
|
|
412
486
|
* @param {Error} error - The error object to fix
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codeceptjs",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Supercharged End 2 End Testing Framework for NodeJS",
|
|
6
6
|
"keywords": [
|
|
@@ -97,6 +97,7 @@
|
|
|
97
97
|
"@cucumber/cucumber-expressions": "18",
|
|
98
98
|
"@cucumber/gherkin": "38.0.0",
|
|
99
99
|
"@cucumber/messages": "32.3.1",
|
|
100
|
+
"@jridgewell/trace-mapping": "^0.3.31",
|
|
100
101
|
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
101
102
|
"@xmldom/xmldom": "^0.9.12",
|
|
102
103
|
"acorn": "8.15.0",
|
package/typings/index.d.ts
CHANGED
|
@@ -527,6 +527,18 @@ declare namespace CodeceptJS {
|
|
|
527
527
|
|
|
528
528
|
type StringOrSecret = string | CodeceptJS.Secret
|
|
529
529
|
|
|
530
|
+
interface StepPromise extends Promise<any> {
|
|
531
|
+
stepConfig(config: CodeceptJS.StepConfig): this
|
|
532
|
+
timeout(seconds: number): this
|
|
533
|
+
retry(retry: number | object): this
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
interface ElementStepPromise extends StepPromise {
|
|
537
|
+
first(): this
|
|
538
|
+
last(): this
|
|
539
|
+
at(position: number): this
|
|
540
|
+
}
|
|
541
|
+
|
|
530
542
|
interface GherkinCallback {
|
|
531
543
|
(test: Test): void | Promise<void>
|
|
532
544
|
}
|