Playwright is more than an E2E testing tool — it has a powerful API for browser automation, web scraping, PDF generation, and building custom testing infrastructure.
Browser Automation API
Playwright's core API gives you full browser control:
import { chromium } from "playwright"
const browser = await chromium.launch({ headless: true })
const context = await browser.newContext({
viewport: { width: 1920, height: 1080 },
userAgent: "Custom Agent",
locale: "en-US",
geolocation: { latitude: 40.7128, longitude: -74.0060 },
permissions: ["geolocation"]
})
const page = await context.newPage()
await page.goto("https://example.com")
// Smart waiting — no sleep() needed
await page.getByRole("button", { name: "Submit" }).click()
await page.waitForURL("**/success")
await browser.close()
Network Interception
Intercept and modify network requests:
// Mock API responses
await page.route("**/api/users", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify([{ id: 1, name: "Test User" }])
})
})
// Block resources for faster scraping
await page.route("**/*.{png,jpg,gif,css,woff2}", route => route.abort())
// Modify requests
await page.route("**/api/**", async (route) => {
const headers = {
...route.request().headers(),
"Authorization": "Bearer custom-token"
}
await route.continue({ headers })
})
// Capture responses
page.on("response", async (response) => {
if (response.url().includes("/api/data")) {
const json = await response.json()
console.log("Captured:", json)
}
})
PDF Generation
Generate PDFs from any webpage:
const page = await browser.newPage()
await page.goto("https://example.com/report")
await page.pdf({
path: "report.pdf",
format: "A4",
margin: { top: "1cm", bottom: "1cm", left: "1cm", right: "1cm" },
printBackground: true,
displayHeaderFooter: true,
headerTemplate: "<span style=\"font-size:10px\">Report</span>",
footerTemplate: "<span style=\"font-size:10px\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></span>"
})
Parallel Execution
Run browsers in parallel for speed:
import { chromium } from "playwright"
const urls = ["url1", "url2", "url3", "url4", "url5"]
const browser = await chromium.launch()
const results = await Promise.all(
urls.map(async (url) => {
const context = await browser.newContext()
const page = await context.newPage()
try {
await page.goto(url, { timeout: 10000 })
const title = await page.title()
const text = await page.locator("main").textContent()
return { url, title, text }
} catch (e) {
return { url, error: e.message }
} finally {
await context.close()
}
})
)
await browser.close()
Test API — Built-in Assertions
Playwright Test has powerful web-first assertions:
import { test, expect } from "@playwright/test"
test("user flow", async ({ page }) => {
await page.goto("/login")
await page.getByLabel("Email").fill("user@test.com")
await page.getByLabel("Password").fill("pass123")
await page.getByRole("button", { name: "Sign in" }).click()
// Auto-retrying assertions
await expect(page).toHaveURL("/dashboard")
await expect(page.getByText("Welcome")).toBeVisible()
await expect(page.getByRole("table")).toContainText("Recent")
})
Codegen — Record Tests
npx playwright codegen https://example.com
Opens a browser and records your actions as test code.
Key Takeaways
- Browser API for full automation with smart waiting
- Network interception to mock, block, or modify requests
- PDF generation from any webpage
- Parallel contexts for high-throughput scraping
- Test assertions that auto-retry
- Codegen to record tests visually
- Works with Chromium, Firefox, and WebKit
See Playwright docs for the full API.
Building web scrapers or data pipelines? Check out my Apify actors for ready-made solutions, or email spinov001@gmail.com for custom development.
Top comments (0)