startJSCoverage method options
startJSCoverage accepts two options: resetOnNavigation (boolean, defaults to true) which controls whether coverage resets on every navigation, and reportAnonymousScripts (boolean, defaults to false) which controls whether anonymous scripts generated by the page should be reported. Note that passing resetOnNavigation as false does not guarantee that coverage persists through navigations due to browser architecture limitations.
Coverage API browser support
Coverage APIs are only supported on Chromium-based browsers.
Anonymous scripts in coverage
Anonymous scripts are scripts that don't have an associated URL and are dynamically created on the page using eval or new Function. If reportAnonymousScripts is set to true, anonymous scripts will have __playwright_evaluation_script__ as their URL.
startCSSCoverage method options
startCSSCoverage accepts the resetOnNavigation option (boolean, defaults to true) which controls whether coverage resets on every navigation.
stopJSCoverage return format
stopJSCoverage returns an array of objects, each containing: url (string, the script URL), scriptId (string), source (optional string with script content if applicable), and functions (array of V8-specific coverage format objects). Each function object contains functionName (string), isBlockCoverage (boolean), and ranges (array of objects with count (int), startOffset (int), and endOffset (int)).
stopCSSCoverage return format
stopCSSCoverage returns an array of coverage report objects for all stylesheets. Each object contains: url (string, the stylesheet URL), text (optional string with stylesheet content if available), and ranges (array of objects specifying which parts of the stylesheet were used, sorted and non-overlapping). Each range object contains start (int, inclusive offset in text) and end (int, exclusive offset in text).
CSS coverage limitations
CSS Coverage does not include dynamically injected style tags without sourceURLs.
JavaScript coverage limitations
JavaScript Coverage does not include anonymous scripts by default. However, scripts with sourceURLs are reported.
JavaScript coverage with v8toIstanbul example
const { chromium } = require('playwright');
const v8toIstanbul = require('v8-to-istanbul');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.coverage.startJSCoverage();
await page.goto('https://chromium.org');
const coverage = await page.coverage.stopJSCoverage();
for (const entry of coverage) {
const converter = v8toIstanbul('', 0, { source: entry.source });
await converter.load();
converter.applyCoverage(entry.functions);
console.log(JSON.stringify(converter.toIstanbul()));
}
await browser.close();
})();
This example demonstrates how to use JavaScript coverage to produce an Istanbul report for page load.
Dialog objects dispatched by page.dialog event
Dialog objects are dispatched by page via the page.dialog event. Dialogs are dismissed automatically unless there is a Page.dialog listener present.
Handle dialog example in JavaScript
Example of handling dialogs in JavaScript: page.on('dialog', async dialog => { console.log(dialog.message()); await dialog.dismiss(); }); await page.evaluate(() => alert('1'));
Dialog listener must accept or dismiss dialog
When a Page.dialog listener is present, it must either call Dialog.accept() or Dialog.dismiss() on the dialog. If neither is called, the page will freeze waiting for the dialog, and actions like click will never finish.
Dialog.accept() method with optional prompt text
Dialog.accept() is an async method that returns when the dialog has been accepted. It accepts an optional promptText parameter of type string. The promptText parameter only has an effect if the dialog's type is 'prompt', otherwise it has no effect.
Dialog.dismiss() method
Dialog.dismiss() is an async method that returns when the dialog has been dismissed.
Dialog.message() returns dialog text
Dialog.message() is a synchronous method that returns the message displayed in the dialog as a string.
Dialog.defaultValue() returns prompt default
Dialog.defaultValue() is a synchronous method that returns the default prompt value if the dialog is of type prompt, otherwise returns an empty string.
Dialog.type() returns dialog type
Dialog.type() is a synchronous method that returns the dialog's type as a string. Possible values are 'alert', 'beforeunload', 'confirm', or 'prompt'.
Dialog.page() returns initiating page
Dialog.page() is a synchronous method that returns the page that initiated the dialog, or null if not available. Available since v1.34.
Detect print dialog with dialog event listener
A print dialog can be detected by listening to the Page.dialog event and checking if the dialog type is 'beforeunload', which is one of the dialog types that can be triggered. However, print dialogs specifically are browser-native and may require special handling.