fix(tests): attach the BiDi trace of each browser to a failed test

The classic WebDriver commands of a failed test are in the junit
output, but the WebDriver BiDi ones are not: the webdriver logger
writes them to the worker log, which only ever receives launcher-level
lines. Script calls and frame switches go over BiDi, and a frame switch
that lands on a browsing context which never answers (seen on iframe
API rejoins in CI, and not reproducible locally in 35 runs) cannot be
understood without the context ids that were requested and what the
browser reported for them.

Keep the last 2000 BiDi commands and results of each browser instance
and attach them to the report next to the screenshot and logs.
This commit is contained in:
damencho authored and Дамян Минков committed 2026-09-28 22:45:27 +03:00
1 parent 7f6845a046
commit bc9728f421
1 file changed
+58
+58
View File
@@ -237,6 +237,51 @@ function completionSentinelPath(cid: string): string {
return path.join(TEST_RESULTS_DIR, `completed-${cid}.json`);
}
/**
* How many WebDriver BiDi commands and results to keep per browser instance for the failure report.
*/
const BIDI_TRACE_SIZE = 2000;
/**
* How much of each traced BiDi command or result to keep. Script calls carry the whole wrapped function
* source, which is noise here; the method, target context and result are what matter.
*/
const BIDI_TRACE_LINE_LENGTH = 800;
/**
* The last BiDi commands and results of each browser instance, attached to the report when a test fails.
*
* The classic WebDriver commands are already in the report (wdio logs them into the junit output), but the
* BiDi ones are not: the webdriver logger writes them to the worker log, which only ever receives
* launcher-level lines (see completionSentinelPath). Script calls and frame switches go over BiDi, and a
* failure like a frame switch landing on a browsing context that never answers cannot be understood
* without seeing which context ids were requested and what the browser reported.
*/
const bidiTraces = new Map<string, string[]>();
/**
* Starts recording the BiDi traffic of a browser instance into its ring buffer.
*
* @param instance - The multiremote instance name.
* @param browser - The browser instance.
*/
function traceBidi(instance: string, browser: WebdriverIO.Browser) {
const trace: string[] = [];
const record = (direction: string, payload: unknown) => {
trace.push(`${new Date().toISOString()} ${direction} ${
JSON.stringify(payload).slice(0, BIDI_TRACE_LINE_LENGTH)}`);
if (trace.length > BIDI_TRACE_SIZE) {
trace.splice(0, trace.length - BIDI_TRACE_SIZE);
}
};
bidiTraces.set(instance, trace);
browser.on('bidiCommand', (command: unknown) => record('>>', command));
browser.on('bidiResult', (result: unknown) => record('<<', result));
}
/**
* Reads a worker's completion sentinel, if it wrote one.
*
@@ -436,6 +481,10 @@ export const config: WebdriverIO.MultiremoteConfig = {
return `${instance}:${browserName}/${browserVersion}`;
}).join(' ')}`);
multiRemoteBrowser.instances.forEach((instance: string) => {
traceBidi(instance, multiRemoteBrowser.getInstance(instance));
});
const globalAny: any = global;
globalAny.ctx = {
@@ -737,6 +786,15 @@ export const config: WebdriverIO.MultiremoteConfig = {
content: pretty(source),
type: 'text/plain' });
}));
const bidiTrace = bidiTraces.get(instance);
if (bidiTrace?.length) {
attachments.push({
filename: `${instance}-bidi-trace`,
content: bidiTrace.join('\n'),
type: 'text/plain' });
}
});
await Promise.allSettled(allProcessing);