Appium 1.6.0
Is there a way to directly convert Xpaths in UIAAutomation framework to XCUITest framework?
Appium 1.6.0
Is there a way to directly convert Xpaths in UIAAutomation framework to XCUITest framework?
Currently, it looks like Appium 1.6.x does a pretty decent job of converting them for you. But, there were a few occasions where it didn’t work for me, and I wanted to retain compatibility with older version of iOS xpaths in my code still. So luckily, my appium framework is defined with a common class at it’s base for all controls. So I added a method to do the conversion that uses an ugly replace() action for xpaths and classNames…
private By convertForXCUITest(By iosLocator) {
String[] parts = iosLocator.toString().split(": ");
if (parts.length > 1) {
String newValue = parts[1].
replace("UIAStaticText", "XCUIElementTypeStaticText").
replace("UIAButton", "XCUIElementTypeButton").
replace("UIATableCell", "XCUIElementTypeCell").
replace("UIANavigationBar", "XCUIElementTypeNavigationBar").
replace("UIATextField", "XCUIElementTypeTextField").
replace("UIAActivityIndicator", "XCUIElementTypeActivityIndicator").
replace("UIASecureTextField", "XCUIElementTypeSecureTextField").
replace("UIACollectionView", "XCUIElementTypeCollection").
replace("UIACollectionCell", "XCUIElementTypeCell").
trim();
switch (parts[0]) {
case "By.xpath":
return By.xpath(newValue);
case "By.className":
return By.className(newValue);
default:
return iosLocator;
}
} else {
return iosLocator;
}
}
An update for anyone finding this thread now: UIAutomation is long gone, but the conversion question is still common. Today’s version is turning XPath locators into the faster -ios class chain strategy of the XCUITest driver.
There is now an open-source tool for that, which I wrote: xpath-to-class-chain. It scans your test code (JS/TS, Java, Kotlin, Python, Ruby, C#…), converts each XPath that has an exact class chain, warns on approximations, and skips the rest with a reason. It made no wrong chains on 678 real and edge-case locators checked against WDA’s own code, a simulator and an iPhone.
npx xpath-to-class-chain ./tests # preview the diff
npx xpath-to-class-chain ./tests --write # apply it
I also compared it with asking an LLM (Claude) to convert the same locators: results.