安装
介绍
¥Introduction
Playwright 专为满足端到端测试的需求而创建。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或持续集成 (CI),无头或使用原生移动模拟进行有头测试。
¥Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.
Playwright 作为一组 Maven 模块分发。最简单的使用方法是向项目的 pom.xml
添加一个依赖,如下所述。如果你不熟悉 Maven,请参阅其 documentation。
¥Playwright is distributed as a set of Maven modules. The easiest way to use it is to add one dependency to your project's pom.xml
as described below. If you're not familiar with Maven please refer to its documentation.
用法
¥Usage
首先安装 Playwright 并运行示例文件以查看其运行情况。
¥Get started by installing Playwright and running the example file to see it in action.
- App.java
- pom.xml
package org.example;
import com.microsoft.playwright.*;
public class App {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://playwright.nodejs.cn");
System.out.println(page.title());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>examples</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Playwright Client Examples</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.52.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<!-- References to interface static methods are allowed only at source level 1.8 or above -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用上面的 Example.java 和 pom.xml,按如下方式编译并执行你的新程序:
¥With the Example.java and pom.xml above, compile and execute your new program as follows:
mvn compile exec:java -D exec.mainClass="org.example.App"
运行它会下载 Playwright 软件包并安装 Chromium、Firefox 和 WebKit 的浏览器二进制文件。要修改此行为,请参阅 安装参数。
¥Running it downloads the Playwright package and installs browser binaries for Chromium, Firefox and WebKit. To modify this behavior see installation parameters.
第一个脚本
¥First script
在我们的第一个脚本中,我们将导航到 playwright.dev
并在 WebKit 中截取屏幕截图。
¥In our first script, we will navigate to playwright.dev
and take a screenshot in WebKit.
package org.example;
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.webkit().launch();
Page page = browser.newPage();
page.navigate("https://playwright.nodejs.cn/");
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("example.png")));
}
}
}
默认情况下,Playwright 以无头模式运行浏览器。要查看浏览器 UI,请将 setHeadless 选项设置为 false
。你还可以使用 setSlowMo 来减慢执行速度。在调试工具 section 中了解更多信息。
¥By default, Playwright runs the browsers in headless mode. To see the browser UI, setHeadless option to false
. You can also use setSlowMo to slow down execution. Learn more in the debugging tools section.
playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
运行示例脚本
¥Running the Example script
mvn compile exec:java -D exec.mainClass="org.example.App"
默认情况下,使用 Playwright 启动的浏览器以无头模式运行,这意味着运行脚本时不会打开任何浏览器 UI。要更改此设置,你可以在启动浏览器时传递 new BrowserType.LaunchOptions().setHeadless(false)
。
¥By default browsers launched with Playwright run headless, meaning no browser UI will open up when running the script. To change that you can pass new BrowserType.LaunchOptions().setHeadless(false)
when launching the browser.
系统要求
¥System requirements
-
Java 8 或更高版本。
¥Java 8 or higher.
-
Windows 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 (WSL)。
¥Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).
-
macOS 14 Ventura 或更高版本。
¥macOS 14 Ventura, or later.
-
Debian 12、Ubuntu 22.04、Ubuntu 24.04,基于 x86-64 和 arm64 架构。
¥Debian 12, Ubuntu 22.04, Ubuntu 24.04, on x86-64 and arm64 architecture.
下一步是什么
¥What's next