Playwright Tutorial

Playwright is an open-source project launched by Microsoft in 2020, an open-source framework for web automation and end-to-end testing.

Playwright supports multiple browsers (Chromium, Firefox, and WebKit) and multiple programming languages (JavaScript/TypeScript, Python, Java, and .NET), aiming to provide a reliable, fast, and feature-rich automated testing solution.


What basics do you need to learn Playwright?

Before learning Playwright, mastering these few things is enough:

  • Know a littleJavaScript, especiallyasync/await;
  • be able to useNode.jsandnpmto run scripts;
  • know a bit aboutHTMLand selectors(e.g.,#id、.class);
  • can use the terminalto run commands.

What can Playwright do?

In one sentence: anything a person can click in the browser, Playwright can almost automate.

For example:

  • Click buttons
  • Fill out forms
  • Navigate to different pages
  • Take screenshots and save web content

Playwright Simple Example

The following is a simple Playwright example that can take a screenshot of Baidu's homepage:

Example

// hello.js
const { chromium } = require('playwright');

(async () => {
  // 1. Launch the browser
  const browser = await chromium.launch({ headless: false }); // Set to false to see the browser
  const page = await browser.newPage();

  // 2. Open the webpage
  await page.goto('https://www.baidu.com');

  // 3. Take a screenshot and save
  await page.screenshot({ path: 'baidu.png' });

  // 4. Close the browser
  await browser.close();

  console.log("Screenshot saved to example.png");
})();

Run:

node hello.js

The browser will automatically enter two to-do items and save a screenshot.


Related Links

Other extensions