Playwright
Playwright is the best fit when you need chaos from the first page load. The adapter registers Chaos Maker as an init script before page.goto().
npm install @chaos-maker/core @chaos-maker/playwrightThe fastest start is a named preset - drop a scenario into the config and run:
import { test, expect } from '@playwright/test';import { injectChaos } from '@chaos-maker/playwright';
test('checkout works under degraded mobile network', async ({ page }) => { await injectChaos(page, { presets: ['mobile-3g', 'checkout-degraded'], seed: 42 }); await page.goto('/checkout'); await expect(page.locator('[data-testid="checkout-form"]')).toBeVisible();});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(page, { 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 { test, expect } from '@playwright/test';import { injectChaos, getChaosLog, getChaosSeed } from '@chaos-maker/playwright';
test('shows error state when the API fails', async ({ page }) => { await injectChaos(page, { seed: 42, network: { failures: [{ urlPattern: '/api/orders', statusCode: 503, probability: 1 }], }, });
await page.goto('/orders'); await expect(page.locator('[data-testid="error-state"]')).toBeVisible();
const log = await getChaosLog(page); expect(log.some((event) => event.type === 'network:failure' && event.applied)).toBe(true); console.info('Chaos seed:', await getChaosSeed(page));});Log the replay seed only when a test fails:
import { formatSeedReproduction, getChaosSeed } from '@chaos-maker/playwright';
test.afterEach(async ({ page }, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) { console.error(formatSeedReproduction(await getChaosSeed(page))); }});Trace integration
Section titled “Trace integration”Pass testInfo with tracing enabled to annotate Playwright traces and attach the chaos log.
await injectChaos(page, config, { tracing: true, testInfo });The fixture entry point can also wire tracing automatically for suites that prefer shared setup.