Playwright 是专门为满足端到端测试的需要而创建的。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行本地测试或在 CI 上进行测试,无外设或带本机移动仿真。
一.安装
在JAVA中,使用Playwright最简单方法就是在项目pom.xml中添加Playwright依赖。
1.新建Maven项目
新建Maven项目
新建Maven项目
创建成功之后的项目
2.修改pom.xml
# Maven地址
https://mvnrepository.com/artifact/com.microsoft.playwright/playwright/1.28.1
3.下载Playwright包
下载Playwright包
下载Playwright包
注意:driver-bundle-1.28.1.jar提示以下错误,忽略就好,不影响
二.运行
1.在test/java下新建一个PlaywrightInstall.java文件
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
/**
* @author 作者:测试工程师成长之路
* @version 创建时间:2022/12/11
* 类说明:playwright demo
*/
public class PlaywrightInstall {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
// Can be "msedge", "chrome-beta", "msedge-beta", "msedge-dev", etc.
Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
Page page = browser.newPage();
page.navigate("https://www.baidu.com");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.执行Playwright.java,会自动下载相关浏览器
Playwright会将Chromium、WebKit 和Firefox 浏览器下载到操作系统特定的缓存文件夹中。
Windows:%USERPROFILE%\AppData\Local\ms-playwright
MacOS:~/Library/Caches/ms-playwright
Linux:~/.cache/ms-playwright
3.运行结果
三.关键代码详解
# 使用chromium浏览器
BrowserType chromium = playwright.chromium();
# 浏览器配置,设置以GUI模式启动Chrome浏览器
Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
# 创建page
Page page = browser.newPage();
# 浏览器打开百度
page.navigate("https://www.baidu.com");
四.其它说明
对于try()中创建对象的说明,参考教程:https://urlify.cn/amMfYf
资源分享
下方这份完整的软件测试视频学习教程已经上传CSDN官方认证的二维码,朋友们如果需要可以自行免费领取 【保证100%免费】