Electron
Playwright 具有 实验性 的 Electron 自动化支持。您可以通过以下方式访问 electron 命名空间:
const { _electron } = require('playwright');
Electron 自动化脚本的示例可以是
const { _electron: electron } = require('playwright');
(async () => {
// Launch Electron app.
const electronApp = await electron.launch({ args: ['main.js'] });
// Evaluation expression in the Electron context.
const appPath = await electronApp.evaluate(async ({ app }) => {
// This runs in the main Electron process, parameter here is always
// the result of the require('electron') in the main app script.
return app.getAppPath();
});
console.log(appPath);
// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
// Print the title.
console.log(await window.title());
// Capture a screenshot.
await window.screenshot({ path: 'intro.png' });
// Direct Electron console to Node terminal.
window.on('console', console.log);
// Click button.
await window.click('text=Click me');
// Exit app.
await electronApp.close();
})();
支持的 Electron 版本是
- v12.2.0+
- v13.4.0+
- v14+
已知问题
如果您无法启动 Electron 并且在启动期间最终超时,请尝试以下操作
- 确保
nodeCliInspect
(FuseV1Options.EnableNodeCliInspectArguments) fuse 未 设置为false
。
方法
launch
添加于: v1.9启动使用 executablePath 指定的 electron 应用程序。
用法
await electron.launch();
await electron.launch(options);
参数
options
Object (可选)-
acceptDownloads
boolean (可选)添加于: v1.12#是否自动下载所有附件。默认为
true
,即接受所有下载。 -
启动应用程序时要传递的其他参数。您通常在此处传递主脚本名称。
-
bypassCSP
boolean (可选)添加于: v1.12#切换绕过页面的内容安全策略 (Content-Security-Policy)。默认为
false
。 -
colorScheme
null | "light" | "dark" | "no-preference" (可选)添加于: v1.12#模拟 prefers-colors-scheme 媒体功能,支持的值为
'light'
和'dark'
。有关更多详细信息,请参阅 page.emulateMedia()。传递null
会将模拟重置为系统默认值。默认为'light'
。 -
从中启动应用程序的当前工作目录。
-
env
Object<string, string> (可选)#指定 Electron 可见的环境变量。默认为
process.env
。 -
启动给定的 Electron 应用程序。如果未指定,则启动此软件包中安装的默认 Electron 可执行文件,位于
node_modules/.bin/electron
。 -
extraHTTPHeaders
Object<string, string> (可选)添加于: v1.12#一个包含要随每个请求发送的附加 HTTP 标头的对象。默认为无。
-
httpCredentials
Object (可选)添加于: v1.12#-
username
string -
password
string -
origin
string (可选)限制在特定 origin 上发送 http 凭据 (scheme://host:port).
-
send
"unauthorized" | "always" (可选)此选项仅适用于从相应的 APIRequestContext 发送的请求,并且不影响从浏览器发送的请求。
'always'
- 带有基本身份验证凭据的Authorization
标头将随每个 API 请求一起发送。'unauthorized
- 仅当收到带有WWW-Authenticate
标头的 401(未授权)响应时,才会发送凭据。默认为'unauthorized'
。
用于 HTTP 身份验证 的凭据。如果未指定 origin,则在收到未授权响应后,用户名和密码将发送到任何服务器。
-
-
ignoreHTTPSErrors
boolean (可选)添加于: v1.12#是否在发送网络请求时忽略 HTTPS 错误。默认为
false
。 -
指定用户区域设置,例如
en-GB
、de-DE
等。区域设置将影响navigator.language
值、Accept-Language
请求标头值以及数字和日期格式规则。默认为系统默认区域设置。在我们的 模拟指南 中了解有关模拟的更多信息。 -
offline
boolean (可选)添加于: v1.12#是否模拟网络离线。默认为
false
。在 网络模拟 中了解更多信息。 -
recordHar
Object (可选)添加于: 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 路由所需的信息。这省略了在从 HAR 重放时未使用的尺寸、计时、页面、cookie、安全性和其他类型的 HAR 信息。默认为full
。 -
urlFilter
string | RegExp (可选)用于过滤存储在 HAR 中的请求的 glob 或正则表达式模式。当通过上下文选项提供了 baseURL 并且传递的 URL 是路径时,它将通过
new URL()
构造函数合并。默认为无。
启用将所有页面的 HAR 记录到
recordHar.path
文件中。如果未指定,则不记录 HAR。确保等待 browserContext.close() 以保存 HAR。 -
-
recordVideo
Object (可选)添加于: v1.12#-
dir
string用于存放视频的目录路径。
-
size
Object (可选)录制视频的可选尺寸。如果未指定,则大小将等于缩小以适应 800x800 的
viewport
。如果未显式配置viewport
,则视频大小默认为 800x450。如有必要,每个页面的实际图片将被缩小以适应指定的大小。
启用将所有页面的视频录制到
recordVideo.dir
目录中。如果未指定,则不录制视频。确保等待 browserContext.close() 以保存视频。 -
-
timeout
number (可选)添加于: v1.15#等待应用程序启动的最长时间(以毫秒为单位)。默认为
30000
(30 秒)。传递0
以禁用超时。 -
timezoneId
string (可选)添加于: v1.12#更改上下文的时区。有关支持的时区 ID 列表,请参阅 ICU 的 metaZones.txt。默认为系统时区。
-
tracesDir
string (可选)添加于: v1.36#如果指定,跟踪将保存到此目录中。
-
返回值