Playwright Adapter
Use @chaos-maker/playwright when you need pre-navigation interception and optional trace annotations.
import { injectChaos, removeChaos, getChaosLog, getChaosSeed, injectSWChaos, removeSWChaos, getSWChaosLog,} from '@chaos-maker/playwright';| Function | Purpose |
|---|---|
injectChaos(page, config, options?) | Registers config and core UMD as init scripts. Call before page.goto(). |
removeChaos(page) | Stops chaos and flushes trace attachments when tracing is active. |
getChaosLog(page) | Returns every emitted ChaosEvent. |
getChaosSeed(page) | Returns the seed for replay. |
injectSWChaos(page, config, options?) | Sends config to the active Service Worker after it controls the page. |
removeSWChaos(page, options?) | Stops chaos inside the active Service Worker. |
getSWChaosLog(page) | Returns Service Worker chaos events buffered on the page. |
Trace options
Section titled “Trace options”await injectChaos(page, config, { tracing: true, testInfo,});Trace output includes named chaos steps and a chaos-log.json attachment. Direct injectChaos() calls default tracing to off; fixture-based setup can opt into automatic trace handling.
Timing
Section titled “Timing”Call injectChaos() before navigation. The adapter uses Playwright addInitScript(), so app code observes patched browser APIs from the first script.
For Service Worker chaos, navigate to the app, wait for navigator.serviceWorker.controller, then call injectSWChaos() before the user action that triggers SW fetches.
SSE chaos and GraphQL operation matching need no extra adapter API. Pass sse rules or graphqlOperation network matchers to injectChaos().
Scenario profiles and runtime overrides
Section titled “Scenario profiles and runtime overrides”Pass profile (and optionally profileOverrides and customProfiles) on the config object - the adapter forwards them through to core’s resolution pipeline unchanged.
await injectChaos(page, { profile: 'mobile-checkout', profileOverrides: { network: { latencies: [{ urlPattern: '/api/extra', delayMs: 999, probability: 1 }] }, }, seed: 42,});See Scenario profiles for the resolution rules, the singular built-in mobileCheckout demo profile, the runtime override precedence, and the validation errors that surface as ChaosConfigError.
Debug Mode
Section titled “Debug Mode”Set debug: true on the config passed to injectChaos() to enable Debug Mode. It is independent of Playwright’s own --debug mode and PWDEBUG env var - Chaos Maker never reads them. Console mirrors land on page.on('console', …) with msg.type() === 'debug' and a [Chaos] ... body. Debug events ride into the chaos-log.json attachment but never as inline test.step entries.
Validation
Section titled “Validation”injectChaos validates the config from Node before any page touch. A malformed config throws ChaosConfigError synchronously from the test runner - the page never sees a bad rule.
import { injectChaos, ChaosConfigError } from '@chaos-maker/playwright';
await injectChaos(page, config, { validation: { unknownFields: 'warn' },});Pass validation to relax unknown-field handling, hook deprecation events, or run custom per-RuleType validators. See Rule Validation.