Implementation:Webdriverio Webdriverio Selector Testing
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, Selector_Validation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The Selector Testing module tests optimized selectors against live browser elements to verify they find the correct elements and measure their performance.
Description
The testOptimizedSelector function calls the browser's findElement or findElements protocol method using the provided strategy (accessibility id, -ios predicate string, or -ios class chain) and value. It measures the execution duration via high-resolution timing and returns element references with timing data. In debug mode, the function provides extensive step-by-step logging, including page source analysis when elements are not found and automatic retry logic. An internal helper extractMatchingElementsFromPageSource searches the raw page source XML using regex patterns to identify potential matches for debugging purposes.
Usage
This function is called by the optimizer module's step 4 to verify that a proposed optimized selector actually finds the target element(s) before recommending the replacement.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/utils/selector-testing.ts
- Lines: 112-247
Signature
export async function testOptimizedSelector(
browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser,
using: string,
value: string,
isMultiple: boolean,
debug?: boolean
): Promise<{ elementRefs: Array<{ [key: string]: string }>, duration: number } | null>
Import
import { testOptimizedSelector } from './utils/selector-testing.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| browser | WebdriverIO.MultiRemoteBrowser | Yes | Browser instance with findElement/findElements protocol methods |
| using | string |
Yes | Locator strategy (e.g., 'accessibility id', '-ios predicate string', '-ios class chain') |
| value | string |
Yes | Selector value for the given strategy |
| isMultiple | boolean |
Yes | If true, uses findElements; if false, uses findElement |
| debug | boolean |
No | Enables detailed step-by-step debug logging and retry logic; defaults to false |
Outputs
| Name | Type | Description |
|---|---|---|
| return | null> | Object containing an array of element reference objects and the execution duration in milliseconds; returns null if an exception occurs during element finding |
Usage Examples
Testing an Accessibility ID Selector
import { testOptimizedSelector } from './selector-testing.js'
const result = await testOptimizedSelector(
browser,
'accessibility id',
'Login',
false, // single element
false // no debug
)
if (result && result.elementRefs.length > 0) {
console.log(`Found element in ${result.duration.toFixed(2)}ms`)
} else {
console.log('Element not found with optimized selector')
}
Testing a Predicate String for Multiple Elements
const result = await testOptimizedSelector(
browser,
'-ios predicate string',
"type == 'XCUIElementTypeCell' AND visible == 'true'",
true, // multiple elements
true // enable debug logging
)
if (result) {
console.log(`Found ${result.elementRefs.length} element(s) in ${result.duration.toFixed(2)}ms`)
}