Home Documentation Transformations

E - Value Transformations

Transform values before applying conditions to make locators robust against casing, whitespace, and character variations.

One of the most powerful features of XPathy is the ability to transform values before applying conditions. Transformations make locators more robust against variations in casing, whitespace, special characters, and more.

1. Case Transformations

Import cases:

import static com.xpathy.Case.*;

Ignore Case

Example
XPathy locator = button.byAttribute(id)
                .withCase(IGNORED)
                .contains("login-button");
Result
//button[contains(translate(@id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'login-button')]

Use Case: When the id attribute can appear in different cases (Login-Button, LOGIN-BUTTON, etc.).

Force Uppercase

Example
XPathy locator = label.byText()
                .withCase(UPPER)
                .equals("USERNAME");
Result
//label[translate(text(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')='USERNAME']

Force Lowercase

Example
XPathy locator = div.byAttribute(class_)
                .withCase(LOWER)
                .equals("active");
Result
//div[translate(@class, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='active']

2. Whitespace Handling

Normalize Space in Text

Example
XPathy locator = div.byText()
                .withNormalizeSpace()
                .equals("Invalid password");
Result
//div[normalize-space(text())='Invalid password']

Use Case: Cleans up inconsistent spacing inside text content. Error messages sometimes appear with odd padding.

3. Character Filtering

Import filters:

import static com.xpathy.Only.*;

Keep Only

Single Filter
XPathy locator = span.byText()
                .withKeepOnly(ENGLISH_ALPHABETS)
                .contains("ProductABC");
Multiple Filters
XPathy locator = td.byText()
                .withKeepOnly(ENGLISH_ALPHABETS, NUMBERS)
                .equals("ORD1234");
Result
//td[translate(text(), translate(text(), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', ''), '')='ORD1234']

Use Case: Order IDs sometimes render as ORD-1234 or ORD 1234. This transformation makes them match as ORD1234.

Remove Only

Example
XPathy locator = span.byText()
                .withRemoveOnly(SPECIAL_CHARACTERS)
                .contains("1999");

Use Case: Prices appear as $1,999 or €1.999. After removing special characters, you can reliably match 1999.

4. Character Translation

Example
XPathy locator = h1.byText()
                .withTranslate("éàè", "eae")
                .contains("Cafe");
Result
//h1[contains(translate(text(), 'éàè', 'eae'), 'Cafe')]

Use Case: The UI shows Café, Cafè, or Càfe. Translating accents ensures all match Cafe.

5. Combining Multiple Transformations

Transformations can be stacked in sequence and are applied in order.

Example
XPathy locator = div.byText()
                .withNormalizeSpace()
                .withRemoveOnly(NUMBERS)
                .withTranslate("éàè", "eae")
                .withCase(IGNORED)
                .contains("premium cafe");

Use Case: Product title in UI appears as " Prémium Café 2024 " or "PREMIUM CAFE". This locator still matches it reliably.

Key Benefit: Transformations are the critical differentiator in XPathy. They eliminate brittle locators by normalizing casing, whitespace, characters, and formatting differences automatically.