new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Playwright · all subjects

webview2

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

WebView2 is a WinForms control for automating web content

WebView2 is a WinForms control that uses Microsoft Edge under the hood to render web content. It is part of the Microsoft Edge browser and is available on Windows 10 and Windows 11. Playwright can automate WebView2 applications and test web content in WebView2.

Connect to WebView2 using BrowserType.connectOverCDP

Playwright connects to WebView2 via the Chrome DevTools Protocol (CDP) using the BrowserType.connectOverCDP method.

Enable WebView2 remote debugging port

A WebView2 control can be instructed to listen to incoming CDP connections by setting the WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS environment variable with --remote-debugging-port=9222, or by calling EnsureCoreWebView2Async with the --remote-debugging-port=9222 argument. Any unused port can be used instead of 9222.

JavaScript example connecting to WebView2

const browser = await playwright.chromium.connectOverCDP('http://localhost:9222'); const context = browser.contexts()[0]; const page = context.pages()[0];

WebView2 parallel tests require separate user data directories

By default, WebView2 uses the same user data directory for all instances, causing interference when running multiple tests in parallel. To avoid this, set the WEBVIEW2_USER_DATA_FOLDER environment variable (or use EnsureCoreWebView2Async) to a different folder for each test to ensure each test runs in its own user data directory.

Wait for WebView2 initialization

The CoreWebView2InitializationCompleted event can be used to ensure the WebView2 control is ready. The event provides an IsSuccess property to check if initialization succeeded.

Debug WebView2 using DevTools

Inside a WebView2 control, right-click to open the context menu and select 'Inspect' to open DevTools, or press F12. The WebView2.CoreWebView2.OpenDevToolsWindow method can also be used to open DevTools programmatically.

WebView2 JavaScript test fixture setup

A custom test fixture can extend Playwright's test to manage WebView2 processes as sub-processes, assign unique user data directories and CDP ports based on worker index, and provide Page instances to tests. The fixture spawns the WebView2 executable with environment variables for CDP port and user data folder, waits for 'WebView2 initialized' output, connects via connectOverCDP, and cleans up the process and directory after the test.

WebView2 Python pytest fixture setup

Python pytest fixtures at session scope manage the WebView2 process by creating a temporary directory, finding a free CDP port, spawning the WebView2 executable with environment variables, waiting for 'WebView2 initialized' output, and connecting via connect_over_cdp. Function-scoped fixtures provide context and page instances from the first context and page of the browser.

Give your agent this brain