Playwright是由微软公司2020年初发布的新一代自动化测试工具,相较于目前最常用的 Selenium,它仅用一个API即可自动执行Chromium、Firefox、WebKit等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。
- 官网地址:https://playwright.dev/docs/why-playwright/
- 官方文档:https://playwright.dev/docs/api/class-electron
- GitHub:https://github.com/microsoft/playwright
引入 playwright
const { _electron } = require('playwright');
示例代码
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 时不需要 Playwright 安装 Web 浏览器,因此您可以在安装 Playwright 时通过设置以下环境变量来省略浏览器下载:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i -D playwright
支持的 Electron 版本有:
v12.2.0+
v13.4.0+
v14+
作者:Jeebiz 创建时间:2023-07-07 20:05
最后编辑:Jeebiz 更新时间:2024-10-29 20:36
最后编辑:Jeebiz 更新时间:2024-10-29 20:36