| Crates.io | tauri-plugin-wdio |
| lib.rs | tauri-plugin-wdio |
| version | 1.0.0-next.0 |
| created_at | 2025-12-28 13:38:24.364947+00 |
| updated_at | 2025-12-28 13:38:24.364947+00 |
| description | A Tauri plugin for WebDriverIO testing with execute and mocking capabilities |
| homepage | https://github.com/webdriverio/desktop-mobile-testing/tree/main/packages/tauri-plugin |
| repository | https://github.com/webdriverio/desktop-mobile-testing |
| max_upload_size | |
| id | 2008834 |
| size | 169,359 |
A Tauri v2 plugin providing execute and mocking capabilities for WebDriverIO testing. This plugin enables E2E tests to execute JavaScript code in the frontend context with access to Tauri APIs and mock backend commands for isolated testing.
[dependencies]
tauri-plugin-wdio = { path = "../../packages/tauri-plugin" }
# or from crates.io when published
# tauri-plugin-wdio = "0.1.0"
Add the plugin to your Tauri app's src-tauri/src/main.rs:
use tauri_plugin_wdio::init;
fn main() {
tauri::Builder::default()
.plugin(init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Add the plugin to your app's src-tauri/build.rs:
fn main() {
tauri_build::try_build(
tauri_build::Attributes::new()
.plugin(
"wdio",
tauri_build::InlinedPlugin::new()
)
)
.expect("failed to run tauri-build");
}
The plugin requires permissions to be configured in your app's capability file. Add these permissions to your src-tauri/capabilities/default.json:
{
"permissions": [
"wdio:allow-execute",
"wdio:allow-set-mock",
"wdio:allow-get-mock",
"wdio:allow-clear-mocks",
"wdio:allow-reset-mocks",
"wdio:allow-restore-mocks"
]
}
Include the plugin's frontend JavaScript in your app. The plugin exposes window.wdioTauri with the following API:
// Import the plugin's frontend code
import '@wdio/tauri-plugin/guest-js';
// Or if using a bundler, ensure the guest-js is included in your build
The plugin provides an execute command that runs JavaScript in the frontend context with access to Tauri APIs:
// In your WebDriverIO tests
await browser.execute(() => {
return window.wdioTauri.execute(
(tauri) => tauri.core.invoke('get_platform_info'),
[]
);
});
The execute function receives the Tauri APIs object as the first parameter, allowing you to call any Tauri command:
await browser.execute(() => {
return window.wdioTauri.execute(
async (tauri) => {
const platform = await tauri.core.invoke('get_platform_info');
const version = await tauri.core.invoke('get_version');
return { platform, version };
},
[]
);
});
Set up mocks for Tauri backend commands:
// Set a mock with a return value
await browser.execute(() => {
return window.wdioTauri.setMock('get_platform_info', {
return_value: { os: 'mocked', arch: 'x64' }
});
});
// Execute the command (will return mocked value)
const result = await browser.execute(() => {
return window.wdioTauri.execute(
(tauri) => tauri.core.invoke('get_platform_info'),
[]
);
});
// result === { os: 'mocked', arch: 'x64' }
// Get mock configuration
const mock = await browser.execute(() => {
return window.wdioTauri.getMock('get_platform_info');
});
// Clear all mocks
await browser.execute(() => {
return window.wdioTauri.clearMocks();
});
// Reset all mocks (clears and removes handlers)
await browser.execute(() => {
return window.wdioTauri.resetMocks();
});
// Restore all mocks (removes mocks and restores original handlers)
await browser.execute(() => {
return window.wdioTauri.restoreMocks();
});
window.wdioTauri)execute(script: string, args?: unknown[]): Promise<unknown>Execute JavaScript code in the frontend context. The script should be a function that receives the Tauri APIs object as the first parameter.
Parameters:
script: JavaScript function string (without the first parameter - it will receive Tauri APIs)args: Optional array of arguments to pass to the script (after Tauri APIs)Returns: Promise resolving to the script's return value
Example:
window.wdioTauri.execute(
'(tauri) => tauri.core.invoke("get_platform_info")',
[]
);
setMock(command: string, config: MockConfig): Promise<void>Set a mock for a Tauri command.
Parameters:
command: Name of the command to mockconfig: Mock configuration object
return_value: Value to return when the command is calledimplementation: Function string to execute instead (not yet implemented)Example:
window.wdioTauri.setMock('get_platform_info', {
return_value: { os: 'mocked', arch: 'x64' }
});
getMock(command: string): Promise<MockConfig | null>Get the mock configuration for a command.
Returns: Mock configuration or null if not mocked
clearMocks(): Promise<void>Clear all mocks.
resetMocks(): Promise<void>Reset all mocks (clears and removes handlers).
restoreMocks(): Promise<void>Restore all mocks (removes mocks and restores original handlers).
The plugin provides the following Tauri commands:
plugin:wdio|execute - Execute JavaScript in frontend contextplugin:wdio|set-mock - Set a mock for a commandplugin:wdio|get-mock - Get mock configurationplugin:wdio|clear-mocks - Clear all mocksplugin:wdio|reset-mocks - Reset all mocksplugin:wdio|restore-mocks - Restore all mocksThe @wdio/tauri-service automatically uses this plugin when available. The service's execute() method will:
window.wdioTauri is availableThis means you can use the service's high-level API while benefiting from the plugin's capabilities:
import { execute } from '@wdio/tauri-service';
// Service automatically uses the plugin if available
const result = await execute(browser, (tauri) => {
return tauri.core.invoke('get_platform_info');
});
The plugin requires explicit permissions in your Tauri app's capability file. See the Installation section for the required permissions.
Ensure your build.rs registers the plugin as an InlinedPlugin:
tauri_build::try_build(
tauri_build::Attributes::new()
.plugin(
"wdio",
tauri_build::InlinedPlugin::new()
)
)
.expect("failed to run tauri-build");
Important: Do not pass .commands() to InlinedPlugin::new() - this causes Tauri to auto-generate invalid permission identifiers. The plugin uses explicit permissions defined in permissions/default.toml.
If window.wdioTauri is undefined:
main.rsbuild.rswithGlobalTauri is enabled in tauri.conf.json:{
"app": {
"withGlobalTauri": true
}
}
If you get permission errors:
tauri.conf.json:
{
"app": {
"security": {
"capabilities": ["default"]
}
}
}
cargo clean && pnpm buildIf execute commands timeout:
window.__TAURI__ is availableSee the test fixtures in fixtures/e2e-apps/tauri/ for complete examples of:
MIT OR Apache-2.0