Skip to content

Commit dce5e45

Browse files
committed
Fixed top level commands trace
1 parent 7610ddd commit dce5e45

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

packages/service/src/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ export const DEFAULT_LAUNCH_CAPS: WebdriverIO.Capabilities = {
1313
args: ['--window-size=1600,1200', '--auto-open-devtools-for-tabs']
1414
}
1515
}
16+
17+
export const INTERNAL_COMMANDS = [
18+
'$', '$$', 'emit', 'browsingContextLocateNodes', 'browsingContextNavigate',
19+
'waitUntil', 'getTitle', 'getUrl', 'getWindowSize', 'setWindowSize', 'deleteSession',
20+
'findElementFromShadowRoot', 'findElementsFromShadowRoot', 'waitForExist', 'browsingContextGetTree',
21+
'scriptCallFunction', 'getElement', 'execute'
22+
]
23+
24+
export const SPEC_FILE_PATTERN = /(test|spec|features)[\\/].*\.(js|ts)$/i

packages/service/src/index.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { DevToolsAppLauncher } from './launcher.js'
1313
import { getBrowserObject } from './utils.ts'
1414
import { parse } from 'stack-trace'
1515
import { type TraceLog, TraceType } from './types.ts'
16+
import { INTERNAL_COMMANDS, SPEC_FILE_PATTERN } from './constants.ts'
1617

1718
export const launcher = DevToolsAppLauncher
1819

@@ -131,19 +132,23 @@ export default class DevToolsHookService implements Services.ServiceInstance {
131132
}
132133

133134
/**
135+
* Hook for Cucumber framework.
134136
* beforeScenario is triggered at the beginning of every worker session, therefore
135137
* we can use it to reset the command stack and last command signature
136138
*/
137139
beforeScenario() {
138-
this.#lastCommandSig = null;
139-
this.#commandStack = []
140+
this.resetStack()
140141
}
141142

142143
/**
143-
* Hook for Mocha/Jasmine.
144+
* Hook for Mocha/Jasmine frameworks.
144145
* It does the exact same thing as beforeScenario.
145146
*/
146147
beforeTest() {
148+
this.resetStack()
149+
}
150+
151+
private resetStack() {
147152
this.#lastCommandSig = null
148153
this.#commandStack = []
149154
}
@@ -164,17 +169,15 @@ export default class DevToolsHookService implements Services.ServiceInstance {
164169
/**
165170
* Smart stack filtering to detect top-level user commands
166171
*/
167-
const stack = parse(new Error(''))
168-
const source = stack.find((frame) =>
169-
Boolean(frame.getFileName()) &&
170-
!frame.getFileName()?.includes('node_modules')
171-
)
172-
173-
// If the command is a selector command, we don't want to capture it
174-
// as it is not a top-level user command.
175-
const isSelectorCommand = command.startsWith('$') || command.includes('LocateNodes')
176-
177-
if (source && this.#commandStack.length === 0 && !isSelectorCommand) {
172+
Error.stackTraceLimit = 20
173+
const stack = parse(new Error('')).reverse()
174+
const source = stack.find((frame) => {
175+
const file = frame.getFileName()
176+
// Only consider frames from user spec/test files
177+
return file && SPEC_FILE_PATTERN.test(file)
178+
})
179+
180+
if (source && this.#commandStack.length === 0 && !INTERNAL_COMMANDS.includes(command)) {
178181
const cmdSig = JSON.stringify({
179182
command,
180183
args,

0 commit comments

Comments
 (0)