Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | import { chromium, test } from '@playwright/test';
import type { BrowserContext, ConsoleMessage, Page } from '@playwright/test';
import { appendFileSync } from 'node:fs';
import { mkdir, rm, readFile, access } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
// CDP event types for Runtime domain
// These are simplified versions of the Chrome DevTools Protocol types
type CdpRemoteObject = {
type: string;
value?: unknown;
description?: string;
};
type CdpExecutionContextCreatedEvent = {
context: {
id: number;
origin: string;
auxData?: { frameId?: string };
};
};
type CdpConsoleAPICalledEvent = {
type: string;
args: CdpRemoteObject[];
executionContextId: number;
};
export const sessionPath = path.resolve(os.tmpdir(), 'ocap-test');
// Run ID is generated once per Playwright invocation (per worker process)
// This allows associating all test log files from the same run
const runId = new Date()
.toISOString()
.slice(0, -5) // Remove ".123Z"
.replace(/[:.]/gu, '-'); // Make filename-safe
type Options = {
contextId?: string | undefined;
extensionPath: string;
onPageLoad?: (popupPage: Page) => Promise<void> | undefined;
};
/**
* Creates an extension context, extension ID, and popup page.
*
* @param options - Options for the extension
* @param options.contextId - Optional context identifier to create separate browser contexts.
* If not provided, uses the TEST_WORKER_INDEX environment variable.
* @param options.extensionPath - The path to the extension dist folder.
* @param options.onPageLoad - Optional callback to run after the extension is loaded. Useful for
* e.g. waiting for components to be visible before proceeding with a test.
* @returns The extension context, extension ID, popup page, log file path, and cleanup function
*/
export const makeLoadExtension = async ({
contextId,
extensionPath,
onPageLoad = async () => Promise.resolve(),
}: Options): Promise<{
browserContext: BrowserContext;
extensionId: string;
popupPage: Page;
logFilePath: string;
attachLogs: () => Promise<void>;
}> => {
const workerIndex = process.env.TEST_WORKER_INDEX ?? '0';
// Use provided contextId or fall back to workerIndex for separate user data dirs
const effectiveContextId = contextId ?? workerIndex;
const userDataDir = path.join(sessionPath, effectiveContextId);
await rm(userDataDir, { recursive: true, force: true });
// Set up log file for capturing console output from extension contexts
const packageRoot = path.dirname(extensionPath); // extensionPath is <package>/dist
const logsDir = path.join(packageRoot, 'logs');
await mkdir(logsDir, { recursive: true });
const testTitle = test
.info()
.titlePath.join('-')
.replace(/[^a-zA-Z0-9-]/gu, '_'); // Make filename-safe
const logFilePath = path.join(logsDir, `${runId}-${testTitle}.log`);
/**
* Attaches the log file to test results. Call this at the end of your test
* to include console logs in the Playwright HTML report.
*/
const attachLogs = async (): Promise<void> => {
try {
await access(logFilePath);
const content = await readFile(logFilePath, 'utf-8');
await test.info().attach('console-logs', {
body: content,
contentType: 'text/plain',
});
} catch {
// File doesn't exist, nothing to attach
}
};
/**
* Write a raw log entry (for CDP events where we don't have a ConsoleMessage).
*
* @param source - The source identifier for the log.
* @param type - The console method type.
* @param text - The log message text.
*/
const writeRawLog = (source: string, type: string, text: string): void => {
const logTimestamp = new Date().toISOString().slice(0, -5);
// eslint-disable-next-line n/no-sync
appendFileSync(
logFilePath,
`[${logTimestamp}] [${source}] [${type}] ${text}\n`,
);
};
const writeLog = (source: string, consoleMessage: ConsoleMessage): void => {
writeRawLog(source, consoleMessage.type(), consoleMessage.text());
};
const browserArgs = [
`--disable-features=ExtensionDisableUnsupportedDeveloper`,
`--disable-extensions-except=${extensionPath}`,
`--load-extension=${extensionPath}`,
'--lang=en-US',
];
const isHeadless = process.env.npm_lifecycle_event === 'test:e2e';
if (isHeadless) {
browserArgs.push(`--headless=new`);
}
// Launch the browser with the extension
const browserContext = await chromium.launchPersistentContext(userDataDir, {
headless: false,
args: browserArgs,
});
// Capture background service worker console logs
browserContext.on('serviceworker', (worker) => {
worker.on('console', (consoleMessage) =>
writeLog('background', consoleMessage),
);
});
// Capture console logs from extension pages (offscreen document, etc.)
// Note: Pages may start at about:blank, so we attach the listener and check URL in the handler
browserContext.on('page', (page) => {
page.on('console', (consoleMessage) => {
if (page.url().includes('offscreen.html')) {
writeLog('offscreen', consoleMessage);
}
});
// Capture Web Worker console logs (e.g., kernel worker)
page.on('worker', (worker) => {
worker.on('console', (consoleMessage) => {
writeLog('kernel-worker', consoleMessage);
});
});
// Set up CDP to capture iframe console logs (vat iframes)
// We need to do this because Playwright doesn't have frame.on('console')
setupCdpForIframeConsoleLogs(page, writeRawLog).catch(
// eslint-disable-next-line no-console
(error) => console.warn('Failed to set up CDP for iframe logs:', error),
);
});
// Wait for the extension to be loaded
await new Promise((resolve) => setTimeout(resolve, 1000));
const chromeExtensionURLIdMatcher = /^chrome-extension:\/\/([^/]+)/u;
const serviceWorkers = browserContext.serviceWorkers();
const extensionId = serviceWorkers[0]
?.url()
.match(chromeExtensionURLIdMatcher)?.[1];
if (!extensionId) {
throw new Error('Extension ID not found');
}
const popupPage = await browserContext.newPage();
popupPage.on('console', (consoleMessage) =>
writeLog('popup', consoleMessage),
);
await popupPage.goto(`chrome-extension://${extensionId}/popup.html`);
await onPageLoad(popupPage);
return { browserContext, extensionId, popupPage, logFilePath, attachLogs };
};
/**
* Sets up Chrome DevTools Protocol (CDP) to capture console logs from iframes.
* Playwright doesn't provide `frame.on('console')`, so we use CDP's Runtime domain
* to listen for console API calls from all execution contexts including iframes.
*
* @param page - The Playwright page to set up CDP for.
* @param writeRawLog - Function to write raw log entries.
*/
async function setupCdpForIframeConsoleLogs(
page: Page,
writeRawLog: (source: string, type: string, text: string) => void,
): Promise<void> {
// Pages may start at about:blank, so wait for navigation to complete
// Only set up CDP for pages that might have iframes (offscreen document)
try {
await page.waitForURL('**/offscreen.html', { timeout: 5000 });
} catch {
// Page didn't navigate to offscreen.html, skip CDP setup
return;
}
const cdpSession = await page.context().newCDPSession(page);
// Enable Runtime domain to receive console events
await cdpSession.send('Runtime.enable');
// Track execution contexts to identify iframe sources
const executionContexts = new Map<number, string>();
// Track the main frame's execution context ID
// The first context created with the extension's origin is the main frame
let mainFrameContextId: number | undefined;
// Listen for new execution contexts (iframes get their own context)
cdpSession.on(
'Runtime.executionContextCreated',
(event: CdpExecutionContextCreatedEvent) => {
const { id, origin, auxData } = event.context;
const frameId = auxData?.frameId;
// Track the main frame's context ID when we see it
// The frameId from CDP won't match Playwright's _guid directly,
// but the main frame context is typically the first one with the page's origin
if (
mainFrameContextId === undefined &&
origin.includes('chrome-extension')
) {
mainFrameContextId = id;
}
// Build source identifier for iframes (contexts with a frameId)
const source = frameId ? `iframe-${frameId.slice(0, 8)}` : `ctx-${id}`;
executionContexts.set(id, frameId ? source : origin);
},
);
// Listen for console API calls from all contexts (including iframes)
cdpSession.on(
'Runtime.consoleAPICalled',
(event: CdpConsoleAPICalledEvent) => {
const { type, args, executionContextId } = event;
// Skip main frame logs - they're already captured via page.on('console')
if (executionContextId === mainFrameContextId) {
return;
}
// Format args into a readable string
const text = args
.map((arg) => {
if (arg.value !== undefined) {
return typeof arg.value === 'string'
? arg.value
: JSON.stringify(arg.value);
}
return arg.description ?? arg.type;
})
.join(' ');
// Determine the source based on execution context
const contextSource = executionContexts.get(executionContextId);
const source = contextSource?.startsWith('iframe')
? contextSource
: `iframe-ctx-${executionContextId}`;
writeRawLog(source, type, text);
},
);
}
|