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
XPathy locator = button.byAttribute(id)
.withCase(IGNORED)
.contains("login-button");
//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
XPathy locator = label.byText()
.withCase(UPPER)
.equals("USERNAME");
//label[translate(text(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')='USERNAME']
Force Lowercase
XPathy locator = div.byAttribute(class_)
.withCase(LOWER)
.equals("active");
//div[translate(@class, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='active']
2. Whitespace Handling
Normalize Space in Text
XPathy locator = div.byText()
.withNormalizeSpace()
.equals("Invalid password");
//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
XPathy locator = span.byText()
.withKeepOnly(ENGLISH_ALPHABETS)
.contains("ProductABC");
XPathy locator = td.byText()
.withKeepOnly(ENGLISH_ALPHABETS, NUMBERS)
.equals("ORD1234");
//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
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
XPathy locator = h1.byText()
.withTranslate("éàè", "eae")
.contains("Cafe");
//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.
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.