0
# Reporter Base Class
1
2
The WDIOReporter base class provides the foundation for creating custom WebdriverIO reporters with comprehensive event handling and output management capabilities.
3
4
## Capabilities
5
6
### WDIOReporter Class
7
8
The main base class that custom reporters extend, providing event-driven architecture and output stream management.
9
10
```typescript { .api }
11
/**
12
* Base class for creating custom WebdriverIO reporters
13
* Extends EventEmitter to provide comprehensive event handling
14
*/
15
export default class WDIOReporter extends EventEmitter {
16
// Properties
17
outputStream: WriteStream | CustomWriteStream;
18
failures: number;
19
suites: Record<string, SuiteStats>;
20
hooks: Record<string, HookStats>;
21
tests: Record<string, TestStats>;
22
currentSuites: SuiteStats[];
23
counts: {
24
suites: number;
25
tests: number;
26
hooks: number;
27
passes: number;
28
skipping: number;
29
failures: number;
30
pending: number;
31
};
32
retries: number;
33
runnerStat?: RunnerStats;
34
isContentPresent: boolean;
35
specs: string[];
36
currentSpec?: string;
37
38
/**
39
* Constructor for WDIOReporter
40
* @param options - Reporter configuration options
41
*/
42
constructor(options: Partial<Reporters.Options>);
43
}
44
45
interface CustomWriteStream {
46
write: (content: unknown) => boolean;
47
}
48
```
49
50
**Usage Example:**
51
52
```typescript
53
import WDIOReporter from "@wdio/reporter";
54
import type { Reporters } from "@wdio/types";
55
56
export class MyReporter extends WDIOReporter {
57
constructor(options: Partial<Reporters.Options>) {
58
super(options);
59
console.log('Custom reporter initialized');
60
}
61
}
62
```
63
64
### Output Management
65
66
Methods for managing reporter output streams and content writing.
67
68
```typescript { .api }
69
/**
70
* Write content to the reporter's output stream
71
* @param content - Content to write (any type)
72
*/
73
write(content: unknown): void;
74
75
/**
76
* Indicates if reporter has completed all async operations
77
* Used to control process shutdown timing
78
* @returns true if reporter can safely terminate
79
*/
80
get isSynchronised(): boolean;
81
```
82
83
**Usage Example:**
84
85
```typescript
86
export class FileReporter extends WDIOReporter {
87
onTestPass(testStats: TestStats) {
88
this.write(`✓ ${testStats.fullTitle}\n`);
89
}
90
91
onTestFail(testStats: TestStats) {
92
this.write(`✗ ${testStats.fullTitle}\n`);
93
if (testStats.error) {
94
this.write(` ${testStats.error.message}\n`);
95
}
96
}
97
}
98
```
99
100
### Runner Event Handlers
101
102
Event handlers for test runner lifecycle events.
103
104
```typescript { .api }
105
/**
106
* Called when test runner starts
107
* @param runnerStats - Statistics about the test runner
108
*/
109
onRunnerStart(runnerStats: RunnerStats): void;
110
111
/**
112
* Called when test runner ends
113
* @param runnerStats - Final statistics about the test runner
114
*/
115
onRunnerEnd(runnerStats: RunnerStats): void;
116
```
117
118
### Suite Event Handlers
119
120
Event handlers for test suite lifecycle events.
121
122
```typescript { .api }
123
/**
124
* Called when test suite starts
125
* @param suiteStats - Statistics about the suite
126
*/
127
onSuiteStart(suiteStats: SuiteStats): void;
128
129
/**
130
* Called when test suite ends
131
* @param suiteStats - Final statistics about the suite
132
*/
133
onSuiteEnd(suiteStats: SuiteStats): void;
134
135
/**
136
* Called when test suite is retried (Cucumber only)
137
* @param suiteStats - Statistics about the retried suite
138
*/
139
onSuiteRetry(suiteStats: SuiteStats): void;
140
```
141
142
### Test Event Handlers
143
144
Event handlers for individual test lifecycle events.
145
146
```typescript { .api }
147
/**
148
* Called when test starts
149
* @param testStats - Statistics about the test
150
*/
151
onTestStart(testStats: TestStats): void;
152
153
/**
154
* Called when test passes
155
* @param testStats - Statistics about the passed test
156
*/
157
onTestPass(testStats: TestStats): void;
158
159
/**
160
* Called when test fails
161
* @param testStats - Statistics about the failed test
162
*/
163
onTestFail(testStats: TestStats): void;
164
165
/**
166
* Called when test is retried
167
* @param testStats - Statistics about the retried test
168
*/
169
onTestRetry(testStats: TestStats): void;
170
171
/**
172
* Called when test is skipped
173
* @param testStats - Statistics about the skipped test
174
*/
175
onTestSkip(testStats: TestStats): void;
176
177
/**
178
* Called when test is pending
179
* @param testStats - Statistics about the pending test
180
*/
181
onTestPending(testStats: TestStats): void;
182
183
/**
184
* Called when test ends (regardless of result)
185
* @param testStats - Final statistics about the test
186
*/
187
onTestEnd(testStats: TestStats): void;
188
```
189
190
### Hook Event Handlers
191
192
Event handlers for test hook (before/after) lifecycle events.
193
194
```typescript { .api }
195
/**
196
* Called when hook starts
197
* @param hookStats - Statistics about the hook
198
*/
199
onHookStart(hookStats: HookStats): void;
200
201
/**
202
* Called when hook ends
203
* @param hookStats - Final statistics about the hook
204
*/
205
onHookEnd(hookStats: HookStats): void;
206
```
207
208
### Command Event Handlers
209
210
Event handlers for WebDriver command execution events.
211
212
```typescript { .api }
213
/**
214
* Called before WebDriver command execution
215
* @param commandArgs - Command arguments and metadata
216
*/
217
onBeforeCommand(commandArgs: BeforeCommandArgs): void;
218
219
/**
220
* Called after WebDriver command execution
221
* @param commandArgs - Command arguments, metadata, and results
222
*/
223
onAfterCommand(commandArgs: AfterCommandArgs): void;
224
```
225
226
### Assertion Event Handlers
227
228
Event handlers for assertion execution events.
229
230
```typescript { .api }
231
/**
232
* Called before assertion execution
233
* @param assertionArgs - Assertion arguments and metadata
234
*/
235
onBeforeAssertion(assertionArgs: unknown): void;
236
237
/**
238
* Called after assertion execution
239
* @param assertionArgs - Assertion arguments, metadata, and results
240
*/
241
onAfterAssertion(assertionArgs: unknown): void;
242
```
243
244
## Configuration Options
245
246
```typescript { .api }
247
interface Reporters.Options {
248
stdout?: boolean;
249
logFile?: string;
250
writeStream?: CustomWriteStream;
251
outputDir?: string;
252
}
253
```
254
255
The reporter constructor accepts configuration options to control output behavior:
256
257
- `stdout`: Write to standard output
258
- `logFile`: Path to log file for output
259
- `writeStream`: Custom write stream for output
260
- `outputDir`: Directory for output files (created if it doesn't exist)