Playwright Core Api
# Playwright Core API Reference
This reference guide covers the core classes and APIs of the Playwright Library. Playwright is a powerful framework for Web Testing and Automation, allowing you to control Chromium, Firefox, and WebKit with a single API.
---
## Introduction to Playwright Core Architecture
Playwright's architecture is hierarchical and designed to isolate browser sessions efficiently. Understanding the relationship between these core classes is key to writing robust automation scripts:
1. **Playwright**: The entry point to the library.
2. **BrowserType**: Represents a specific browser engine (e.g., Chromium, Firefox, WebKit) used to launch or connect to a browser.
3. **Browser**: A running browser instance.
4. **BrowserContext**: An isolated, incognito-like session within a Browser instance. Contexts are fast and cheap to create, making them ideal for parallel test execution.
5. **Page**: A single tab or window within a BrowserContext.
---
## Playwright Class
The `Playwright` class is the entry point for launching and interacting with browsers, managing selectors, and handling API requests.
### Properties & Methods
| Property / Method | Return Type | Description |
| :--- | :--- | :--- |
| `playwright.chromium` | `BrowserType` | Returns the Chromium `BrowserType` instance. |
| `playwright.firefox` | `BrowserType` | Returns the Firefox `BrowserType` instance. |
| `playwright.webkit` | `BrowserType` | Returns the WebKit `BrowserType` instance. |
| `playwright.request` | `APIRequest` | Returns the `APIRequest` instance for sending web requests. |
| `playwright.selectors` | `Selectors` | Returns the `Selectors` instance to register custom selector engines. |
---
## BrowserType Class
The `BrowserType` class provides methods to launch a browser instance or connect to an existing one.
### Methods
| Method | Return Type | Description |
| :--- | :--- | :--- |
| `browserType.launch()` | `Promise` | Launches a new browser instance. |
| `browserType.launchPersistentContext(userDataDir, )` | `Promise` | Launches a browser instance with a persistent context (uses a local user data directory). |
| `browserType.connect(wsEndpoint, )` | `Promise` | Connects to an already running browser instance via a WebSocket endpoint. |
| `browserType.connectOverCDP(endpointURL, )` | `Promise` | Connects to an active browser via the Chrome DevTools Protocol (CDP). |
| `browserType.executablePath()` | `string` | Returns the path to the browser's executable file. |
| `browserType.name()` | `string` | Returns the browser name (e.g., `chromium`, `firefox`, `webkit`). |
---
## Browser Class
A `Browser` represents an instance of a running browser (Chromium, Firefox, or WebKit).
### Methods & Properties
| Method / Property | Return Type | Description |
| :--- | :--- | :--- |
| `browser.newContext()` | `Promise` | Creates a new, isolated `BrowserContext`. |
| `browser.newPage()` | `Promise` | Creates a new `Page` inside a new `BrowserContext` (convenience method). |
| `browser.contexts()` | `Array` | Returns an array of all open browser contexts. |
| `browser.close()` | `Promise` | Closes the browser and all of its associated contexts and pages. |
| `browser.browserType()` | `BrowserType` | Returns the `BrowserType` that this browser belongs to. |
| `browser.version()` | `string` | Returns the version of the browser. |
| `browser.isConnected()` | `boolean` | Returns whether the browser is connected to the client. |
---
## BrowserContext Class
`BrowserContext` provides a way to operate multiple independent browser sessions with fully isolated cookies, local storage, and permissions.
### Methods
| Method | Return Type | Description |
| :--- | :--- | :--- |
| `context.newPage()` | `Promise` | Creates a new page (tab) in the context. |
| `context.pages()` | `Array` | Returns all open pages within this context. |
| `context.close()` | `Promise` | Closes the context and all associated pages. |
| `context.cookies()` | `Promise>` | Retrieves cookies for the specified URLs (or all cookies if omitted). |
| `context.addCookies(cookies)` | `Promise` | Adds cookies to the context. |
| `context.clearCookies()` | `Promise` | Clears all cookies from the context. |
| `context.storageState()` | `Promise
YouTip