English · Node.js
Appearance
English · Node.js
Appearance
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();
})();Returns the BrowserWindow object that corresponds to the given Playwright page.
Arguments
Returns
Promise<JSHandle>Closes Electron application.
This method returns browser context that can be used for setting up context-wide routing, etc.
Returns
BrowserContextReturns the return value of pageFunction.
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.
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.
Arguments
pageFunction function() | Electron
Function to be evaluated in the main Electron process.
arg EvaluationArgument (optional)
Optional argument to pass to pageFunction.
Returns
Promise<Serializable>Returns the return value of pageFunction as a JSHandle.
The only difference between electronApplication.evaluate() and electronApplication.evaluateHandle() is that electronApplication.evaluateHandle() returns JSHandle.
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.
Arguments
pageFunction function() | Electron
Function to be evaluated in the main Electron process.
arg EvaluationArgument (optional)
Optional argument to pass to pageFunction.
Returns
Promise<JSHandle>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
timeout float (optional) Added in: v1.33
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
Promise<Page>Returns the main process for this Electron Application.
Returns
ChildProcessWaits 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
Event name, same one typically passed into *.on(event).
optionsOrPredicate function() | Object (optional)
Either a predicate that receives an event or an options object. Optional.
predicate function()
receives the event data and resolves to truthy value when the waiting should resolve.
timeout float (optional)
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
Promise<any>Convenience method that returns all the opened windows.
Returns
Array<Page>This event is issued when the application process has been terminated.
Event data
voidEmitted when JavaScript within the Electron main process calls one of console API methods, e.g. console.log or console.dir.
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
ConsoleMessageThis event is issued for every window that is created and loaded in Electron. It contains a Page that can be used for Playwright automation.
Event data
Page