Usage Examples
Practical examples for testing Dioxus applications with WebdriverIO.
Basic Usage
Element Interactions
Standard WebDriver element interactions work with Dioxus apps:
describe('Dioxus App Interactions', () => {
it('should interact with form elements', async () => {
const input = await browser.$('input[name="username"]');
await input.setValue('test_user');
const button = await browser.$('button[type="submit"]');
await button.click();
const result = await browser.$('.result');
await result.waitForDisplayed();
const text = await result.getText();
expect(text).toBe('Success!');
});
it('should handle multiple elements', async () => {
const buttons = await browser.$$('button');
expect(buttons).toHaveLength(5);
for (const button of buttons) {
const text = await button.getText();
console.log('Button text:', text);
}
});
});
Execute API
Execute JavaScript in App Context
Use browser.dioxus.execute() to run JavaScript with access to Dioxus IPC:
describe('Dioxus API Access', () => {
it('should access Dioxus invoke API', async () => {
const result = await browser.dioxus.execute(({ invoke }) => {
return invoke('get_config');
});
expect(result).toBeDefined();
});
it('should use async operations', async () => {
const data = await browser.dioxus.execute(async ({ invoke }) => {
const user = await invoke('get_user');
const permissions = await invoke('get_user_permissions', { userId: (user as any).id });
return { user, permissions };
});
expect(data.user).toBeDefined();
expect(data.permissions).toBeInstanceOf(Array);
});
it('should execute with parameters', async () => {
const username = 'test_user';
const result = await browser.dioxus.execute(
(dx, name) => ({ received: name }),
username
);
expect(result.received).toBe('test_user');
});
it('should handle errors', async () => {
try {
await browser.dioxus.execute(() => {
throw new Error('Test error');
});
} catch (error) {
expect(error.message).toContain('Test error');
}
});
});
Mocking Dioxus Commands
Mock Backend Commands
describe('Command Mocking', () => {
it('should mock a simple command', async () => {
const mock = await browser.dioxus.mock('get_app_version');
await mock.mockReturnValue('1.2.3');
const version = await browser.dioxus.execute(({ invoke }) => {
return invoke('get_app_version');
});
expect(version).toBe('1.2.3');
});
it('should mock command with arguments', async () => {
const mock = await browser.dioxus.mock('get_user');
await mock.mockReturnValue({ id: 1, name: 'John Doe' });
const user = await browser.dioxus.execute(({ invoke }) => {
return invoke('get_user', { userId: 123 });
});
expect(user).toEqual({ id: 1, name: 'John Doe' });
});
it('should track mock calls', async () => {
const mock = await browser.dioxus.mock('save_data');
await mock.mockReturnValue({ success: true });
await browser.dioxus.execute(async ({ invoke }) => {
await invoke('save_data', { data: 'test1' });
await invoke('save_data', { data: 'test2' });
});
await mock.update();
expect(mock.mock.calls.length).toBeGreaterThanOrEqual(2);
});
it('should handle errors in mocks', async () => {
const mock = await browser.dioxus.mock('risky_operation');
await mock.mockRejectedValue(new Error('Operation failed'));
try {
await browser.dioxus.execute(({ invoke }) => {
return invoke('risky_operation');
});
} catch (error) {
expect(error.message).toBe('Operation failed');
}
});
it('should restore mocks after test', async () => {
const mock = await browser.dioxus.mock('get_data');
await mock.mockReturnValue({ mocked: true });
await mock.mockRestore();
// Now calls the real command
const result = await browser.dioxus.execute(({ invoke }) => {
return invoke('get_data');
});
expect(result).toBeDefined();
});
});