Skip to content

Playwright 速查表

微软出品的浏览器自动化与端到端测试框架。

01

快速上手

基础测试

Playwright 测试使用 { page } 这样的 fixture。

playwright
import { test, expect } from "@playwright/test";

test("home", async ({ page }) => {
  await page.goto("/");
  await expect(page).toHaveTitle("Welcome");
});

交互操作

常用的交互方法。

playwright
await page.fill("input", "hello");
await page.click("button");
await page.waitForSelector(".result");

定位器

Locator API 提供稳健的元素定位。

playwright
const button = page.getByRole("button", { name: "Submit" });
await button.click();

const item = page.getByText("Hello");
await expect(item).toBeVisible();
02

核心特性

多浏览器

开箱即用的跨浏览器测试。

playwright
test.describe.configure({ mode: "serial" });
test("works in all", async ({ page, browserName }) => {
  // runs in chromium, firefox, webkit
});

截图

用于视觉回归测试。

playwright
await page.screenshot({ path: "screenshot.png" });
await expect(page).toHaveScreenshot();

这篇内容对您有帮助吗?