Playwright Extensions and Migration
This chapter introduces the extended features of the Playwright ecosystem, including Library script mode, MCP services, Test Agents, and guides for migrating from other frameworks to Playwright.
Playwright Library Script Mode
Playwright Library is a standalone browser automation library for scenarios outside a test runner.
Installation
npm i playwright
Typical usage
Example
// Screenshot script
import
{
chromium
}
from
'playwright'
;
(
async
(
)
=>
{
const
browser
=
await chromium.
launch
(
{
headless
:
true
}
)
;
const
page
=
await browser.
newPage
(
)
;
await page.
goto
(
'https://www.runoob.com/'
)
;
await page.
screenshot
(
{
path
:
'runoob.png'
,
fullPage
:
true
}
)
;
await browser.
close
(
)
;
console.
log
(
'Screenshot saved to runoob.png'
)
;
}
)
(
)
;
PDF Generation
Example
import
{
chromium
}
from
'playwright'
;
(
async
(
)
=>
{
const
browser
=
await chromium.
launch
(
)
;
const
page
=
await browser.
newPage
(
)
;
await page.
goto
(
'https://www.runoob.com/'
)
;
// Generate PDF (only supported in Chromium)
await page.
(
{
path
:
'runoob.pdf'
,
format
:
'A4'
,
margin
:
{
top
:
'20px'
,
bottom
:
'20px'
}
,
}
)
;
await browser.
close
(
)
;
}
)
(
)
;
Choosing Between Library Mode and Test Runner
| Scenario | Recommend |
|---|---|
| Writing E2E tests | Playwright Test Runner |
| Taking web page screenshots, generating PDFs | Playwright Library |
| Web scraping | Playwright Library |
| One-off automation scripts | Playwright Library |
Introduction to Playwright MCP
The Playwright MCP (Model Context Protocol) server lets AI assistants control the browser directly.
MCP Installation and Configuration
# Add Playwright MCP to Claude Code
claude mcp add playwright npx @playwright/mcp@latest
Configure in VS Code or another MCP client:
Example
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"]
}
}
}
How MCP Works
AI Agents perceive page content through structured accessibility snapshots.
Each piece of accessibility information has a ref ref identifier, and the Agent uses this ref to perform actions such as clicking and typing.
This approach needs no screenshots and is entirely based on structured data interaction, making it fast and highly deterministic.
Playwright Test Agents
Test Agents are experimental AI-driven testing tools that can automatically generate and repair tests.
| Agent | Function | Trigger Method |
|---|---|---|
| Planner | Automatically explore the application and generate a Markdown test plan | Driven by an LLM Agent |
| Generator | Convert the test plan into Playwright test code | Driven by an LLM Agent |
| Healer | Execute the test suite and automatically repair failing tests | Driven by an LLM Agent |
Initialize Test Agents:
# Initialize for VS Code
npx playwright init-agents --loop=vscode
# Initialize for Claude Code
npx playwright init-agents --loop=claude
Component Testing Overview (Experimental)
Playwright supports mounting and testing React/Vue components directly, without a full page environment.
Example
test
(
'Button Component Test'
,
async
(
{
mount
}
)
=>
{
// Mount a React component
const
component
=
await mount
(
<
Button title
=
"Submit"
/>
)
;
// Assert the text displayed by the component
await expect
(
component
)
.
toContainText
(
'Submit'
)
;
// Click the button
await component.
click
(
)
;
}
)
;
Accessibility Testing
Playwright provides assertions for accessible names and descriptions to help verify a page's accessibility.
Example
// Verify an element has the correct accessible name
await expect
(
page.
getByRole
(
'button'
)
)
.
toHaveAccessibleName
(
'Submit Form'
)
;
// Verify an element has an accessible description
await expect
(
page.
getByLabel
(
'Username'
)
)
.
toHaveAccessibleDescription
(
'Please enter 4-16 alphanumeric characters'
)
;
Migrating from Other Frameworks
Migrating from Puppeteer
Playwright and Puppeteer share the capabilities of the Chrome DevTools Protocol, so migration is relatively straightforward.
| Puppeteer | Playwright |
|---|---|
puppeteer.launch() |
chromium.launch() |
page.waitForSelector() |
Not needed (auto-waiting) or page.locator() |
page.$(selector) |
page.locator(selector) |
page.evaluate() |
page.evaluate()(the same) |
page.setRequestInterception() |
page.route() |
Migrating from Cypress
Playwright's Locator concept is similar to Cypress's query chains, but with some key differences:
| Cypress | Playwright |
|---|---|
cy.get('.class') |
page.locator('.class') |
cy.contains('text') |
page.getByText('text') |
cy.get('[data-cy="btn"]') |
page.getByTestId('btn')(with testIdAttribute: 'data-cy') |
cy.intercept() |
page.route() |
cy.wait() |
Not needed (auto-waiting) |
Migrating from Selenium
Selenium uses the WebDriver protocol, while Playwright controls the browser protocol directly, so the API styles differ considerably.
| Selenium | Playwright |
|---|---|
driver.findElement(By.id('x')) |
page.locator('#x') |
driver.get(url) |
page.goto(url) |
element.click() |
locator.click() |
WebDriverWait |
Not needed (built-in auto-waiting) |
new RemoteWebDriver() |
chromium.connect(url) |
Migration advice: do not try to migrate all tests in one step.
First choose one or two tests for core features and rewrite them in Playwright; once they pass, gradually migrate the rest.