Skip to main content

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 electronApplication.browserWindow

返回与给定 Playwright 页面对应的 BrowserWindow 对象。

¥Returns the BrowserWindow object that corresponds to the given Playwright page.

用法

¥Usage

await electronApplication.browserWindow(page);

参数

¥Arguments

要检索其窗口的页面。

¥Page to retrieve the window for.

返回

¥Returns


close

Added in: v1.9 electronApplication.close

关闭 Electron 应用。

¥Closes Electron application.

用法

¥Usage

await electronApplication.close();

返回

¥Returns


context

Added in: v1.9 electronApplication.context

此方法返回可用于设置上下文范围路由等的浏览器上下文。

¥This method returns browser context that can be used for setting up context-wide routing, etc.

用法

¥Usage

electronApplication.context();

返回

¥Returns


evaluate

Added in: v1.9 electronApplication.evaluate

返回 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() 的函数返回非 可串行化 值,则 electronApplication.evaluate() 返回 undefined。Playwright 还支持传输一些 JSON 无法序列化的附加值:-0NaNInfinity-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.

用法

¥Usage

await electronApplication.evaluate(pageFunction);
await electronApplication.evaluate(pageFunction, arg);

参数

¥Arguments

在主 Electron 进程中要评估的函数。

¥Function to be evaluated in the main Electron process.

传递给 pageFunction 的可选参数。

¥Optional argument to pass to pageFunction.

返回

¥Returns


evaluateHandle

Added in: v1.9 electronApplication.evaluateHandle

pageFunction 的返回值作为 JSHandle 返回。

¥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.

用法

¥Usage

await electronApplication.evaluateHandle(pageFunction);
await electronApplication.evaluateHandle(pageFunction, arg);

参数

¥Arguments

在主 Electron 进程中要评估的函数。

¥Function to be evaluated in the main Electron process.

传递给 pageFunction 的可选参数。

¥Optional argument to pass to pageFunction.

返回

¥Returns


firstWindow

Added in: v1.9 electronApplication.firstWindow

等待第一个应用窗口打开的便捷方法。

¥Convenience method that waits for the first application window to be opened.

用法

¥Usage

const electronApp = await electron.launch({
args: ['main.js']
});
const window = await electronApp.firstWindow();
// ...

参数

¥Arguments

  • options Object (optional)

    • timeout number (optional) Added in: v1.33#

等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 browserContext.setDefaultTimeout() 更改默认值。

¥Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout().

返回

¥Returns


process

Added in: v1.21 electronApplication.process

返回此 Electron 应用的主进程。

¥Returns the main process for this Electron Application.

用法

¥Usage

electronApplication.process();

返回

¥Returns


waitForEvent

Added in: v1.9 electronApplication.waitForEvent

等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果应用在事件触发之前关闭,则会抛出错误。返回事件数据值。

¥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.

用法

¥Usage

const windowPromise = electronApp.waitForEvent('window');
await mainWindow.click('button');
const window = await windowPromise;

参数

¥Arguments

事件名称,通常传递到 *.on(event) 中。

¥Event name, same one typically passed into *.on(event).

接收事件数据并在等待应该解决时解析为真值。

¥receives the event data and resolves to truthy value when the waiting should resolve.

等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 browserContext.setDefaultTimeout() 更改默认值。

¥maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout().

接收事件的谓词或选项对象。可选的。

¥Either a predicate that receives an event or an options object. Optional.

返回

¥Returns


windows

Added in: v1.9 electronApplication.windows

返回所有打开的窗口的便捷方法。

¥Convenience method that returns all the opened windows.

用法

¥Usage

electronApplication.windows();

返回

¥Returns


事件

¥Events

on('close')

Added in: v1.9 electronApplication.on('close')

当应用进程终止时会触发此事件。

¥This event is issued when the application process has been terminated.

用法

¥Usage

electronApplication.on('close', data => {});

on('console')

Added in: v1.42 electronApplication.on('console')

当 Electron 主进程中的 JavaScript 调用控制台 API 方法之一时触发,例如 console.logconsole.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.

用法

¥Usage

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' }));

事件数据

¥Event data


on('window')

Added in: v1.9 electronApplication.on('window')

对于在 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.

用法

¥Usage

electronApplication.on('window', data => {});

事件数据

¥Event data