Implementation:Webdriverio Webdriverio MSPO Service
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, Performance_Optimization |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
SelectorPerformanceService intercepts mobile selector commands during test execution, measures their performance, and stores timing metrics for later aggregation.
Description
The SelectorPerformanceService class implements Services.ServiceInstance and hooks into the WebdriverIO command lifecycle via beforeCommand and afterCommand. It tracks XPath-based element-finding commands ($, $$, custom$, custom$$) and their underlying findElement/findElements protocol calls. The service records timing data including original XPath duration and test context (file, suite, test name, line number). It automatically registers the MobileSelectorPerformanceReporter and applies command overwrites for optimization. On session end, it writes per-worker JSON data files for later aggregation.
Usage
This service is enabled by configuring the trackSelectorPerformance option on the Appium service. It runs automatically within each worker process during test execution.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/mspo-service.ts
- Lines: 28-302
Signature
export default class SelectorPerformanceService implements Services.ServiceInstance {
constructor(
private _options: AppiumServiceConfig,
private _config?: Options.Testrunner
)
async beforeSession(config: Options.Testrunner, _capabilities: never, _specs: never): Promise<void>
async before(_capabilities: never, _specs: never, browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser): Promise<void>
async beforeCommand(commandName: string, args: unknown[]): Promise<void>
async afterCommand(commandName: string, args: unknown[], _result: unknown, _error?: Error): Promise<void>
async afterSession(): Promise<void>
}
Import
import SelectorPerformanceService from './mobileSelectorPerformanceOptimizer/mspo-service.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| _options | AppiumServiceConfig |
Yes | Service config containing trackSelectorPerformance with pageObjectPaths, enableCliReport, enableMarkdownReport, reportPath, and maxLineLength |
| _config | Options.Testrunner |
No | WebdriverIO test runner config used for outputDir fallback and reporter registration |
Outputs
| Name | Type | Description |
|---|---|---|
| Worker JSON file | JSON file |
Per-worker performance data written to {reportDirectory}/selector-performance-worker-data/worker-data-{pid}.json
|
Usage Examples
Enabling the Service via Configuration
// wdio.conf.ts
export const config: WebdriverIO.Config = {
services: [
['appium', {
trackSelectorPerformance: {
pageObjectPaths: ['./tests/pageobjects'],
enableCliReport: true,
enableMarkdownReport: false,
reportPath: './reports',
maxLineLength: 120
}
}]
]
}
How Command Tracking Works Internally
// beforeCommand tracks the start time when a user command like $ is called
// with an XPath selector:
// $('//XCUIElementTypeButton[@name="Login"]')
//
// afterCommand captures the end time when the underlying findElement completes
// and stores the performance data with test context