Deeplink Testing
The service provides the ability to test custom protocol handlers and deeplinks in your Electron application using the browser.electron.triggerDeeplink() method. This feature automatically handles platform-specific differences, particularly on Windows where deeplinks would normally launch a new instance instead of reaching the test instance.
Overview
What is Deeplink Testing?
Deeplink testing allows you to verify that your Electron application correctly handles custom protocol URLs (e.g., myapp://action?param=value). This is essential when your app registers as a protocol handler and needs to respond to URLs opened from external sources like web browsers, emails, or other applications.
Why is it Needed?
Testing protocol handlers presents unique challenges:
- Windows Issue: On Windows, triggering a deeplink normally launches a new app instance instead of routing to the running test instance. This happens because the test instance and the externally-triggered instance use different user data directories.
- Test Automation: You need a programmatic way to trigger deeplinks without manual intervention.
- Cross-Platform Testing: Different platforms use different mechanisms to trigger protocol handlers.
When Should You Use It?
Use browser.electron.triggerDeeplink() when you need to:
- Test that your app correctly handles custom protocol URLs
- Verify deeplink parameter parsing and routing logic
- Ensure single-instance behavior works correctly
- Test protocol handler registration and activation
- Validate deeplink-driven workflows in your application
Basic Usage
Simple Example
describe('Protocol Handler Tests', () => {
it('should handle custom protocol deeplinks', async () => {
// Trigger the deeplink
await browser.electron.triggerDeeplink('myapp://open?file=test.txt');
// Wait for app to process it
await browser.waitUntil(async () => {
const openedFile = await browser.electron.execute(() => {
return globalThis.lastOpenedFile;
});
return openedFile === 'test.txt';
}, {
timeout: 5000,
timeoutMsg: 'App did not handle the deeplink'
});
});
});
Complex URL Parameters
The method preserves all URL parameters, including complex query strings:
it('should preserve query parameters', async () => {
await browser.electron.triggerDeeplink(
'myapp://action?param1=value1¶m2=value2&array[]=a&array[]=b'
);
const receivedParams = await browser.electron.execute(() => {
return globalThis.lastDeeplinkParams;
});
expect(receivedParams.param1).toBe('value1');
expect(receivedParams.param2).toBe('value2');
expect(receivedParams.array).toEqual(['a', 'b']);
});
Error Handling
it('should reject invalid protocols', async () => {
await expect(
browser.electron.triggerDeeplink('https://example.com')
).rejects.toThrow('Invalid deeplink protocol');
});
it('should reject malformed URLs', async () => {
await expect(
browser.electron.triggerDeeplink('not a url')
).rejects.toThrow('Invalid deeplink URL');
});