Xpath-to-class-chain: convert iOS XPath locators to class chain

If you have a lot of //XCUIElementType… XPath locators in your iOS tests and want to move them to -ios class chain, I made an open-source tool for it. It rewrites a locator only when a class chain finds the same elements. Every other locator stays as it is and gets a reason.

npx xpath-to-class-chain ./tests            # preview the diff, writes nothing
npx xpath-to-class-chain ./tests --write    # save the changes
- //XCUIElementTypeStaticText[@name="Order total"]
+ -ios class chain:**/XCUIElementTypeStaticText[`name == "Order total"`]

- //*[contains(@type,"XCUIElementTypeButton") and contains(@label,"Add to cart")]
+ -ios class chain:**/XCUIElementTypeButton[`label CONTAINS "Add to cart"`]

[skip] skipped_unsupported_logic: //*[@name="Total:"]/following-sibling::*[1]

It scans JS/TS, Java, Kotlin, Python, Ruby, C#, PHP, JSON and YAML. Strings passed to an explicit XPath strategy (AppiumBy.xpath(…), @iOSXCUITFindBy(xpath = …), find_element(AppiumBy.XPATH, …)) are left alone, because changing only the string would break the locator. Needs Node.js 20+, no dependencies.

How it was checked

  • 678 locators: 563 from real test suites, 115 edge cases.
  • Each XPath and its chain were run against the same page source in WebDriverAgent’s own parser code, then on an iOS simulator and an iPhone.
  • 489 of the 563 real locators have a class chain, and the converter made 0 wrong chains.
  • Checked with Appium 3.8.0, appium-xcuitest-driver 12.16.0, WDA 16.14.1, on iOS 26.6 and 27.0. Older versions were not checked.

Is class chain faster?

On big screens it is: 22–29 % faster per lookup with 100–2000 elements in the tree. On small screens the gain is only a few milliseconds. Before you convert, check visible predicates and lookups that return dozens of elements, because those can be slower as a chain.

Known limit: (//X)[2] has only an approximation. WDA counts class chain matches breadth-first and XPath in document order, so the converter converts it but prints a warning.

Links:

If you run it on your project, I’d like to know which locators it skipped that you think should convert, or any chain that found the wrong element. Issues on GitHub or replies here are both fine.