Implementation:Webdriverio Webdriverio Selector Location
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, Source_Analysis |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The Selector Location module locates XPath selector definitions in test files and page object files to provide source references in performance reports.
Description
The findSelectorLocation function searches for a given XPath selector string across test files and page objects. It first checks the test file directly, then searches configured page object directories (or falls back to heuristic discovery based on naming conventions). The search handles selectors wrapped in single quotes, double quotes, and template literals. Internal helpers include findSelectorInFile for line-by-line string matching, findPotentialPageObjects for heuristic page object discovery (supporting common patterns like pageobjects/, pages/, page-objects/), findFilesInDirectory for recursive file traversal, and findPageObjectFilesFromConfig for searching explicitly configured paths.
Usage
This function is called by the optimizer and the MSPO service to determine the file path and line number where an XPath selector is defined, enabling actionable source references in reports.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/utils/selector-location.ts
- Lines: 171-243
Signature
export function findSelectorLocation(
testFile: string | undefined,
selector: string,
pageObjectPaths?: string[]
): SelectorLocation[]
Import
import { findSelectorLocation } from './utils/selector-location.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| testFile | undefined | No | Path to the current test file; if undefined, an empty array is returned |
| selector | string |
Yes | The XPath selector string to search for; non-XPath selectors are skipped |
| pageObjectPaths | string[] |
No | Configured directories to search for page objects; if not provided, heuristic discovery is used |
Outputs
| Name | Type | Description |
|---|---|---|
| return | SelectorLocation[] |
Array of locations, each with file (string path), line (number), and isPageObject (boolean) indicating whether the match was in a page object file
|
Usage Examples
Finding Selector Location with Configured Paths
import { findSelectorLocation } from './selector-location.js'
const locations = findSelectorLocation(
'tests/specs/login.spec.ts',
'//XCUIElementTypeButton[@name="Login"]',
['./tests/pageobjects']
)
// Result:
// [
// { file: 'tests/pageobjects/login.page.ts', line: 15, isPageObject: true }
// ]
Finding Selector with Heuristic Discovery
// Without pageObjectPaths, it searches common patterns:
// tests/pageobjects/login.page.ts
// tests/pages/login.page.ts
// tests/page-objects/login.page.ts
const locations = findSelectorLocation(
'tests/specs/login.spec.ts',
'//XCUIElementTypeTextField[@name="username"]'
)