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

142 lines
4.2 KiB
Markdown
Raw Normal View History

2021-02-01 11:43:26 -08:00
# class: Electron
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
* langs: js
Playwright has **experimental** support for Electron automation. You can access electron namespace via:
```js
const { _electron } = require('playwright');
```
An example of the Electron automation script would be:
```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
})();
```
**Supported Electron versions are:**
* v12.2.0+
* v13.4.0+
* v14+
**Known issues:**
If you are not able to launch Electron and it will end up in timeouts during launch, try the following:
* Ensure that `nodeCliInspect` ([FuseV1Options.EnableNodeCliInspectArguments](https://www.electronjs.org/docs/latest/tutorial/fuses#nodecliinspect)) fuse is **not** set to `false`.
2021-02-01 11:43:26 -08:00
## async method: Electron.launch
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- returns: <[ElectronApplication]>
Launches electron application specified with the [`option: executablePath`].
### option: Electron.launch.executablePath
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- `executablePath` <[string]>
Launches given Electron application. If not specified, launches the default Electron
executable installed in this package, located at `node_modules/.bin/electron`.
### option: Electron.launch.args
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- `args` <[Array]<[string]>>
Additional arguments to pass to the application when launching. You typically pass the main
script name here.
### option: Electron.launch.cwd
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- `cwd` <[string]>
Current working directory to launch application from.
### option: Electron.launch.env
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-01 11:43:26 -08:00
- `env` <[Object]<[string], [string]>>
Specifies environment variables that will be visible to Electron. Defaults to `process.env`.
### option: Electron.launch.timeout
2022-07-05 16:24:50 -08:00
* since: v1.15
2021-02-01 11:43:26 -08:00
- `timeout` <[float]>
Maximum time in milliseconds to wait for the application to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
### option: Electron.launch.acceptdownloads = %%-context-option-acceptdownloads-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.bypassCSP = %%-context-option-bypasscsp-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.colorScheme = %%-context-option-colorscheme-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.extraHTTPHeaders = %%-context-option-extrahttpheaders-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.geolocation = %%-context-option-geolocation-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.httpcredentials = %%-context-option-httpcredentials-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.locale = %%-context-option-locale-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.offline = %%-context-option-offline-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordhar = %%-context-option-recordhar-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordharpath = %%-context-option-recordhar-path-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordHarOmitContent = %%-context-option-recordhar-omit-content-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordvideo = %%-context-option-recordvideo-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordvideodir = %%-context-option-recordvideo-dir-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.recordvideosize = %%-context-option-recordvideo-size-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
2022-11-21 09:30:32 -08:00
### option: Electron.launch.timezoneId = %%-context-option-timezoneid-%%
2022-07-05 16:24:50 -08:00
* since: v1.12
### option: Electron.launch.tracesDir = %%-browser-option-tracesdir-%%
* since: v1.36