Electron
Playwright 对 Electron 自动化提供了实验性支持。你可以通过以下方式访问 electron 命名空间:
const { _electron } = require('playwright');
Electron 自动化脚本的示例如下:
const { _electron: electron } = require('playwright');
(async () => {
// 启动 Electron 应用。
const electronApp = await electron.launch({ args: ['main.js'] });
// 在 Electron 上下文中评估表达式。
const appPath = await electronApp.evaluate(async ({ app }) => {
// 这在主 Electron 进程中运行,这里的参数始终是
// 主应用脚本中 require('electron') 的结果。
return app.getAppPath();
});
console.log(appPath);
// 获取应用打开的第一个窗口,必要时等待。
const window = await electronApp.firstWindow();
// 打印标题。
console.log(await window.title());
// 捕获屏幕截图。
await window.screenshot({ path: 'intro.png' });
// 将 Electron 控制台定向到 Node 终端。
window.on('console', console.log);
// 点击按钮。
await window.click('text=Click me');
// 退出应用。
await electronApp.close();
})();
请注意,由于在测试 Electron 时不需要 Playwright 安装 Web 浏览器,因此可以在安装 Playwright 时通过设置以下环境变量来省略浏览器下载:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i -D playwright
支持的 Electron 版本:
- v12.2.0+
- v13.4.0+
- v14+
electron.launch([options])
Added in: v1.9options?<Object>acceptDownloads?<boolean> 是否自动下载所有附件。默认为true,接受所有下载。Added in: v1.12#args?<Array<string>> 启动应用程序时传递给应用程序的附加参数。你通常在这里传递主脚本名称。#bypassCSP?<boolean> 切换绕过页面的内容安全策略。Added in: v1.12#colorScheme?<"light"|"dark"|"no-preference"> 模拟'prefers-colors-scheme'媒体功能,支持的值为'light'、'dark'、'no-preference'。有关更多详细信息,请参阅 page.emulateMedia([options])。默认为'light'。Added in: v1.12#cwd?<string> 启动应用程序的当前工作目录。#env?<Object<string, string>> 指定对 Electron 可见的环境变量。默认为process.env。#executablePath?<string> 启动给定的 Electron 应用程序。如果未指定,则启动此包中安装的默认 Electron 可执行文件,位于node_modules/.bin/electron。#extraHTTPHeaders?<Object<string, string>> 包含要随每个请求发送的附加 HTTP 标头的对象。Added in: v1.12#geolocation?<Object> Added in: v1.12#httpCredentials?<Object> HTTP 身份验证的凭据。Added in: v1.12#ignoreHTTPSErrors?<boolean> 发送网络请求时是否忽略 HTTPS 错误。默认为false。Added in: v1.12#locale?<string> 指定用户区域设置,例如en-GB、de-DE等。区域设置将影响navigator.language值、Accept-Language请求标头值以及数字和日期格式规则。Added in: v1.12#offline?<boolean> 是否模拟网络离线。默认为false。Added in: v1.12#recordHar?<Object> 启用将所有页面的 HAR 记录到recordHar.path文件中。如果未指定,则不记录 HAR。确保等待 browserContext.close() 以保存 HAR。Added in: v1.12#omitContent?<boolean> 控制是否从 HAR 中省略请求内容的可选设置。默认为false。已弃用,请改用content策略。content?<"omit"|"embed"|"attach"> 控制资源内容管理的可选设置。如果指定omit,则不会持久化内容。如果指定attach,则资源将作为单独的文件或 ZIP 存档中的条目持久化。如果指定embed,则内容将按照 HAR 规范内联存储在 HAR 文件中。对于.zip输出文件,默认为attach,对于所有其他文件扩展名,默认为embed。path<string> 文件系统上写入 HAR 文件的路径。如果文件名以.zip结尾,则默认使用content: 'attach'。mode?<"full"|"minimal"> 当设置为minimal时,仅记录从 HAR 路由所需的信息。这会省略大小、时间、页面、cookie、安全性和其他在从 HAR 重放时不使用的 HAR 信息类型。默认为full。urlFilter?<string|RegExp> 用于过滤存储在 HAR 中的请求的 glob 或正则表达式模式。当通过上下文选项提供baseURL并且传递的 URL 是路径时,它会通过new URL()构造函数合并。
recordVideo?<Object> 启用将所有页面的视频录制到recordVideo.dir目录中。如果未指定,则不录制视频。确保等待 browserContext.close() 以保存视频。Added in: v1.12#timeout?<number> 等待应用程序启动的最大时间(以毫秒为单位)。默认为30000(30 秒)。传递0以禁用超时。Added in: v1.15#timezoneId?<string> 更改上下文的时区。有关支持的时区 ID 列表,请参阅 ICU 的 metaZones.txt。Added in: v1.12#
- returns: <Promise<ElectronApplication>>#
使用 executablePath 指定的 electron 应用程序启动。