Java版 Playwright 的配置
在自动化测试领域,Playwright 和 Selenium 都是备受推崇的工具,它们各自具有独特的优点和适用场景。
Playwright 是由微软开发的 Web UI 自动化测试工具(也是一款爬虫利器),支持Node.js、Python、C#、Java语言,用于对新式 Web 应用进行可靠的端到端测试,它适用于所有新式浏览器引擎(Chromium, WebKit, Firefox),并支持移动覆盖范围的设备仿真。
本文将详细介绍如何在 Ubuntu 24.04 的系统上配置 Playwright,并验证安装结果。
注1:不要使用 CentOS 系统,因为[第6步]安装浏览器驱动的脚本里使用的是 apt-get
- 第1步,安装 Java 22.0.2
注2:若java版本过低则[第8步]报错
[ERROR] Source option 5 is no longer supported. Use 6 or later.
curl -sL http://iso.sqlfans.cn/jdk/install_openjdk_2202.sh | bash
source /etc/profile
java -version
- 第2步,安装 Maven 3.8.8
cd /opt/
wget -c http://iso.sqlfans.cn/linux/apache-maven-3.8.8-bin.tar.gz
tar -zxvf apache-maven-3.8.8-bin.tar.gz
mv apache-maven-3.8.8 /usr/local/maven3
ln -s /usr/local/maven3/bin/mvn /usr/sbin/mvn
mvn -version
- 第3步,配置 Playwright Maven,预计耗时 00:27:03(注:aws虚机耗时8秒)
cd /opt/
mvn archetype:generate -DgroupId=com.example -DartifactId=playwright-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 第4步,编辑 pom.xml 文件,添加 Playwright 的 Maven 依赖(只添加2段注释内的部分,其他的不动)
注3:通过指定 maven 使用6或更高版本,以解决[第8步]可能的报错
[ERROR] Source option 5 is no longer supported. Use 6 or later.
cp /opt/playwright-demo/pom.xml /opt/playwright-demo/pom.xml.$(date +%Y%m%d%H%M%S)
cat > /opt/playwright-demo/pom.xml <<EOF
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>playwright-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<!-- Add the following lines -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
<!-- Add the above lines -->
<name>playwright-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Add the following lines -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.48.0</version>
</dependency>
<!-- Add the above lines -->
</dependencies>
</project>
EOF
- 第5步,执行
mvn install
更新依赖,预计耗时 00:53:23(注:aws虚机耗时11秒)
cd /opt/playwright-demo
mvn install
- 第6步,安装Playwright专属的浏览器(Chromium、Firefox、WebKit)驱动程序来支持多浏览器测试
cd /opt/playwright-demo
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install-deps"
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
ll ~/.cache/ms-playwright
- 第7步,创建一个简单的测试类,验证 Playwright 是否正常运行
cat > /opt/playwright-demo/src/main/java/com/example/Main.java <<EOF
package com.example;
import com.microsoft.playwright.*;
public class Main {
public static void main(String[] args) {
try {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
Page page = browser.newPage();
page.navigate("https://www.baidu.com");
System.out.println("PageTitle: " + page.title());
browser.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
EOF
- 第8步,执行如下命令进行测试验证
mvn compile exec:java -Dexec.mainClass="com.example.Main"
示例应返回 PageTitle: 百度一下,你就知道