Accessing Electron APIs
This guide covers how to work with Electron APIs in your tests, including accessing APIs from the main process and mocking them for testing.
Accessing Electron APIs
The service provides access to Electron APIs from the main process using the Chrome DevTools Protocol (CDP). You can access these APIs by using the browser.electron.execute() method in your test suites.
Execute Scripts
Arbitrary scripts can be executed within the context of your Electron application main process using browser.electron.execute(). This allows Electron APIs to be accessed in a fluid way, in case you wish to manipulate your application at runtime or trigger certain events.
For example, a message modal can be triggered from a test via:
await browser.electron.execute(
(electron, param1, param2, param3) => {
const appWindow = electron.BrowserWindow.getFocusedWindow();
electron.dialog.showMessageBox(appWindow, {
message: 'Hello World!',
detail: `${param1} + ${param2} + ${param3} = ${param1 + param2 + param3}`,
});
},
1,
2,
3,
);
...which results in the application displaying the following alert:

Note: The first argument of the function is always the default export of the electron package that contains the Electron API.
How It Works
The service uses the Chrome DevTools Protocol (CDP) to communicate with your Electron application's main process. This provides a reliable and efficient way to:
- Execute JavaScript code in the main process context
- Access all Electron APIs
- Mock Electron APIs for testing
- Handle multiple windows and processes
No additional setup or imports are required in your Electron application - the service automatically connects to your app when it starts.
Mocking Electron APIs
The service allows for mocking of Electron API functionality via a Vitest-like interface.
Creating Mocks
Use browser.electron.mock() to mock individual Electron API functions:
const mockedShowOpenDialog = await browser.electron.mock('dialog', 'showOpenDialog');
await browser.electron.execute(
async (electron) =>
await electron.dialog.showOpenDialog({
properties: ['openFile', 'openDirectory'],
}),
);
expect(mockedShowOpenDialog).toHaveBeenCalledTimes(1);
expect(mockedShowOpenDialog).toHaveBeenCalledWith({
properties: ['openFile', 'openDirectory'],
});
Use browser.electron.mockAll() to mock all functions on an API simultaneously:
const { showOpenDialog, showMessageBox } = await browser.electron.mockAll('dialog');
await showOpenDialog.mockReturnValue('I opened a dialog!');
await showMessageBox.mockReturnValue('I opened a message box!');
Mocking Electron Classes
For Electron classes like Tray, BrowserWindow, Menu, etc., you can mock the entire class and all its instance methods:
// Mock the Tray class
const mockTray = await browser.electron.mock('Tray');
// Mock instance methods
await mockTray.setTitle.mockReturnValue(undefined);
await mockTray.setToolTip.mockReturnValue(undefined);
// Track constructor calls
const tray = await browser.electron.execute((electron) => new electron.Tray('/path/to/icon.png'));
expect(mockTray.__constructor).toHaveBeenCalledWith('/path/to/icon.png');
// Test instance method calls
await browser.electron.execute((electron) => {
const tray = new electron.Tray('/path/to/icon.png');
tray.setTitle('My App');
tray.setToolTip('Click for menu');
});
expect(mockTray.setTitle).toHaveBeenCalledWith('My App');
expect(mockTray.setToolTip).toHaveBeenCalledWith('Click for menu');
Class mocks provide:
__constructor: An ElectronFunctionMock object that tracks calls to the class constructor- Instance methods: All class instance methods are available as mock objects
mockRestore(): Method to restore the original class