Playwright, Microsoft's modern browser automation library, officially supports five programming languages with fully-featured, native APIs:
Supported Languages
- JavaScript (Node.js)
- TypeScript (Node.js)
- Python
- C# (.NET)
- Java
Language-Specific Installation & Usage
JavaScript/TypeScript
Installation:
npm install playwright
# Install browsers
npx playwright install
JavaScript Example:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Interact with the page
await page.click('button');
const title = await page.title();
console.log(title);
await browser.close();
})();
TypeScript Example:
import { chromium, Browser, Page } from 'playwright';
(async (): Promise<void> => {
const browser: Browser = await chromium.launch();
const page: Page = await browser.newPage();
await page.goto('https://example.com');
const title: string = await page.title();
console.log(`Page title: ${title}`);
await browser.close();
})();
Python
Installation:
pip install playwright
# Install browsers
playwright install
Async API Example:
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto('https://example.com')
title = await page.title()
print(f"Page title: {title}")
await browser.close()
asyncio.run(main())
Sync API Example:
from playwright.sync_api import sync_playwright
def main():
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
title = page.title()
print(f"Page title: {title}")
browser.close()
if __name__ == "__main__":
main()
C# (.NET)
Installation:
dotnet add package Microsoft.Playwright
# Build to download browsers
dotnet build
# Install browsers
pwsh bin/Debug/net6.0/playwright.ps1 install
Example:
using Microsoft.Playwright;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://example.com");
var title = await page.TitleAsync();
Console.WriteLine($"Page title: {title}");
await browser.CloseAsync();
}
}
Java
Maven Installation (pom.xml):
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.40.0</version>
</dependency>
Gradle Installation:
implementation 'com.microsoft.playwright:playwright:1.40.0'
Example:
import com.microsoft.playwright.*;
public class PlaywrightExample {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://example.com");
String title = page.title();
System.out.println("Page title: " + title);
browser.close();
}
}
}
Key Features Across All Languages
- Cross-browser support: Chromium, Firefox, and WebKit
- Auto-wait functionality: Built-in smart waiting for elements
- Mobile emulation: Device-specific testing capabilities
- Network interception: Mock and modify network requests
- Screenshots and videos: Visual testing and debugging
- Parallel execution: Run tests concurrently for faster execution
Language-Specific Considerations
| Language | Test Framework Integration | Async Support | Community | |----------|---------------------------|---------------|-----------| | JavaScript/TypeScript | Jest, Mocha, Playwright Test | Native async/await | Largest | | Python | pytest, unittest | Both sync and async APIs | Strong | | C# | NUnit, xUnit, MSTest | async/await | Growing | | Java | JUnit, TestNG | CompletableFuture | Established |
Each language binding is maintained by the Playwright team, ensuring feature parity and consistent updates across all supported languages.