Skip to content
Latest stable: v0.8.0.

WebdriverIO

WebdriverIO has no cross-browser pre-navigation hook, so call injectChaos() after browser.url(). Initial-load requests are not intercepted in this adapter.

Terminal window
npm install @chaos-maker/core @chaos-maker/webdriverio

The fastest start is a named preset - drop a scenario into the config and run:

import { browser, expect, $ } from '@wdio/globals';
import { injectChaos } from '@chaos-maker/webdriverio';
describe('checkout', () => {
it('works under degraded mobile network', async () => {
await browser.url('/checkout');
await injectChaos(browser, { presets: ['mobile-3g', 'checkout-degraded'], seed: 42 });
await $('[data-testid="checkout-form"]').waitForDisplayed();
});
});

See the Presets catalog for the full list. When several tests should share the same named scenario, wrap it into a scenario profile - the one built-in demo profile composes both presets above into a single name:

await injectChaos(browser, { profile: 'mobile-checkout', seed: 42 });

Pass profileOverrides alongside profile to tune one parameter for this call without forking the profile. When you need precise control, drop down to explicit rules:

import { browser, expect, $ } from '@wdio/globals';
import { injectChaos, getChaosLog } from '@chaos-maker/webdriverio';
describe('orders', () => {
it('shows error state when the API fails', async () => {
await browser.url('/orders');
await injectChaos(browser, {
seed: 42,
network: {
failures: [{ urlPattern: '/api/orders', statusCode: 503, probability: 1 }],
},
});
await $('#reload-orders').click();
await expect($('[data-testid="error-state"]')).toBeDisplayed();
const log = await getChaosLog(browser);
expect(log.some((event) => event.type === 'network:failure' && event.applied)).toBe(true);
});
});

Log the replay seed only when a test fails. WDIO’s afterTest runner hook (in wdio.conf.ts) is the reliable place: passed is supplied directly, with no Mocha-internal state to inspect.

wdio.conf.ts
import { formatSeedReproduction, getChaosSeed } from '@chaos-maker/webdriverio';
export const config = {
// …
async afterTest(_test, _context, { passed }) {
if (passed) return;
console.error(formatSeedReproduction(await getChaosSeed(browser)));
},
};

Call registerChaosCommands(browser) in a WDIO setup hook if you prefer browser.injectChaos(config), browser.getChaosLog(), and related commands.