1 release (0 unstable)
| 1.0.0-next.0 | Dec 28, 2025 |
|---|
#1501 in GUI
44KB
345 lines
@wdio/tauri-plugin
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.
Features
- ✅ Execute JavaScript: Run arbitrary JavaScript code in the frontend context with access to Tauri APIs
- ✅ Command Mocking: Intercept and mock Tauri backend commands for testing
- ✅ Mock Management: Set, get, clear, reset, and restore mocks
- ✅ TypeScript Support: Full TypeScript definitions for the frontend API
- ✅ Tauri v2 Compatible: Built for Tauri v2 with proper plugin architecture
Installation
1. Add to Cargo.toml
[dependencies]
tauri-plugin-wdio = { path = "../../packages/tauri-plugin" }
# or from crates.io when published
# tauri-plugin-wdio = "0.1.0"
2. Register Plugin in Your App
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");
}
3. Register Plugin in Build Script
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");
}
4. Configure Permissions
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"
]
}
5. Include Frontend JavaScript
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
Usage
Execute JavaScript
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 };
},
[]
);
});
Mocking Commands
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' }
Mock Management
// 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();
});
API Reference
Frontend API (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 objectreturn_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).
Rust API
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 mocks
Integration with @wdio/tauri-service
The @wdio/tauri-service automatically uses this plugin when available. The service's execute() method will:
- Check if
window.wdioTauriis available - Use the plugin's execute API if available
- Fall back to direct Tauri API calls if the plugin is not available
This 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');
});
Configuration
Permissions
The plugin requires explicit permissions in your Tauri app's capability file. See the Installation section for the required permissions.
Build Configuration
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.
Troubleshooting
Plugin Not Available
If window.wdioTauri is undefined:
- Ensure the plugin is registered in
main.rs - Ensure the plugin is registered in
build.rs - Ensure permissions are configured in your capability file
- Ensure the frontend JavaScript is included in your app
- Check that
withGlobalTauriis enabled intauri.conf.json:
{
"app": {
"withGlobalTauri": true
}
}
Permission Errors
If you get permission errors:
- Verify your capability file includes all required permissions
- Ensure the capability file is referenced correctly in
tauri.conf.json:{ "app": { "security": { "capabilities": ["default"] } } } - Clean and rebuild:
cargo clean && pnpm build
Execute Timeout
If execute commands timeout:
- Check that the script is valid JavaScript
- Ensure
window.__TAURI__is available - Verify the script doesn't have infinite loops
- Check browser console for errors
Examples
See the test fixtures in fixtures/e2e-apps/tauri/ for complete examples of:
- Plugin registration
- Permission configuration
- Frontend integration
- Test usage
License
MIT OR Apache-2.0
Dependencies
~16–63MB
~861K SLoC