🎭 Playwright for Swift
Playwright is a Swift library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
Add swift-playwright to your Package.swift:
dependencies: [
.package(url: "https://github.com/m1guelpf/swift-playwright.git", from: "0.1.0"),
],
targets: [
.target(name: "MyApp", dependencies: [
.product(name: "Playwright", package: "swift-playwright"),
]),
]Then install the Playwright driver and browsers:
# Download the Playwright driver (~40MB, one-time setup)
swift package install-playwright
# Install browsers (requires Node.js)
npx playwright installimport Playwright
let playwright = try await Playwright.launch()
let browser = try await playwright.chromium.launch()
let page = try await browser.newPage()
// Navigate and read content
let response = try await page.goto("https://example.com")
print(try await page.title()) // "Example Domain"
// Find and interact with elements
try await page.locator("input[name=q]").fill("swift playwright")
try await page.getByRole(.button, name: "Search").click()
// Run JavaScript
let result = try await page.evaluate("1 + 1", as: Int.self)
// Capture a screenshot
let png = try await page.screenshot()
try await browser.close()
await playwright.close()swift-playwright works by communicating with the Node.js Playwright server, same as the official Python, Java, and .NET drivers:
┌─────────────────────────────────────────────────┐
│ Playwright (Swift API) │
├─────────────────────────────────────────────────┤
│ JSON-RPC over stdio │
├─────────────────────────────────────────────────┤
│ Playwright Server (Node.js) │
├───────────────┬───────────────┬─────────────────┤
│ Chromium │ Firefox │ WebKit │
└───────────────┴───────────────┴─────────────────┘
This project is licensed under the MIT License - see the LICENSE file for details.