ElectronApplication
Electron 应用表示。你可以使用 electron.launch() 获取应用实例。通过这个实例,你可以控制主 Electron 进程,并操作 Electron 窗口:
🌐 Electron application representation. You can use electron.launch() to obtain the application instance. This instance you can control main electron process as well as work with Electron windows:
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();
})();
方法
🌐 Methods
browserWindow
Added in: v1.11返回与给定 Playwright 页面对应的 BrowserWindow 对象。
🌐 Returns the BrowserWindow object that corresponds to the given Playwright page.
用法
await electronApplication.browserWindow(page);
参数
返回
close
Added in: v1.9关闭 Electron 应用。
🌐 Closes Electron application.
用法
await electronApplication.close();
返回
context
Added in: v1.9此方法返回可用于设置上下文范围路由等的浏览器上下文。
🌐 This method returns browser context that can be used for setting up context-wide routing, etc.
用法
electronApplication.context();
返回
evaluate
Added in: v1.9返回 pageFunction 的返回值。
🌐 Returns the return value of pageFunction.
如果传递给 electronApplication.evaluate() 的函数返回一个 Promise,那么 electronApplication.evaluate() 会等待该 promise 解决并返回其值。
🌐 If the function passed to the electronApplication.evaluate() returns a Promise, then electronApplication.evaluate() would wait for the promise to resolve and return its value.
如果传递给 electronApplication.evaluate() 的函数返回了一个非 Serializable 值,那么 electronApplication.evaluate() 会返回 undefined。Playwright 还支持传递一些 JSON 无法序列化的额外值:-0、NaN、Infinity、-Infinity。
🌐 If the function passed to the electronApplication.evaluate() returns a non-Serializable value, then electronApplication.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.
用法
await electronApplication.evaluate(pageFunction);
await electronApplication.evaluate(pageFunction, arg);
参数
-
pageFunctionfunction | Electron#在主 Electron 进程中要评估的函数。
-
argEvaluationArgument (optional)#可选择的参数,可传递给 pageFunction。
返回
evaluateHandle
Added in: v1.9以 JSHandle 的形式返回 pageFunction 的返回值。
🌐 Returns the return value of pageFunction as a JSHandle.
electronApplication.evaluate() 和 electronApplication.evaluateHandle() 唯一的区别在于 electronApplication.evaluateHandle() 返回 JSHandle。
🌐 The only difference between electronApplication.evaluate() and electronApplication.evaluateHandle() is that electronApplication.evaluateHandle() returns JSHandle.
如果传递给 electronApplication.evaluateHandle() 的函数返回一个 Promise,那么 electronApplication.evaluateHandle() 会等待该 Promise 解析并返回其值。
🌐 If the function passed to the electronApplication.evaluateHandle() returns a Promise, then electronApplication.evaluateHandle() would wait for the promise to resolve and return its value.
用法
await electronApplication.evaluateHandle(pageFunction);
await electronApplication.evaluateHandle(pageFunction, arg);
参数
-
pageFunctionfunction | Electron#在主 Electron 进程中要评估的函数。
-
argEvaluationArgument (optional)#可选择的参数,可传递给 pageFunction。
返回
firstWindow
Added in: v1.9等待第一个应用窗口打开的便捷方法。
🌐 Convenience method that waits for the first application window to be opened.
用法
const electronApp = await electron.launch({
args: ['main.js']
});
const window = await electronApp.firstWindow();
// ...
参数
optionsObject (optional)-
timeoutnumber (optional) Added in: v1.33#等待的最长时间(以毫秒为单位)。默认为
30000(30 秒)。传入0可禁用超时。可以使用 browserContext.setDefaultTimeout() 来更改默认值。
-
返回
process
Added in: v1.21返回此 Electron 应用的主进程。
🌐 Returns the main process for this Electron Application.
用法
electronApplication.process();
返回
waitForEvent
Added in: v1.9等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果应用在事件触发前关闭,将抛出错误。返回事件数据值。
🌐 Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the application is closed before the event is fired. Returns the event data value.
用法
const windowPromise = electronApp.waitForEvent('window');
await mainWindow.click('button');
const window = await windowPromise;
参数
-
事件名称,通常传递到
*.on(event)的那个相同名称。 -
optionsOrPredicatefunction | Object (optional)#-
predicatefunction接收事件数据并在等待应该解决时解析为真值。
-
timeoutnumber (optional)等待的最长时间(以毫秒为单位)。默认为
30000(30 秒)。传入0可禁用超时。可以使用 browserContext.setDefaultTimeout() 来更改默认值。
可以是接收事件的谓词,也可以是一个选项对象。可选。
-
返回
windows
Added in: v1.9返回所有打开的窗口的便捷方法。
🌐 Convenience method that returns all the opened windows.
用法
electronApplication.windows();
返回
事件
🌐 Events
on('close')
Added in: v1.9当应用进程终止时会触发此事件。
🌐 This event is issued when the application process has been terminated.
用法
electronApplication.on('close', data => {});
on('console')
Added in: v1.42当 Electron 主进程中的 JavaScript 调用控制台 API 方法(例如 console.log 或 console.dir)时触发。
🌐 Emitted when JavaScript within the Electron main process calls one of console API methods, e.g. console.log or console.dir.
console.log 传入的参数可以在 ConsoleMessage 事件处理程序的参数中使用。
🌐 The arguments passed into console.log are available on the ConsoleMessage event handler argument.
用法
electronApp.on('console', async msg => {
const values = [];
for (const arg of msg.args())
values.push(await arg.jsonValue());
console.log(...values);
});
await electronApp.evaluate(() => console.log('hello', 5, { foo: 'bar' }));
事件数据
on('window')
Added in: v1.9此事件会在 Electron 中每个被创建并加载的窗口上触发。它包含一个可用于 Playwright 自动化的 Page。
🌐 This event is issued for every window that is created and loaded in Electron. It contains a Page that can be used for Playwright automation.
用法
electronApplication.on('window', data => {});
事件数据