安装
介绍
🌐 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,请参阅其 文档。
🌐 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.57.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>
使用上述的 App.java 和 pom.xml,按如下方式编译并执行你的新程序:
🌐 With the App.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 会以无头模式运行浏览器。要查看浏览器界面,请将 setHeadless 选项设置为 false。你也可以使用 setSlowMo 来减慢执行速度。更多信息请参阅调试工具的 部分。
🌐 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 启动的浏览器是无头模式,这意味着运行脚本时不会打开浏览器界面。若要更改此设置,可以在启动浏览器时传入 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 或更高版本。
- Windows 11+、Windows Server 2019+ 或 Windows Subsystem for Linux (WSL)。
- macOS 14 Ventura 或更高版本。
- Debian 12、Debian 13、Ubuntu 22.04、Ubuntu 24.04,基于 x86-64 和 arm64 架构。
下一步是什么
🌐 What's next