每个版本的Playwright都需要特定版本的浏览器二进制文件来操作。你需要使用Playwright CLI来安装这些浏览器。Playwright在每次发布时都会更新其支持的浏览器版本,以便最新的Playwright随时支持最新的浏览器。这意味着每次更新Playwright时,可能需要重新运行install CLI命令。
安装浏览器
Playwright可以安装支持的浏览器。运行不带参数的命令将安装默认浏览器。前提是你得先有个pom.xml,不然可安装不成功哟,只会安装依赖包,pom.xml内容可参考我之前写的另一篇文章
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
你也可以通过提供参数来安装特定的浏览器,如webkit
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install webkit"
当然你也可以直接使用playwright install一键安装所有浏览器。
查看所有支持的浏览器
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --help"
安装浏览器依赖
系统依赖项可以自动安装。这对于CI环境很有用。
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-deps chromium"
也可以将install-deps与install组合使用,以便使用单个命令安装浏览器和操作系统依赖项。
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps chromium"
配置浏览器
Playwright可以在Chromium、WebKit和Firefox浏览器以及Google Chrome和Microsoft Edge等品牌浏览器上运行测试。它还可以在模拟平板电脑和移动的设备上运行。我们来看下如何在不同的浏览器上运行测试。在多个浏览器上运行测试,并根据环境变量BROWSER进行判断。
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = null;
String browserName = System.getenv("BROWSER");
if (browserName.equals("chromium")) {
browser = playwright.chromium().launch();
} else if (browserName.equals("firefox")) {
browser = playwright.firefox().launch();
} else if (browserName.equals("webkit")) {
browser = playwright.webkit().launch();
}
Page page = browser.newPage();
// ...
}
}
}
1.Chromium
对于Google Chrome,Microsoft Edge和其他基于Chromium的浏览器,默认情况下,Playwright使用开源Chromium构建。
2.Google Chrome & Microsoft Edge
虽然Playwright可以下载和使用最新的Chromium版本,但它可以针对机器上可用的品牌Google Chrome和Microsoft Edge浏览器运行。可用的浏览器版本如chrome, msedge, chrome-beta, msedge-beta or msedge-dev。
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
// Channel can be "chrome", "msedge", "chrome-beta", "msedge-beta" or "msedge-dev".
// 我们设置要打开的浏览器为 chrome
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome"));
Page page = browser.newPage();
// ...
}
}
}
3.安装Google Chrome和Microsoft Edge
如果你的电脑上没有安装Google Chrome或Microsoft Edge,你可以使用Playwright命令行工具安装它们
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install msedge"
3.Firefox
Playwright的Firefox版本与最近的Firefox稳定版本相匹配。
4.WebKit
Playwright的WebKit版本与最近的WebKit主干版本相匹配。
5.通过代理安装浏览器
默认情况下,Playwright从微软的CDN下载浏览器。有时,公司维护一个内部代理,阻止直接访问公共资源。在这种情况下,可以将Playwright配置为通过代理服务器下载浏览器。
# bash
HTTPS_PROXY=https://192.0.2.1 mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# PowerShell
$Env:HTTPS_PROXY="https://192.0.2.1"
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# batch
set HTTPS_PROXY=https://192.0.2.1
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
如果代理的请求被自定义的不受信任的证书颁发机构(CA)拦截,并且在下载浏览器时报Error: self signed certificate in certificate chain,则在安装浏览器之前,必须通过NODE_EXTRA_CA_CERTS环境变量设置自定义根证书。
# bash
export NODE_EXTRA_CA_CERTS="/path/to/cert.pem"
# PowerShell
$Env:NODE_EXTRA_CA_CERTS="C:\certs\root.crt"
# batch
set NODE_EXTRA_CA_CERTS="C:\certs\root.crt"
如果你的网络连接到Playwright浏览器存档的速度很慢,你可以使用PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT环境变量以毫秒为单位增加连接超时。
# bash
PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT=120000 mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# PowerShell
$Env:PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT="120000"
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# batch
set PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT=120000
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
注意:如何从自定义位置下载浏览器?如公司服务器,在这种情况下,Playwright可以配置为使用PLAYWRIGHT_DOWNLOAD_HOST环境变量从自定义位置下载。
# bash
PLAYWRIGHT_DOWNLOAD_HOST=192.0.2.1 mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# PowerShell
$Env:PLAYWRIGHT_DOWNLOAD_HOST="192.0.2.1"
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# batch
set PLAYWRIGHT_DOWNLOAD_HOST=192.0.2.1
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
6.浏览器默认安装位置
Playwright将Chromium、WebKit和Firefox浏览器下载到操作系统特定的缓存文件夹中。
Windows:%USERPROFILE%\AppData\Local\ms-playwright
MacOS:~/Library/Caches/ms-playwright
Linux:~/.cache/ms-playwright
当然,你也可以指定安装路径,记得先配置环境变量
# bash
PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# PowerShell
$Env:PLAYWRIGHT_BROWSERS_PATH="$Env:USERPROFILE\pw-browsers"
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
# batch
set PLAYWRIGHT_BROWSERS_PATH=%USERPROFILE%\pw-browsers
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"
在这种情况下,在运行测试脚本时,需要指定浏览器路径
# bash
PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers mvn test
# PowerShell
$Env:PLAYWRIGHT_BROWSERS_PATH="$Env:USERPROFILE\pw-browsers"
mvn test
# batch
set PLAYWRIGHT_BROWSERS_PATH=%USERPROFILE%\pw-browsers
mvn test
有些情况下,我们希望在执行脚本时,不需要重新下载浏览器,那该如何配置呢?
# bash
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 mvn test
# PowerShell
$Env:PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
mvn test
# batch
set PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
mvn test
感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取