playwright/docs/src/api/class-electronapplication.md

170 lines
5.4 KiB
Markdown
Raw Normal View History

2021-02-01 11:43:26 -08:00
# class: ElectronApplication
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
* langs: js
Electron application representation. You can use [`method: Electron.launch`] to
obtain the application instance. This instance you can control main electron process
as well as work with Electron windows:
```js
const { _electron: electron } = require('playwright');
(async () => {
// Launch Electron app.
const electronApp = await electron.launch({ args: ['main.js'] });
// Evaluation expression in the Electron context.
2021-04-04 14:06:30 -07:00
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();
2021-02-01 11:43:26 -08:00
});
2021-04-04 14:06:30 -07:00
console.log(appPath);
2021-02-01 11:43:26 -08:00
// 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');
2021-04-04 14:06:30 -07:00
// Exit app.
await electronApp.close();
2021-02-01 11:43:26 -08:00
})();
```
## event: ElectronApplication.close
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
This event is issued when the application closes.
## event: ElectronApplication.window
2022-07-05 16:24:50 -08:00
* since: v1.9
- argument: <[Page]>
2021-02-01 11:43:26 -08:00
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.
## async method: ElectronApplication.browserWindow
2022-07-05 16:24:50 -08:00
* since: v1.11
- returns: <[JSHandle]>
Returns the BrowserWindow object that corresponds to the given Playwright page.
### param: ElectronApplication.browserWindow.page
2022-07-05 16:24:50 -08:00
* since: v1.11
- `page` <[Page]>
Page to retrieve the window for.
2021-02-01 11:43:26 -08:00
## async method: ElectronApplication.close
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
Closes Electron application.
## method: ElectronApplication.context
2022-07-05 16:24:50 -08:00
* since: v1.9
- returns: <[BrowserContext]>
2021-02-01 11:43:26 -08:00
This method returns browser context that can be used for setting up context-wide routing, etc.
## async method: ElectronApplication.evaluate
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[Serializable]>
Returns the return value of [`param: expression`].
If the function passed to the [`method: ElectronApplication.evaluate`] returns a [Promise], then
[`method: ElectronApplication.evaluate`] would wait for the promise to resolve and return its value.
If the function passed to the [`method: ElectronApplication.evaluate`] returns a non-[Serializable] value, then
[`method: ElectronApplication.evaluate`] returns `undefined`. Playwright also supports transferring
some additional values that are not serializable by `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`.
### param: ElectronApplication.evaluate.expression = %%-evaluate-expression-%%
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
### param: ElectronApplication.evaluate.arg
2022-07-05 16:24:50 -08:00
* since: v1.9
- `arg` ?<[EvaluationArgument]>
2021-02-01 11:43:26 -08:00
Optional argument to pass to [`param: expression`].
## async method: ElectronApplication.evaluateHandle
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[JSHandle]>
Returns the return value of [`param: expression`] as a [JSHandle].
The only difference between [`method: ElectronApplication.evaluate`] and [`method: ElectronApplication.evaluateHandle`] is that [`method: ElectronApplication.evaluateHandle`] returns [JSHandle].
If the function passed to the [`method: ElectronApplication.evaluateHandle`] returns a [Promise], then
[`method: ElectronApplication.evaluateHandle`] would wait for the promise to resolve and return its value.
### param: ElectronApplication.evaluateHandle.expression = %%-evaluate-expression-%%
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
### param: ElectronApplication.evaluateHandle.arg
2022-07-05 16:24:50 -08:00
* since: v1.9
- `arg` ?<[EvaluationArgument]>
Optional argument to pass to [`param: expression`].
2021-02-01 11:43:26 -08:00
## async method: ElectronApplication.firstWindow
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[Page]>
Convenience method that waits for the first application window to be opened.
**Usage**
2021-02-01 11:43:26 -08:00
```js
const electronApp = await electron.launch({
args: ['main.js']
});
const window = await electronApp.firstWindow();
// ...
```
## method: ElectronApplication.process
2022-07-05 16:24:50 -08:00
* since: v1.21
- returns: <[ChildProcess]>
Returns the main process for this Electron Application.
2021-02-01 11:43:26 -08:00
## async method: ElectronApplication.waitForEvent
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[any]>
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**
2021-02-01 11:43:26 -08:00
```js
const windowPromise = electronApp.waitForEvent('window');
await mainWindow.click('button');
const window = await windowPromise;
2021-02-01 11:43:26 -08:00
```
### param: ElectronApplication.waitForEvent.event = %%-wait-for-event-event-%%
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
### param: ElectronApplication.waitForEvent.optionsOrPredicate
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
* langs: js
- `optionsOrPredicate` ?<[function]|[Object]>
2021-02-01 11:43:26 -08:00
- `predicate` <[function]> receives the event data and resolves to truthy value when the waiting should resolve.
- `timeout` ?<[float]> maximum time to wait for in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to
2021-02-01 11:43:26 -08:00
disable timeout. The default value can be changed by using the [`method: BrowserContext.setDefaultTimeout`].
Either a predicate that receives an event or an options object. Optional.
## method: ElectronApplication.windows
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[Array]<[Page]>>
Convenience method that returns all the opened windows.