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 · Test runner API · all subjects

core test api

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

Browser class creation via BrowserType.launch

A Browser is created via the BrowserType.launch method. The Browser object represents a browser instance that can be used to create pages and browser contexts.

Browser.browserType() method returns browser type

The Browser.browserType() method returns the BrowserType object indicating the browser type (chromium, firefox, or webkit) that the browser belongs to. Available since v1.23.

Browser.close() closes browser and all pages

The async Browser.close() method closes the browser and all of its pages if any were opened. If the browser is connected to a browser server, it clears all created contexts and disconnects from the server. The Browser object is disposed and cannot be used anymore after this call. An optional reason parameter (string) can be provided since v1.40 to report the reason for the closure to interrupted operations.

Browser.contexts() returns array of open browser contexts

The Browser.contexts() method returns an array of all open BrowserContext instances. In a newly created browser, this will return an empty array.

Browser.isConnected() indicates browser connection status

The Browser.isConnected() method returns a boolean indicating whether the browser is connected.

Browser.newBrowserCDPSession() creates CDP session

The async Browser.newBrowserCDPSession() method returns a newly created CDPSession. CDP Sessions are only supported on Chromium-based browsers. Available since v1.11.

Browser.newContext() creates isolated browser context

The async Browser.newContext() method creates a new browser context that won't share cookies/cache with other browser contexts. It is best practice to explicitly close the returned context via BrowserContext.close() when done with it and before calling Browser.close() to ensure the context is closed gracefully and artifacts like HARs and videos are fully flushed and saved. The method accepts context options including proxy, clientCertificates, and storageState parameters.

Browser.newPage() creates page in new context

The async Browser.newPage() method creates a new page in a new browser context. Closing this page will close the context as well. This is a convenience API intended only for single-page scenarios and short snippets; production code and testing frameworks should explicitly create Browser.newContext() followed by BrowserContext.newPage() to control exact lifetimes.

Browser.bind() binds browser to named pipe or web socket

The async Browser.bind() method binds the browser to a named pipe or web socket, making it available for other clients to connect to. It requires a title parameter (string) used for identification. It returns an object with an endpoint property (string). Optional parameters include: workspaceDir (string) - working directory associated with the browser server; metadata (Object with string keys and any values, JS only) - additional metadata to associate with the browser server; host (string) - host to bind the web socket server to, when specified a web socket server is created instead of a named pipe; port (int) - port to bind the web socket server to, when specified a web socket server is created instead of a named pipe, use 0 to let the OS pick an available port. Available since v1.59.

Browser.removeAllListeners() removes event listeners

The async Browser.removeAllListeners() method removes all the listeners of the given type, or all registered listeners if no type is given. It allows waiting for async listeners to complete or ignoring subsequent errors from these listeners. Optional parameter type (string) specifies which listener type to remove. Optional behavior parameter controls how listeners are removed. Available since v1.47 for JavaScript.

Browser.startTracing() creates Chromium trace file

The async Browser.startTracing() method starts Chromium Tracing for creating a trace file that can be opened in Chrome DevTools performance panel. This is available for Java, JavaScript, and Python. Optional page parameter specifies a page to include screenshots in the trace. Options include: path (path, required) - path to write the trace file to; screenshots (boolean) - captures screenshots in the trace; categories (Array of strings) - specify custom categories to use instead of default. Available since v1.11.

Browser.stopTracing() returns trace data buffer

The async Browser.stopTracing() method stops Chromium Tracing and returns a Buffer containing the trace data. This is available for Java, JavaScript, and Python. Available since v1.11.

Browser.unbind() unbinds browser server

The async Browser.unbind() method unbinds the browser server that was previously bound with Browser.bind(). Available since v1.59.

Browser.version() returns browser version string

The Browser.version() method returns a string representing the browser version.

Browser.context event emitted on new context creation

The Browser.context event is emitted when a new browser context is created. The event argument is a BrowserContext object. Available since v1.60.

Browser.disconnected event emitted on browser disconnect

The Browser.disconnected event is emitted when the Browser gets disconnected from the browser application. This might happen because the browser application is closed or crashed, or the Browser.close() method was called. The event argument is the Browser object. Available since v1.8.

Browser creation example with Firefox

Example code showing how to create a Browser instance: const { firefox } = require('playwright'); (async () => { const browser = await firefox.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await browser.close(); })();

Browser.contexts() example usage

Example showing how to check open browser contexts: const browser = await pw.webkit.launch(); console.log(browser.contexts().length); // prints `0` const context = await browser.newContext(); console.log(browser.contexts().length); // prints `1`

Browser.newContext() graceful closure example

Example showing best practice for creating and closing browser context: (async () => { const browser = await playwright.firefox.launch(); const context = await browser.newContext(); const page = await context.newPage(); await page.goto('https://example.com'); await context.close(); await browser.close(); })();

Browser.startTracing() example usage

Example showing how to create a Chromium trace file: await browser.startTracing(page, { path: 'trace.json' }); await page.goto('https://www.google.com'); await browser.stopTracing();

Browser.close() best practice for graceful closure

It is best practice to explicitly close any BrowserContext instances created with Browser.newContext() via BrowserContext.close() before calling Browser.close(). This ensures the context is closed gracefully and any artifacts like HARs and videos are fully flushed and saved. This is similar to force-quitting the browser without calling BrowserContext.close() first.

Browser.newPage() not recommended for production

Browser.newPage() should only be used for single-page scenarios and short snippets. Production code and testing frameworks should explicitly create Browser.newContext() followed by BrowserContext.newPage() to control their exact lifetimes.

Give your agent this brain