A WebdriverIO utility to help reporting all events with extensible base class for custom reporters
—
The WDIOReporter base class provides the foundation for creating custom WebdriverIO reporters with comprehensive event handling and output management capabilities.
The main base class that custom reporters extend, providing event-driven architecture and output stream management.
/**
* Base class for creating custom WebdriverIO reporters
* Extends EventEmitter to provide comprehensive event handling
*/
export default class WDIOReporter extends EventEmitter {
// Properties
outputStream: WriteStream | CustomWriteStream;
failures: number;
suites: Record<string, SuiteStats>;
hooks: Record<string, HookStats>;
tests: Record<string, TestStats>;
currentSuites: SuiteStats[];
counts: {
suites: number;
tests: number;
hooks: number;
passes: number;
skipping: number;
failures: number;
pending: number;
};
retries: number;
runnerStat?: RunnerStats;
isContentPresent: boolean;
specs: string[];
currentSpec?: string;
/**
* Constructor for WDIOReporter
* @param options - Reporter configuration options
*/
constructor(options: Partial<Reporters.Options>);
}
interface CustomWriteStream {
write: (content: unknown) => boolean;
}Usage Example:
import WDIOReporter from "@wdio/reporter";
import type { Reporters } from "@wdio/types";
export class MyReporter extends WDIOReporter {
constructor(options: Partial<Reporters.Options>) {
super(options);
console.log('Custom reporter initialized');
}
}Methods for managing reporter output streams and content writing.
/**
* Write content to the reporter's output stream
* @param content - Content to write (any type)
*/
write(content: unknown): void;
/**
* Indicates if reporter has completed all async operations
* Used to control process shutdown timing
* @returns true if reporter can safely terminate
*/
get isSynchronised(): boolean;Usage Example:
export class FileReporter extends WDIOReporter {
onTestPass(testStats: TestStats) {
this.write(`✓ ${testStats.fullTitle}\n`);
}
onTestFail(testStats: TestStats) {
this.write(`✗ ${testStats.fullTitle}\n`);
if (testStats.error) {
this.write(` ${testStats.error.message}\n`);
}
}
}Event handlers for test runner lifecycle events.
/**
* Called when test runner starts
* @param runnerStats - Statistics about the test runner
*/
onRunnerStart(runnerStats: RunnerStats): void;
/**
* Called when test runner ends
* @param runnerStats - Final statistics about the test runner
*/
onRunnerEnd(runnerStats: RunnerStats): void;Event handlers for test suite lifecycle events.
/**
* Called when test suite starts
* @param suiteStats - Statistics about the suite
*/
onSuiteStart(suiteStats: SuiteStats): void;
/**
* Called when test suite ends
* @param suiteStats - Final statistics about the suite
*/
onSuiteEnd(suiteStats: SuiteStats): void;
/**
* Called when test suite is retried (Cucumber only)
* @param suiteStats - Statistics about the retried suite
*/
onSuiteRetry(suiteStats: SuiteStats): void;Event handlers for individual test lifecycle events.
/**
* Called when test starts
* @param testStats - Statistics about the test
*/
onTestStart(testStats: TestStats): void;
/**
* Called when test passes
* @param testStats - Statistics about the passed test
*/
onTestPass(testStats: TestStats): void;
/**
* Called when test fails
* @param testStats - Statistics about the failed test
*/
onTestFail(testStats: TestStats): void;
/**
* Called when test is retried
* @param testStats - Statistics about the retried test
*/
onTestRetry(testStats: TestStats): void;
/**
* Called when test is skipped
* @param testStats - Statistics about the skipped test
*/
onTestSkip(testStats: TestStats): void;
/**
* Called when test is pending
* @param testStats - Statistics about the pending test
*/
onTestPending(testStats: TestStats): void;
/**
* Called when test ends (regardless of result)
* @param testStats - Final statistics about the test
*/
onTestEnd(testStats: TestStats): void;Event handlers for test hook (before/after) lifecycle events.
/**
* Called when hook starts
* @param hookStats - Statistics about the hook
*/
onHookStart(hookStats: HookStats): void;
/**
* Called when hook ends
* @param hookStats - Final statistics about the hook
*/
onHookEnd(hookStats: HookStats): void;Event handlers for WebDriver command execution events.
/**
* Called before WebDriver command execution
* @param commandArgs - Command arguments and metadata
*/
onBeforeCommand(commandArgs: BeforeCommandArgs): void;
/**
* Called after WebDriver command execution
* @param commandArgs - Command arguments, metadata, and results
*/
onAfterCommand(commandArgs: AfterCommandArgs): void;Event handlers for assertion execution events.
/**
* Called before assertion execution
* @param assertionArgs - Assertion arguments and metadata
*/
onBeforeAssertion(assertionArgs: unknown): void;
/**
* Called after assertion execution
* @param assertionArgs - Assertion arguments, metadata, and results
*/
onAfterAssertion(assertionArgs: unknown): void;interface Reporters.Options {
stdout?: boolean;
logFile?: string;
writeStream?: CustomWriteStream;
outputDir?: string;
}The reporter constructor accepts configuration options to control output behavior:
stdout: Write to standard outputlogFile: Path to log file for outputwriteStream: Custom write stream for outputoutputDir: Directory for output files (created if it doesn't exist)Install with Tessl CLI
npx tessl i tessl/npm-wdio--reporter