Implementation:Webdriverio Webdriverio XPath PageSource
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, Selector_Validation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The XPath PageSource module validates selector uniqueness by counting matching elements in the page source XML using regex-based parsing.
Description
This module provides three exported functions for selector uniqueness validation. isSelectorUniqueInPageSource is the main entry point that dispatches to strategy-specific checks based on selector prefix: accessibility ID selectors (~) check both name and label attributes for uniqueness, predicate string selectors are counted via countMatchingElementsByPredicate, and class chain selectors via countMatchingElementsByClassChain. Both counting functions parse predicate conditions from selector strings and match them against element attributes in the page source XML using regex patterns. The module avoids full DOM parsing by using targeted regex matching for performance.
Usage
These functions are called by the selector builder module to verify that a proposed native selector uniquely identifies a single element before recommending it as a replacement.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/utils/xpath-page-source.ts
- Lines: 18-155
Signature
export function isSelectorUniqueInPageSource(selector: string, pageSource: string): boolean
export function countMatchingElementsByPredicate(predicateString: string, pageSource: string): number
export function countMatchingElementsByClassChain(chainString: string, pageSource: string): number
Import
import { isSelectorUniqueInPageSource, countMatchingElementsByPredicate, countMatchingElementsByClassChain } from './utils/xpath-page-source.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| selector | string |
Yes | Full selector string with strategy prefix (e.g., ~Login, -ios predicate string:..., -ios class chain:...)
|
| pageSource | string |
Yes | Raw XML page source string to search within |
| predicateString | string |
Yes | iOS predicate string without prefix (for countMatchingElementsByPredicate) |
| chainString | string |
Yes | iOS class chain string without prefix (for countMatchingElementsByClassChain) |
Outputs
| Name | Type | Description |
|---|---|---|
| isSelectorUniqueInPageSource return | boolean |
True if exactly one element matches the selector in the page source |
| countMatchingElementsByPredicate return | number |
Count of elements matching the predicate conditions |
| countMatchingElementsByClassChain return | number |
Count of elements matching the class chain pattern |
Usage Examples
Checking Selector Uniqueness
import { isSelectorUniqueInPageSource } from './xpath-page-source.js'
const pageSource = `
<AppiumAUT>
<XCUIElementTypeButton name="Login" label="Login" />
<XCUIElementTypeButton name="Cancel" label="Cancel" />
</AppiumAUT>`
isSelectorUniqueInPageSource('~Login', pageSource)
// true (only one element has name="Login")
isSelectorUniqueInPageSource('~Cancel', pageSource)
// true
isSelectorUniqueInPageSource(
"-ios predicate string:type == 'XCUIElementTypeButton'",
pageSource
)
// false (two buttons exist)
Counting Predicate Matches
import { countMatchingElementsByPredicate } from './xpath-page-source.js'
const count = countMatchingElementsByPredicate(
"type == 'XCUIElementTypeButton' AND name == 'Login'",
pageSource
)
// 1