API Reference
Complete API reference for @wdio/tauri-service.
browser.tauri API
The following methods are available on the browser.tauri object when connected to a Tauri app.
browser.tauri.execute(script, ...args)
Execute JavaScript code in the Tauri frontend context with access to Tauri APIs. Requires tauri-plugin-wdio to be installed and configured.
Parameters:
script(Function | string) - JavaScript code to execute. If a function, receives Tauri APIs as the first parameter...args(any[]) - Additional arguments passed to the script
Returns: Promise\<ReturnValue>
Example:
// Execute with destructured Tauri APIs
const result = await browser.tauri.execute(({ core }) => {
return core.invoke('get_platform_info');
});
// Execute with full Tauri APIs object
const version = await browser.tauri.execute(async (tauri) => {
return tauri.app?.getVersion();
});
// Execute with arguments
const result = await browser.tauri.execute(
(tauri, name) => tauri.core.invoke('greet', { name }),
'World'
);
// Execute string of code
const result = await browser.tauri.execute('window.location.href');
Note: Requires tauri-plugin-wdio to be installed. See Plugin Setup.
browser.tauri.mock(command)
Mock a specific Tauri backend command. Returns a TauriMock object for configuring the mock behavior.
Parameters:
command(string) - Name of the Tauri command to mock
Returns: Promise\<TauriMock>
Example:
const mock = await browser.tauri.mock('read_file');
await mock.mockReturnValue('mocked file content');
// Now calling invoke('read_file', ...) returns 'mocked file content'
const content = await browser.tauri.execute(({ core }) => core.invoke('read_file'));
expect(content).toBe('mocked file content');
browser.tauri.isMockFunction(fn)
Check if a value is a Tauri mock function. This is a TypeScript type guard.
Parameters:
fn(unknown) - Value to check
Returns: boolean (type narrows to TauriMockInstance when true)
Example:
const mock = await browser.tauri.mock('clipboard_read');
if (browser.tauri.isMockFunction(mock)) {
// TypeScript knows mock is TauriMockInstance here
expect(mock.mock.calls).toHaveLength(1);
}
browser.tauri.clearAllMocks(commandPrefix?)
Clear all mock call history and reset results, but keep the mock implementations in place.
Parameters:
commandPrefix(string, optional) - If provided, only mocks with command names starting with this prefix will be cleared
Returns: Promise<void>
Example:
// Clear all mocks
await browser.tauri.clearAllMocks();
// Clear only clipboard-related mocks
await browser.tauri.clearAllMocks('clipboard');
browser.tauri.resetAllMocks(commandPrefix?)
Reset all mocks to their initial state (clears implementations and call history).
Parameters:
commandPrefix(string, optional) - If provided, only mocks with matching prefix are reset
Returns: Promise<void>
browser.tauri.restoreAllMocks(commandPrefix?)
Remove all mocks and restore original command implementations.
Parameters:
commandPrefix(string, optional) - If provided, only mocks with matching prefix are restored
Returns: Promise<void>
Example:
await browser.tauri.restoreAllMocks();
// Commands now call the real Tauri backend again
browser.tauri.triggerDeeplink(url)
Trigger a deeplink to the Tauri application for testing protocol handlers. Uses platform-specific commands (open on macOS, xdg-open on Linux, cmd /c start on Windows).
Parameters:
url(string) - The deeplink URL to trigger (e.g.,'myapp://open?file=test.txt')
Returns: Promise<void>
Example:
await browser.tauri.triggerDeeplink('myapp://open?file=test.txt');
await browser.waitUntil(async () => {
const openedFile = await browser.tauri.execute(() => {
return globalThis.lastOpenedFile;
});
return openedFile === 'test.txt';
});
See Deeplink Testing for full usage guide.
browser.tauri.switchWindow(label)
Switch the active Tauri window for subsequent operations. Changes the window that browser.tauri.execute() and other Tauri-specific operations target.
Parameters:
label(string) - The window label to switch to (e.g.,'main','settings')
Returns: Promise<void>
Example:
// Switch to the settings window
await browser.tauri.switchWindow('settings');
// Now executes in the settings window context
const data = await browser.tauri.execute(({ core }) => core.invoke('get_settings'));
// Switch back to main window
await browser.tauri.switchWindow('main');
Note: The window label must exist in your Tauri app. Use browser.tauri.listWindows() to get available labels.
browser.tauri.listWindows()
Get a list of all available Tauri window labels in the application.
Returns: Promise<string[]>
Example:
const windows = await browser.tauri.listWindows();
console.log(windows); // ['main', 'settings', 'dialog']
Updating browser.tauri.execute with Per-Call Options
The execute method supports optional per-call options to override session defaults:
Parameters:
script(Function | string) - JavaScript code to executeoptions(object, optional) - Per-call execution optionswindowLabel(string) - Override the default window for this call only
...args(any[]) - Additional arguments passed to the script
Example:
import { withExecuteOptions } from '@wdio/tauri-service';
// Execute in a specific window without changing session default
const result = await browser.tauri.execute(
(tauri) => tauri.core.invoke('get_data'),
withExecuteOptions({ windowLabel: 'popup' })
);
// Can also pass arguments after options
const greeting = await browser.tauri.execute(
(tauri, name) => tauri.core.invoke('greet', { name }),
withExecuteOptions({ windowLabel: 'settings' }),
'Alice'
);