### Install electron-playwright-helpers Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/readme-template.hbs.md Installs the electron-playwright-helpers package as a development dependency using npm. ```shell npm i -D electron-playwright-helpers ``` -------------------------------- ### Install electron-playwright-helpers Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Installs the electron-playwright-helpers package as a development dependency using npm. ```shell npm i -D electron-playwright-helpers ``` -------------------------------- ### Electron Playwright Helpers Usage Example Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/readme-template.hbs.md Demonstrates how to import and use electron-playwright-helpers in a Javascript test file. It covers finding the latest Electron build, parsing the application, launching the app, stubbing dialogs, clicking menu items, and handling IPC messages. ```javascript const eph = require('electron-playwright-helpers') // - or cherry pick - const { findLatestBuild, parseElectronApp, clickMenuItemById } = require('electron-playwright-helpers') let electronApp: ElectronApplication test.beforeAll(async () => { // find the latest build in the out directory const latestBuild = findLatestBuild() // parse the packaged Electron app and find paths and other info const appInfo = parseElectronApp(latestBuild) electronApp = await electron.launch({ args: [appInfo.main], // main file from package.json executablePath: appInfo.executable // path to the Electron executable }) }) test.afterAll(async () => { await electronApp.close() }) test('open a file', async () => { // stub electron dialog so dialog.showOpenDialog() // will return a file path without opening a dialog await eph.stubDialog(electronApp, 'showOpenDialog', { filePaths: ['/path/to/file'] }) // call the click method of menu item in the Electron app's application menu await eph.clickMenuItemById(electronApp, 'open-file') // get the result of an ipcMain.handle() function const result = await eph.ipcMainInvokeHandler(electronApp, 'get-open-file-path') // result should be the file path expect(result).toBe('/path/to/file') }) ``` -------------------------------- ### Electron Playwright Helpers Usage Example Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Demonstrates how to use electron-playwright-helpers in a Playwright test for an Electron application. It covers finding the latest build, parsing the Electron app, launching the app, stubbing dialogs, clicking menu items by ID, and handling IPC messages. ```javascript const eph = require('electron-playwright-helpers') // - or cherry pick - const { findLatestBuild, parseElectronApp, clickMenuItemById } = require('electron-playwright-helpers') let electronApp: ElectronApplication test.beforeAll(async () => { // find the latest build in the out directory const latestBuild = findLatestBuild() // parse the packaged Electron app and find paths and other info const appInfo = parseElectronApp(latestBuild) electronApp = await electron.launch({ args: [appInfo.main], // main file from package.json executablePath: appInfo.executable // path to the Electron executable }) }) test.afterAll(async () => { await electronApp.close() }) test('open a file', async () => { // stub electron dialog so dialog.showOpenDialog() // will return a file path without opening a dialog await eph.stubDialog(electronApp, 'showOpenDialog', { filePaths: ['/path/to/file'] }) // call the click method of menu item in the Electron app's application menu await eph.clickMenuItemById(electronApp, 'open-file') // get the result of an ipcMain.handle() function const result = await eph.ipcMainInvokeHandler(electronApp, 'get-open-file-path') // result should be the file path expect(result).toBe('/path/to/file') }) ``` -------------------------------- ### Get Electron Application Menu Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Fetches the current state of the application menu in an Electron application. The returned data is serializable and mirrors the structure of Electron's menu construction template. ```javascript getApplicationMenu(electronApp) ``` -------------------------------- ### Menu: Get Application Menu State Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves the current state of the entire Electron application menu. The returned data is serializable and mirrors the structure of Electron's menu construction templates. ```javascript getApplicationMenu(electronApp) ``` -------------------------------- ### Menu: Get Menu Item by ID Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Fetches information about an Electron application menu item using its ID. Returns serializable data, including primitives, objects, and arrays. ```javascript getMenuItemById(electronApp, menuId) ``` -------------------------------- ### IPC: Get Main Process Invoke Handler Return Value Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves the return value of an ipcMain.handle() function in the Electron main process. This is a reliable way to get data back from handled IPC events. ```javascript ipcMainInvokeHandler(electronApp, message, ...args) ``` -------------------------------- ### Handle IPC Invoke (Main Process) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves the return value of an ipcMain.handle() function executed in the Electron main process. This function is designed to get results from asynchronous handlers. ```javascript /** * Get the return value of an `ipcMain.handle()` function * * @param {ElectronApplication} electronApp the ElectronApplication object from Playwright * @param {string} message the channel to call the first listener for * @param {...unknown} args one or more arguments to send * @returns {Promise} resolves with the result of the function called in main process */ async function ipcMainInvokeHandler(electronApp, message, ...args) {} ``` -------------------------------- ### Get Electron App Menu Item by ID Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves information about a specific menu item within an Electron application using its ID. It returns serializable data, including primitives, objects, and arrays. ```javascript getMenuItemById(electronApp, menuId) ``` -------------------------------- ### Menu: Get Menu Item Attribute Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves the value of a specified attribute for an Electron application menu item identified by its ID. ```javascript getMenuItemAttribute(electronApp, menuId, attribute) ``` -------------------------------- ### Get Menu Item Attribute (JavaScript) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Retrieves a specific attribute's value from a menu item identified by its ID. This function targets menu items within the Electron application's menu and returns the attribute value as a string. ```JavaScript async function getMenuItemAttribute(electronApp, menuId, attribute) { // Implementation details for getting a menu item attribute } ``` -------------------------------- ### Parse Electron App Build Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Given a directory containing an Electron app build, this function returns metadata about the application. This includes the path to the executable, main file, resources directory, and package.json contents. ```JavaScript parseElectronApp(buildDir) ``` -------------------------------- ### Electron Playwright Helpers TypeScript Imports Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Shows how to import the electron-playwright-helpers library in a TypeScript project, including options for importing all helpers or cherry-picking specific functions. ```typescript import * as eph from 'electron-playwright-helpers' // - or cherry pick - import { electronWaitForFunction, ipcMainCallFirstListener, clickMenuItemById } from 'electron-playwright-helpers' // then same as Javascript above ``` -------------------------------- ### Parse Electron App Build Information Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Extracts metadata from an Electron application build directory or executable. It returns details such as the executable path, main process file path, application name, resources directory, ASAR status, platform, architecture, and package.json contents. ```javascript parseElectronApp(buildDir) ``` -------------------------------- ### Electron Playwright Helpers TypeScript Import Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/readme-template.hbs.md Shows how to import functions from the electron-playwright-helpers library in a TypeScript project, either importing the entire module or cherry-picking specific functions. ```typescript import * as eph from 'electron-playwright-helpers' // - or cherry pick - import { electronWaitForFunction, ipcMainCallFirstListener, clickMenuItemById } from 'electron-playwright-helpers' // then same as Javascript above ``` -------------------------------- ### Find Latest Electron Build Directory Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Parses a specified directory to locate the most recent build of an Electron project. It assumes build directories are named with a hyphen-delimited platform, like 'out/my-app-win-x64'. If your build directory differs from 'out', you can specify it. ```javascript findLatestBuild(buildDirectory) ``` -------------------------------- ### Call First IPC Listener (Main Process) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Calls the first registered ipcMain listener for a specified message in the main process and returns its result. This is useful for retrieving test data from the main process, though ipcMainInvokeHandler is generally preferred. ```javascript /** * Call the first listener for a given ipcMain message in the main process * and return its result. * NOTE: ipcMain listeners usually don't return a value, but we're using * this to retrieve test data from the main process. * Generally, it's probably better to use `ipcMainInvokeHandler()` instead. * * @param {ElectronApplication} electronApp the ElectronApplication object from Playwright * @param {string} message the channel to call the first listener for * @param {...unknown} args one or more arguments to send * @returns {Promise} resolves with the result of the function * @throws {Error} if there are no ipcMain listeners for the event */ async function ipcMainCallFirstListener(electronApp, message, ...args) {} ``` -------------------------------- ### Find Latest Build Directory Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Parses the specified build directory (defaults to 'out') to find the latest build of an Electron project. It assumes build directories are named with a hyphen-delimited platform name (e.g., 'out/my-app-win-x64'). ```JavaScript findLatestBuild(buildDirectory) ``` -------------------------------- ### Menu: Find Menu Item by Property Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Searches through a list of Electron menu items to find the first one that matches a given property and value. ```javascript findMenuItem(electronApp, property, value, menuItems) ``` -------------------------------- ### Menu: Click Menu Item by Property Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Clicks the first Electron application menu item that matches a specified property and value. This is useful for menu items without an ID but is less efficient than using an ID. ```javascript clickMenuItem(electronApp, property, value) ``` -------------------------------- ### IPC: Call First Main Process Listener Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Calls the first listener for a given IPC message in the Electron main process and returns its result. Useful for retrieving test data from the main process, though ipcMainInvokeHandler is generally preferred. ```javascript ipcMainCallFirstListener(electronApp, message, ...args) ``` -------------------------------- ### Invoke IPC Handler (Renderer Process) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Sends an ipcRenderer.invoke() message from a specified Playwright Page, expecting a return value from the Electron main process. Requires nodeIntegration to be true and contextIsolation to be false in the window's webPreferences. ```javascript /** * Send an ipcRenderer.invoke() from a given window. * Note: nodeIntegration must be true and contextIsolation must be false * in the webPreferences for this window * * @param {Page} page the Playwright Page to send the ipcRenderer.invoke() from * @param {string} message the channel to send the ipcRenderer.invoke() to * @param {...unknown} args one or more arguments to send to the ipcRenderer.invoke() * @returns {Promise} resolves with the result of ipcRenderer.invoke() */ async function ipcRendererInvoke(page, message, ...args) {} ``` -------------------------------- ### IPC: Send to Main Process from Renderer Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Sends an ipcRenderer.send() message to the Electron main process from a specified Playwright page. Requires nodeIntegration to be true and contextIsolation to be false in the BrowserWindow's webPreferences. ```javascript ipcRendererSend(page, channel, ...args) ``` -------------------------------- ### Find Electron App Menu Item by Property Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Searches for a menu item within an Electron application based on a specified property and its value. It can optionally search within a provided list of menu items or retrieve the entire application menu if none is supplied. ```javascript findMenuItem(electronApp, property, value, menuItems) ``` -------------------------------- ### IPC: Call First Renderer Process Listener Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Calls the first listener for a given IPC channel in a specified Electron renderer window. Unlike most IPC listeners, this function is designed to return a value, primarily for retrieving data from the renderer process. Requires nodeIntegration to be true. ```javascript ipcRendererCallFirstListener(page, message, ...args) ``` -------------------------------- ### Click Menu Item by Property (JavaScript) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Clicks the first menu item that matches a specified property and value. This is a fallback for menu items without an ID, though using an ID is generally more reliable and faster. It operates on the application menu. ```JavaScript async function clickMenuItem(electronApp, property, value) { // Implementation details for clicking a menu item by property } ``` -------------------------------- ### Wait for Electron Main Process Function Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Waits for a given function to evaluate to true within the Electron main process. This function mimics Playwright's page.waitForFunction but operates on the Electron main process context, allowing for synchronization of asynchronous operations. ```javascript electronWaitForFunction(electronApp, fn, arg) ``` -------------------------------- ### IPC: Invoke from Renderer Process Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Sends an ipcRenderer.invoke() message from a specified Playwright page. This function is used to invoke handlers in the main process and receive a return value. Requires nodeIntegration to be true and contextIsolation to be false. ```javascript ipcRendererInvoke(page, message, ...args) ``` -------------------------------- ### Wait for Function in Electron Main Process Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Waits for a provided function to evaluate to true within the main Electron process. This utility mirrors Playwright's page.waitForFunction but operates on the Electron main process. ```JavaScript electronWaitForFunction(electronApp, fn, arg) ``` -------------------------------- ### Stub Multiple Electron Dialog Methods Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Mock multiple dialog methods of the Electron dialog module within your Playwright tests. This function allows you to define specific return values for various dialog interactions, such as showOpenDialog and showSaveDialog. It's useful for simulating different user actions like selecting files or canceling dialogs without displaying the actual dialog windows. ```TypeScript await stubMultipleDialogs(app, [ { method: 'showOpenDialog', value: { filePaths: ['/path/to/file1', '/path/to/file2'], canceled: false, }, }, { method: 'showSaveDialog', value: { filePath: '/path/to/file', canceled: false, }, }, ]) await clickMenuItemById(app, 'save-file') // when your application calls dialog.showSaveDialog, // it will return the value you specified ``` -------------------------------- ### Stub Multiple Electron Dialog Methods Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Mocks multiple methods of the Electron dialog module, allowing for controlled return values during testing. This prevents dialogs from appearing and enables testing of various user scenarios, such as file selection or dialog cancellation. ```javascript stubMultipleDialogs(app, mocks) ``` -------------------------------- ### Call First IPC Listener (JavaScript) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Calls the first registered listener for a given IPC channel in an Electron application's renderer process. This function is designed to return a value from the listener, unlike standard ipcRenderer listeners. It requires nodeIntegration to be enabled. ```JavaScript async function ipcRendererCallFirstListener(page, message, ...args) { // Implementation details for calling the first listener } ``` -------------------------------- ### Send IPC Message (Renderer Process) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Sends an ipcRenderer.send() message from a specified Playwright Page to the Electron main process. Requires nodeIntegration to be true and contextIsolation to be false in the BrowserWindow's webPreferences. ```javascript /** * Send an `ipcRenderer.send()` (to main process) from a given window. * Note: nodeIntegration must be true and contextIsolation must be false * in the webPreferences for this BrowserWindow. * * @param {Page} page the Playwright Page to send the ipcRenderer.send() from * @param {string} channel the channel to send the ipcRenderer.send() to * @param {...unknown} args one or more arguments to send to the `ipcRenderer.send()` * @returns {Promise} resolves with the result of `ipcRenderer.send()` */ async function ipcRendererSend(page, channel, ...args) {} ``` -------------------------------- ### Menu: Wait for Menu Item Existence Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Pauses execution until an Electron application menu item with the specified ID becomes available. ```javascript waitForMenuItem(electronApp, id) ``` -------------------------------- ### Stub All Electron Dialog Methods Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Stub all dialog methods for an Electron application using Playwright. This function is a shortcut that calls stubMultipleDialogs for every dialog method. It ensures that no dialog windows are displayed during tests, providing a baseline for dialog behavior. For controlled testing, consider using stubDialog or stubMultipleDialogs to specify return values. ```TypeScript await stubAllDialogs(app) ``` -------------------------------- ### Wait for Electron App Menu Item Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Waits for a specific menu item, identified by its ID, to exist within an Electron application. The function resolves once the menu item is found. ```javascript waitForMenuItem(electronApp, id) ``` -------------------------------- ### Menu: Click Menu Item by ID Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Executes the .click() method on an application menu item identified by its ID. This function is specifically for items within the application menu. ```javascript clickMenuItemById(electronApp, id) ``` -------------------------------- ### Emit IPC Message (Main Process) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Emits an ipcMain message from the Electron main process, triggering all registered listeners for that message. This function is used for signaling events and does not transfer data between processes. ```javascript /** * Emit an ipcMain message from the main process. * This will trigger all ipcMain listeners for the message. * This does not transfer data between main and renderer processes. * It simply emits an event in the main process. * * @param {ElectronApplication} electronApp the ElectronApplication object from Playwright * @param {string} message the channel to call all ipcMain listeners for * @param {...unknown} args one or more arguments to send * @returns {Promise} true if there were listeners for this message * @throws {Error} if there are no ipcMain listeners for the event */ async function ipcMainEmit(electronApp, message, ...args) {} ``` -------------------------------- ### Stub a Single Electron Dialog Method Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Stub a single dialog method in an Electron application using Playwright. This function is a convenience wrapper for stubMultipleDialogs, allowing you to mock specific dialog interactions like file opening or saving. It controls the return value when your application calls a dialog method, enabling testing of user file selection or cancellation scenarios. ```TypeScript await stubDialog(app, 'showOpenDialog', { filePaths: ['/path/to/file'], canceled: false, }) await clickMenuItemById(app, 'open-file') // when time your application calls dialog.showOpenDialog, // it will return the value you specified ``` -------------------------------- ### Wait for Electron App Menu Item Status Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Waits for a menu item in an Electron application to have a specific attribute value. This is useful for scenarios like waiting for a menu item to be enabled or visible. ```javascript waitForMenuItemStatus(electronApp, id, property, value) ``` -------------------------------- ### Stub All Electron Dialog Methods Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md A convenience function that stubs all methods of the Electron dialog module. This ensures that no dialogs are displayed during tests. For more granular control over dialog return values, consider using `stubDialog` or `stubMultipleDialogs`. ```javascript stubAllDialogs(app) ``` -------------------------------- ### Click Menu Item by ID (JavaScript) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Executes the '.click()' method on a menu item identified by its ID within the Electron application's menu. This function is specifically for items in the application menu and returns the result of the click action. ```JavaScript async function clickMenuItemById(electronApp, id) { // Implementation details for clicking a menu item by ID } ``` -------------------------------- ### Emit IPC Main Message Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Emits an IPC message from the Electron main process, triggering all registered listeners for that message. This function does not facilitate data transfer between main and renderer processes. ```javascript ipcMainEmit(electronApp, message, ...args) ``` -------------------------------- ### Stub Single Electron Dialog Method Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Stubs a specific method of the Electron dialog module to control its return value during tests. This is useful for simulating user interactions like file selection or cancellation without actual dialogs appearing. It's recommended to call this before each expected dialog invocation. ```javascript stubDialog(app, method, value) ``` -------------------------------- ### Menu: Wait for Menu Item Status Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Waits until an Electron application menu item with the specified ID has a particular attribute value, such as being enabled or visible. ```javascript waitForMenuItemStatus(electronApp, id, property, value) ``` -------------------------------- ### IPC: Emit Event in Renderer Process Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Emits an IPC message to a specified Electron renderer window, triggering all ipcRenderer listeners for that message. This function does not transfer data between processes but emits an event within the renderer. Requires nodeIntegration to be true. ```javascript ipcRendererEmit(page, message, ...args) ``` -------------------------------- ### Promise: Add Timeout Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Adds a timeout to a given Promise, rejecting it with a specified message if it does not resolve or reject within the given milliseconds. ```javascript addTimeoutToPromise(promise, timeoutMs, timeoutMessage) ``` -------------------------------- ### Check Serialized Native Image Success Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md A type guard function to determine if a SerializedNativeImage object represents a successful operation or an error. ```javascript isSerializedNativeImageSuccess() ``` -------------------------------- ### Add Timeout to Helper Function Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Adds a timeout mechanism to any helper function within this library that returns a Promise. It allows specifying a timeout duration and a custom message for timeout rejections. ```javascript addTimeout(functionName, timeoutMs, timeoutMessage, ...args) ``` -------------------------------- ### Add Timeout to Promise Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Applies a timeout to any given Promise. If the promise does not resolve within the specified time, it will reject with a timeout message. Otherwise, it resolves with the original promise's result. ```javascript addTimeoutToPromise(promise, timeoutMs, timeoutMessage) ``` -------------------------------- ### Check Serialized Native Image Success Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md A type guard function to determine if a SerializedNativeImage represents a successful operation or an error. ```JavaScript isSerializedNativeImageSuccess() ``` -------------------------------- ### Emit IPC Message (JavaScript) Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Emits an IPC message to trigger all registered listeners for a specific channel in an Electron application's renderer process. This function emits an event within the renderer process and requires nodeIntegration to be enabled. It returns true if the event was emitted successfully. ```JavaScript async function ipcRendererEmit(page, message, ...args) { // Implementation details for emitting an IPC message } ``` -------------------------------- ### Add Timeout to Promise Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md Adds a timeout to any helper function from this library that returns a Promise. This is useful for ensuring that asynchronous operations do not hang indefinitely. ```JavaScript addTimeout(functionName, timeoutMs, timeoutMessage, ...args) ``` -------------------------------- ### Check Serialized Native Image Error Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md A type guard function to determine if a SerializedNativeImage represents an error condition. ```JavaScript isSerializedNativeImageError() ``` -------------------------------- ### Check Serialized Native Image Error Source: https://github.com/spaceagetv/electron-playwright-helpers/blob/main/README.md A type guard function to identify if a SerializedNativeImage object indicates an error condition. ```javascript isSerializedNativeImageError() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.