Utility package providing comprehensive TypeScript type definitions for the WebdriverIO ecosystem
—
Service extension interfaces and comprehensive lifecycle hook definitions for extending WebdriverIO functionality with custom behaviors and integrations.
Service class and plugin interfaces for extending WebdriverIO functionality.
/**
* Service options interface - extended by individual services
*/
interface ServiceOption {
[key: string]: any;
}
/**
* Service class constructor
*/
interface ServiceClass {
new(
options: WebdriverIO.ServiceOption,
capabilities: ResolvedTestrunnerCapabilities,
config: WebdriverIOOptions
): ServiceInstance;
}
/**
* Service plugin with optional launcher
*/
interface ServicePlugin extends ServiceClass {
/** Default service class */
default: ServiceClass;
/** Optional launcher service */
launcher?: ServiceClass;
}
/**
* Service instance interface
*/
interface ServiceInstance extends HookFunctions {
/** Service configuration options */
options?: Record<string, any>;
/** Associated capabilities */
capabilities?: WebdriverIO.Capabilities;
/** WebdriverIO configuration */
config?: TestrunnerOptions;
}Different ways to configure services in WebdriverIO.
/**
* Service configuration options
*/
type ServiceEntry =
/** Service name as string */
| string
/** Service as hook functions object */
| HookFunctions
/** Service class */
| ServiceClass
/** Service name with options */
| [string, WebdriverIO.ServiceOption]
/** Service class with options */
| [ServiceClass, WebdriverIO.ServiceOption];Comprehensive lifecycle hook definitions for WebdriverIO test execution.
/**
* Complete hook function interface
*/
interface HookFunctions {
/**
* Executed before a test run starts. Has access to all config options and capabilities.
*/
onPrepare?(
config: Options.Testrunner,
capabilities: TestrunnerCapabilities
): void | Promise<void>;
/**
* Executed after all tests are completed. Has access to exit code and config.
*/
onComplete?(
exitCode: number,
config: Options.Testrunner,
capabilities: TestrunnerCapabilities,
results: Results
): void | Promise<void>;
/**
* Executed when a worker process starts.
*/
onWorkerStart?(
cid: string,
caps: WebdriverIO.Capabilities,
specs: string[],
args: WorkerMessageArgs,
execArgv: string[]
): void | Promise<void>;
/**
* Executed when a worker process ends.
*/
onWorkerEnd?(
cid: string,
exitCode: number,
specs: string[],
retries: number
): void | Promise<void>;
/**
* Executed before a WebDriver session starts.
*/
before?(
capabilities: WebdriverIO.Capabilities,
specs: string[],
browser: WebdriverIO.Browser
): void | Promise<void>;
/**
* Executed after a WebDriver session ends.
*/
after?(
result: number,
capabilities: WebdriverIO.Capabilities,
specs: string[]
): void | Promise<void>;
/**
* Executed before session starts (WebDriver level).
*/
beforeSession?(
config: Options.Testrunner,
capabilities: WebdriverIO.Capabilities,
specs: string[],
cid: string
): void | Promise<void>;
/**
* Executed after session ends (WebDriver level).
*/
afterSession?(
config: Options.Testrunner,
capabilities: WebdriverIO.Capabilities,
specs: string[]
): void | Promise<void>;
/**
* Executed when a session is reloaded.
*/
onReload?(
oldSessionId: string,
newSessionId: string
): void | Promise<void>;
/**
* Executed before a test suite starts.
*/
beforeSuite?(suite: Suite): void | Promise<void>;
/**
* Executed after a test suite ends.
*/
afterSuite?(suite: Suite): void | Promise<void>;
/**
* Executed before a test starts.
*/
beforeTest?(
test: Test,
context: any
): void | Promise<void>;
/**
* Executed after a test ends.
*/
afterTest?(
test: Test,
context: any,
result: TestResult
): void | Promise<void>;
/**
* Executed before a hook starts.
*/
beforeHook?(
test: Test,
context: any,
stepData?: any,
world?: World
): void | Promise<void>;
/**
* Executed after a hook ends.
*/
afterHook?(
test: Test,
context: any,
result: TestResult,
stepData?: any,
world?: World
): void | Promise<void>;
/**
* Executed before a WebDriver command.
*/
beforeCommand?(
commandName: string,
args: any[]
): void | Promise<void>;
/**
* Executed after a WebDriver command.
*/
afterCommand?(
commandName: string,
args: any[],
result: any,
error?: Error
): void | Promise<void>;
/**
* Executed before an assertion/expectation.
*/
beforeAssertion?(params: AssertionHookParams): void | Promise<void>;
/**
* Executed after an assertion/expectation.
*/
afterAssertion?(params: AfterAssertionHookParams): void | Promise<void>;
}Parameters passed to assertion-related hooks.
/**
* Parameters for assertion hooks
*/
interface AssertionHookParams {
/** Name of the matcher (e.g., 'toHaveText', 'toBeClickable') */
matcherName: string;
/** Value passed by user to the matcher */
expectedValue?: any;
/** Options passed to the matcher */
options: object;
}
/**
* Parameters for after assertion hooks
*/
interface AfterAssertionHookParams extends AssertionHookParams {
/** Assertion result */
result: {
/** Function returning the assertion message */
message: () => string;
/** Boolean result of the assertion */
result: boolean;
};
}Type definitions for hook function arrays and single values.
/**
* Hook functions supporting single or array values
*/
type Hooks = {
[K in keyof HookFunctions]: HookFunctions[K] | HookFunctions[K][];
};Test runner instance and class interfaces.
/**
* Test runner instance
*/
interface RunnerInstance {
/** Initialize the runner */
initialize(): Promise<void>;
/** Shutdown the runner */
shutdown(): Promise<boolean>;
/** Close a specific session */
closeSession?(cid: number): Promise<void>;
/** Get worker count */
getWorkerCount(): number;
/** Run tests */
run(args: any): Worker;
/** Worker pool */
workerPool: any;
/** Browser pool */
browserPool: any;
}
/**
* Test runner class constructor
*/
interface RunnerClass {
new(
options: WebdriverIO.BrowserRunnerOptions,
config: Omit<WebdriverIOOptions, 'capabilities' | keyof Hooks>
): RunnerInstance;
}
/**
* Runner plugin with launcher support
*/
interface RunnerPlugin extends RunnerClass {
/** Default runner class */
default: RunnerClass;
/** Optional launcher class */
launcher?: RunnerClass;
}Usage Examples:
import type { Services } from "@wdio/types";
// Custom service implementation
class CustomService implements Services.ServiceInstance {
constructor(
private options: Services.ServiceOption,
private capabilities: any,
private config: any
) {}
async onPrepare(config: any, capabilities: any) {
console.log('Setting up custom service...');
// Custom setup logic
}
async before(capabilities: any, specs: string[], browser: any) {
console.log('Starting test session...');
// Session initialization
}
async beforeTest(test: any, context: any) {
console.log(`Starting test: ${test.title}`);
// Pre-test setup
}
async afterTest(test: any, context: any, result: any) {
console.log(`Test completed: ${test.title}, passed: ${result.passed}`);
// Post-test cleanup
}
async after(result: number, capabilities: any, specs: string[]) {
console.log('Ending test session...');
// Session cleanup
}
async onComplete(exitCode: number, config: any, capabilities: any) {
console.log('Custom service cleanup complete');
// Final cleanup
}
}
// Service configuration
const services: Services.ServiceEntry[] = [
'chromedriver',
['@wdio/selenium-standalone-service', { installArgs: { drivers: { chrome: true } } }],
[CustomService, { customOption: true }],
{
beforeTest: (test, context) => {
console.log(`Running test: ${test.title}`);
},
afterTest: (test, context, result) => {
if (!result.passed) {
console.log(`Test failed: ${test.title}`);
}
}
}
];Install with Tessl CLI
npx tessl i tessl/npm-wdio--types