Implementation:Webdriverio Webdriverio XPath Conditions
| Knowledge Sources | |
|---|---|
| Domains | Mobile_Testing, XPath_Processing |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The XPath Conditions module extracts conditions from XPath expressions and converts them to iOS predicate and class chain syntax.
Description
This module provides three exported functions. extractXPathConditions parses an XPath expression's bracket content and extracts conditions including simple attribute comparisons (@attr="value"), OR conditions, contains(), starts-with(), ends-with(), text() comparisons, and substring() operations. convertConditionToPredicate transforms a single XPathCondition into NSPredicate or class chain syntax with configurable quote styles. groupOrConditions aggregates OR conditions on the same attribute into grouped predicate expressions while keeping regular conditions separate.
Usage
These functions are used by the predicate string converter, class chain converter, and selector builder modules to transform XPath condition nodes into native iOS selector syntax.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-appium-service/src/mobileSelectorPerformanceOptimizer/utils/xpath-conditions.ts
- Lines: 10-300
Signature
export function extractXPathConditions(xpath: string): XPathCondition[]
export function convertConditionToPredicate(
condition: XPathCondition,
quoteStyle?: 'single' | 'double'
): string
export function groupOrConditions(
conditions: XPathCondition[],
quoteStyle?: 'single' | 'double'
): string[]
Import
import { extractXPathConditions, convertConditionToPredicate, groupOrConditions } from './utils/xpath-conditions.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| xpath | string |
Yes | XPath expression to extract conditions from (for extractXPathConditions) |
| condition | XPathCondition |
Yes | A single condition with attribute, operator, value, and optional logicalOp (for convertConditionToPredicate) |
| conditions | XPathCondition[] |
Yes | Array of conditions to group (for groupOrConditions) |
| quoteStyle | 'double' | No | Quote style: 'single' for predicate strings, 'double' for class chain; defaults to 'single' |
Outputs
| Name | Type | Description |
|---|---|---|
| extractXPathConditions return | XPathCondition[] |
Array of extracted conditions with attribute, operator (=, contains, beginswith, endswith), value, and optional logicalOp ('OR') |
| convertConditionToPredicate return | string |
Predicate string like name == 'Login' or label CONTAINS 'Submit'
|
| groupOrConditions return | string[] |
Array of grouped condition strings; OR conditions are wrapped in parentheses |
Usage Examples
Extracting Conditions from XPath
import { extractXPathConditions } from './xpath-conditions.js'
// Simple attribute condition
const conditions1 = extractXPathConditions('//XCUIElementTypeButton[@name="Login"]')
// [{ attribute: 'name', operator: '=', value: 'Login' }]
// OR conditions
const conditions2 = extractXPathConditions('//XCUIElementTypeButton[@name="OK" or @name="Cancel"]')
// [{ attribute: 'name', operator: '=', value: 'OK', logicalOp: 'OR' },
// { attribute: 'name', operator: '=', value: 'Cancel', logicalOp: 'OR' }]
// contains() function
const conditions3 = extractXPathConditions('//XCUIElementTypeStaticText[contains(@label, "Welcome")]')
// [{ attribute: 'label', operator: 'contains', value: 'Welcome' }]
Converting to Predicate Syntax
import { convertConditionToPredicate, groupOrConditions } from './xpath-conditions.js'
const condition = { attribute: 'name', operator: '=', value: 'Login' }
const predicate = convertConditionToPredicate(condition, 'single')
// "name == 'Login'"
const classChainPredicate = convertConditionToPredicate(condition, 'double')
// 'name == "Login"'
const orConditions = [
{ attribute: 'name', operator: '=', value: 'OK', logicalOp: 'OR' as const },
{ attribute: 'name', operator: '=', value: 'Cancel', logicalOp: 'OR' as const }
]
const grouped = groupOrConditions(orConditions, 'single')
// ["(name == 'OK' OR name == 'Cancel')"]