Implementation:Webdriverio Webdriverio XPath Predicate
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, iOS_Automation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The XPath Predicate module converts XPath expressions to iOS Predicate Strings and extracts Accessibility IDs from simple XPath patterns.
Description
This module provides two functions. convertXPathToPredicateString extracts the element type from an XPath (as a type == condition) and all attribute conditions, groups OR conditions, and joins them with AND to produce a complete -ios predicate string: selector. convertXPathToAccessibilityId is a simpler extraction that looks for a single @name or @label attribute value in the XPath and returns it as an accessibility ID. Both functions return null when the XPath does not match their expected patterns.
Usage
These functions are called as part of the static (non-page-source) XPath conversion pipeline to generate predicate strings and accessibility IDs from XPath structure alone.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/utils/xpath-predicate.ts
- Lines: 12-58
Signature
export function convertXPathToPredicateString(xpath: string): XPathConversionResult | null
export function convertXPathToAccessibilityId(xpath: string): string | null
Import
import { convertXPathToPredicateString, convertXPathToAccessibilityId } from './utils/xpath-predicate.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| xpath | string |
Yes | XPath expression to convert; for predicate string, should contain an element type and/or conditions; for accessibility ID, should contain a single @name or @label attribute |
Outputs
| Name | Type | Description |
|---|---|---|
| convertXPathToPredicateString return | null | Object with selector containing the -ios predicate string: prefixed value, or null if no conditions or element type found
|
| convertXPathToAccessibilityId return | null | The accessibility ID value (name or label attribute), or null if the XPath does not contain a simple @name or @label match |
Usage Examples
Converting to Predicate String
import { convertXPathToPredicateString } from './xpath-predicate.js'
const result1 = convertXPathToPredicateString('//XCUIElementTypeButton[@name="Login"]')
// { selector: "-ios predicate string:type == 'XCUIElementTypeButton' AND name == 'Login'" }
const result2 = convertXPathToPredicateString(
'//XCUIElementTypeStaticText[contains(@label, "Welcome")]'
)
// { selector: "-ios predicate string:type == 'XCUIElementTypeStaticText' AND label CONTAINS 'Welcome'" }
const result3 = convertXPathToPredicateString('//*[@name="Login"]')
// { selector: "-ios predicate string:name == 'Login'" }
Extracting Accessibility ID
import { convertXPathToAccessibilityId } from './xpath-predicate.js'
convertXPathToAccessibilityId('//XCUIElementTypeButton[@name="Login"]')
// 'Login'
convertXPathToAccessibilityId('//XCUIElementTypeStaticText[@label="Welcome"]')
// 'Welcome'
convertXPathToAccessibilityId('//XCUIElementTypeButton[contains(@name, "Log")]')
// null (contains() is not a simple match)