Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ export default class WdioMochaTestFramework extends TestFramework {
*/
loadLogEntries(instance: TestFrameworkInstance, testFrameworkState: State, hookState: State, logEntry: Record<string, unknown>) {
const logRecord: Record<string, unknown> = {}
const { level, message, timestamp } = logEntry
const { level, message, timestamp, kind } = logEntry

if (CLIUtils.matchHookRegex(instance.getCurrentTestState().toString())) {
logRecord[TestFrameworkConstants.KEY_HOOK_ID] = TestFramework.getState(instance, TestFrameworkConstants.KEY_HOOK_ID)
}
logRecord.kind = TestFrameworkConstants.KIND_LOG
// SDK-6277: honor the incoming log kind (e.g. TEST_SCREENSHOT) so screenshots route correctly
// on the binary side; default to TEST_LOG for ordinary stdout/console logs.
logRecord.kind = (kind as string) ?? TestFrameworkConstants.KIND_LOG
logRecord.message = Buffer.from(message as string)
logRecord.level = level
logRecord.timestamp = timestamp
Expand Down
35 changes: 29 additions & 6 deletions packages/wdio-browserstack-service/src/insights-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,35 @@ class _InsightsHandler {
const body = 'body' in args ? args.body : undefined
const result = 'result' in args ? args.result : undefined
if (Boolean(process.env[TESTOPS_SCREENSHOT_ENV]) && isScreenshotCommand(args) && result?.value) {
await this.listener.onScreenshot([{
test_run_uuid: testMeta.uuid,
timestamp: new Date().toISOString(),
message: result.value,
kind: 'TEST_SCREENSHOT'
}])
/**
* SDK-6277: in the CLI/binary (v8) flow, forward the screenshot to the binary over gRPC
* as a TEST_SCREENSHOT log. The binary uploads it via its own authorized testhub session.
* The direct-HTTP listener.onScreenshot() path 401s in CLI mode (the worker's binary-issued
* JWT is not valid for direct collector POSTs).
*/
await (BrowserstackCLI.getInstance().isRunning()
? BrowserstackCLI.getInstance().getTestFramework()!.trackEvent(TestFrameworkState.LOG, HookState.POST, {
logEntry: {
kind: 'TEST_SCREENSHOT',
message: result.value,
level: 'INFO',
timestamp: new Date().toISOString()
}
})
: this.listener.onScreenshot([{
test_run_uuid: testMeta.uuid,
timestamp: new Date().toISOString(),
message: result.value,
kind: 'TEST_SCREENSHOT'
}]))
}

if (BrowserstackCLI.getInstance().isRunning()) {
/**
* SDK-6277: in CLI mode the binary captures command/network logs itself; the service only
* forwards the screenshot above. The direct-HTTP per-command logCreated() path 401s here.
*/
return
}

const requestData = this._commands[dataKey]
Expand Down
29 changes: 29 additions & 0 deletions packages/wdio-browserstack-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ export default class BrowserstackService implements Services.ServiceInstance {
if (BrowserstackCLI.getInstance().isRunning()) {
await BrowserstackCLI.getInstance().getAutomationFramework()!.trackEvent(AutomationFrameworkState.CREATE, HookState.POST, { browser: this._browser, hubUrl: this._config.hostname })
this._insightsHandler.setGitConfigPath()
/**
* SDK-6277: register the command/result listeners in the CLI/binary flow too,
* so the user's saveScreenshot()/takeScreenshot() command result is captured and
* forwarded for screenshot-on-failure. Without this the screenshot is dropped in v8.
* (browserCommand routes the screenshot over gRPC in CLI mode — see insights-handler.)
*/
// 'client:beforeCommand' only records the command synchronously — no await needed.
this._browser.on('command', (command) => {
if (shouldProcessEventForTesthub('')) {
this._insightsHandler?.browserCommand(
'client:beforeCommand',
Object.assign(command, { sessionId }),
this._currentTest
)
}
})
this._browser.on('result', (result) => {
if (shouldProcessEventForTesthub('')) {
// 'client:afterCommand' is async in CLI mode (forwards the screenshot to the
// binary over gRPC). Handle the returned promise so a failed upload is logged
// rather than dropped. (o11yClassErrorHandler also routes method errors to
// processError; this .catch makes the call-site handling explicit.)
this._insightsHandler?.browserCommand(
'client:afterCommand',
Object.assign(result, { sessionId }),
this._currentTest
)?.catch((err: unknown) => BStackLogger.debug(`Error forwarding afterCommand in CLI mode: ${err}`))
}
})
PerformanceTester.end(PERFORMANCE_SDK_EVENTS.DRIVER_EVENT.PRE_INITIALIZE)
return
}
Expand Down
Loading