Element's state coming from XPath vs iOS predicate string

Hello

Not sure if this is a bug or not, but that’s the behavior on iOS:

Scenario 1 - runs fine

element = driver.findElementByXpath("//*[@value == ‘test’]")
print(element.text) // prints test
element.sendKeys(“test2”)
print(element.text) // prints test2

Scenario 2 - fails

element = driver.findElementBy(MobileBy.iOSNsPredicateString(“value == ‘test’”))
print(element.text) // prints test
element.sendKeys(“test2”)
print(element.text) // throws the exception ‘The previously found element “test” is not present in the current view anymore’

Questions:

  1. Is this a bug?
  2. If I use other iOS specific locators such as class chain, will it behave the same? Btw, how to convert this locator to ios class chain? I couldn’t search without specifying and element
  3. If I use UiSelector() on Android, will it behave the same?

I don’t think there is a proper answer to this question. Basically, the way elements are cached and resolved depends on their internal representation. XCTest’s XCUIElement, whose instances we keep in the cache, only keeps the actual element’s locator inside. So, when it gets resolved then XCTest actually tries to execute the same locator on the actual/current source tree and that is why the scenario 2 fails. It would also fail for any other native-based locator, except for xpath one.

xpath is special, because it is not natively supported by XCTest, so in order to cache found element(s) we need to map the original xpath query to xctest-compatible one. This is done by fetching the unique element’s identifier. So, basically, when the element found by xpath is cached, we don’t store the original query with it, but just the internal element identifier. That is why the first scenario passes, - element identifier is still unchanged even though its value has been changed.

Theoretically it is possible to align both approaches, but this would break the legacy behaviour that was initially introduced to WebDriverAgent by Facebook and could also fail existing automated tests.

Or… there could be a new capability that, when set to true, would align native and non-native locators to behave the same (xpath behaviour). Do you think it could be launched for future versions?

You could try if https://github.com/appium/WebDriverAgent/pull/516 works for you

1 Like