Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Webdriverio Webdriverio ModuleHookTracker Class

From Leeroopedia
Revision as of 11:57, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Webdriverio_Webdriverio_ModuleHookTracker_Class.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains BrowserStack, Performance_Instrumentation
Last Updated 2026-02-12 00:00 GMT

Overview

The ModuleHookTracker class is a singleton that tracks module lifecycle hooks with performance instrumentation, measuring execution times for startup, shutdown, and initialization phases of each SDK module.

Description

ModuleHookTracker provides a centralized mechanism for measuring the performance of module lifecycle events. It maps each combination of module name and hook type (e.g., accessibility:onStart, ai:onDriverInit) to a performance event name defined in MODULE_HOOK_EVENTS. The trackHook method wraps an async function execution with PerformanceTester.start() and PerformanceTester.end() calls, recording success or failure with error messages. The class supports 8 tracked modules (instrumentation, testhub, observability, percy, accessibility, ai, local, appautomate) and 5 hook types (onStart, onStop, onDriverInit, initSession, beforeSession). Convenience methods (trackOnStart, trackOnStop, etc.) simplify common tracking patterns.

Usage

Use this tracker when instrumenting module lifecycle events for performance measurement. Access the singleton via ModuleHookTracker.getInstance() or the exported moduleHookTracker constant.

Code Reference

Source Location

Signature

export class ModuleHookTracker {
    private static instance: ModuleHookTracker

    private constructor()

    static getInstance(): ModuleHookTracker

    async trackHook<T>(
        module: TrackedModule,
        hook: keyof ModuleLifecycleHooks,
        fn: () => Promise<T> | T
    ): Promise<T>

    async trackOnStart<T = void>(module: TrackedModule, fn: () => Promise<T> | T): Promise<T>
    async trackOnStop<T = void>(module: TrackedModule, fn: () => Promise<T> | T): Promise<T>
    async trackOnDriverInit<T = void>(module: TrackedModule, fn: () => Promise<T> | T): Promise<T>
    async trackInitSession<T = void>(module: TrackedModule, fn: () => Promise<T> | T): Promise<T>
    async trackBeforeSession<T = void>(module: TrackedModule, fn: () => Promise<T> | T): Promise<T>
}

export type TrackedModule =
    | 'instrumentation' | 'testhub' | 'observability' | 'percy'
    | 'accessibility' | 'ai' | 'local' | 'appautomate'

export interface ModuleLifecycleHooks {
    onStart?: () => Promise<void> | void
    onStop?: () => Promise<void> | void
    onDriverInit?: () => Promise<void> | void
    initSession?: () => Promise<void> | void
    beforeSession?: () => Promise<void> | void
}

export const moduleHookTracker: ModuleHookTracker

Import

import { ModuleHookTracker, moduleHookTracker } from './module-hook-tracker.js'
import type { TrackedModule, ModuleLifecycleHooks } from './module-hook-tracker.js'

I/O Contract

Inputs

Name Type Required Description
module TrackedModule Yes The module name to track (e.g., 'accessibility', 'ai', 'percy')
hook keyof ModuleLifecycleHooks Yes The lifecycle hook being executed (e.g., 'onStart', 'onStop')
fn T Yes The async function to execute and measure

Outputs

Name Type Description
Result of fn T The return value of the wrapped function, passed through transparently
Performance measurement PerformanceTester event Start/end timing recorded with success/failure status

Usage Examples

Tracking a module startup

import { moduleHookTracker } from './module-hook-tracker.js'

await moduleHookTracker.trackOnStart('accessibility', async () => {
    // Initialize accessibility module
    await accessibilityHandler.before(sessionId)
})

Tracking module shutdown with error handling

await moduleHookTracker.trackOnStop('percy', async () => {
    await percyHandler.teardown()
})
// If teardown throws, PerformanceTester.end() is called with
// success=false and the error message

Using the generic trackHook method

const result = await moduleHookTracker.trackHook('ai', 'onDriverInit', async () => {
    return await aiHandler.handleSelfHeal(options, browser)
})

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment