Electron
Playwright has experimental support for Electron automation. You can access electron namespace via:
const { _electron } = require('playwright');
An example of the Electron automation script would be:
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();
})();
Note that since you don't need Playwright to install web browsers when testing Electron, you can omit browser download via setting the following environment variable when installing Playwright:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i -D playwright
Supported Electron versions are:
- v12.2.0+
- v13.4.0+
- v14+
electron.launch([options])
Added in: v1.9options?
<Object>acceptDownloads?
<boolean> Whether to automatically download all the attachments. Defaults totrue
where all the downloads are accepted. Added in: v1.12#args?
<Array<string>> Additional arguments to pass to the application when launching. You typically pass the main script name here.#bypassCSP?
<boolean> Toggles bypassing page's Content-Security-Policy. Added in: v1.12#colorScheme?
<"light"|"dark"|"no-preference"> Emulates'prefers-colors-scheme'
media feature, supported values are'light'
,'dark'
,'no-preference'
. See page.emulateMedia([options]) for more details. Defaults to'light'
. Added in: v1.12#cwd?
<string> Current working directory to launch application from.#env?
<Object<string, string>> Specifies environment variables that will be visible to Electron. Defaults toprocess.env
.#executablePath?
<string> Launches given Electron application. If not specified, launches the default Electron executable installed in this package, located atnode_modules/.bin/electron
.#extraHTTPHeaders?
<Object<string, string>> An object containing additional HTTP headers to be sent with every request. Added in: v1.12#geolocation?
<Object> Added in: v1.12#httpCredentials?
<Object> Credentials for HTTP authentication. Added in: v1.12#ignoreHTTPSErrors?
<boolean> Whether to ignore HTTPS errors when sending network requests. Defaults tofalse
. Added in: v1.12#locale?
<string> Specify user locale, for exampleen-GB
,de-DE
, etc. Locale will affectnavigator.language
value,Accept-Language
request header value as well as number and date formatting rules. Added in: v1.12#offline?
<boolean> Whether to emulate network being offline. Defaults tofalse
. Added in: v1.12#recordHar?
<Object> Enables HAR recording for all pages intorecordHar.path
file. If not specified, the HAR is not recorded. Make sure to await browserContext.close() for the HAR to be saved. Added in: v1.12#omitContent?
<boolean> Optional setting to control whether to omit request content from the HAR. Defaults tofalse
. Deprecated, usecontent
policy instead.content?
<"omit"|"embed"|"attach"> Optional setting to control resource content management. Ifomit
is specified, content is not persisted. Ifattach
is specified, resources are persisted as separate files or entries in the ZIP archive. Ifembed
is specified, content is stored inline the HAR file as per HAR specification. Defaults toattach
for.zip
output files and toembed
for all other file extensions.path
<string> Path on the filesystem to write the HAR file to. If the file name ends with.zip
,content: 'attach'
is used by default.mode?
<"full"|"minimal"> When set tominimal
, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies, security and other types of HAR information that are not used when replaying from HAR. Defaults tofull
.urlFilter?
<string|RegExp> A glob or regex pattern to filter requests that are stored in the HAR. When abaseURL
via the context options was provided and the passed URL is a path, it gets merged via thenew URL()
constructor.
recordVideo?
<Object> Enables video recording for all pages intorecordVideo.dir
directory. If not specified videos are not recorded. Make sure to await browserContext.close() for videos to be saved. Added in: v1.12#dir
<string> Path to the directory to put videos into.size?
<Object> Optional dimensions of the recorded videos. If not specified the size will be equal toviewport
scaled down to fit into 800x800. Ifviewport
is not configured explicitly the video size defaults to 800x450. Actual picture of each page will be scaled down if necessary to fit the specified size.
timeout?
<number> Maximum time in milliseconds to wait for the application to start. Defaults to30000
(30 seconds). Pass0
to disable timeout. Added in: v1.15#timezoneId?
<string> Changes the timezone of the context. See ICU's metaZones.txt for a list of supported timezone IDs. Added in: v1.12#
- returns:Promise<ElectronApplication>># <
Launches electron application specified with the executablePath
.