How do we find an iOS native app element with variable text and no xpath?

My environment is:
iOS Native App
Appium inspector
Ruby

  1. Accessibility ID, Name, Value, etc all have the same text: “Invalid username or password. Reference ID: xxxxx” The reference ID changes for every launch
  2. The point is not to use xpath
  3. Need to search on the static text

Here is the rub. I don’t really want to use an element type. I just want to see if the alert launches and text is present so that when the element values change (which they will) my test won’t break unless the text of the message changes.

use xpath as: //*[contains(@name,‘Your Text’)]

No xpath. Assume xpath doesn’t exist for the purpose of this question because it doesn’t for me. I had an xpath solution in less than a minute but I can’t use xpath and no element name. I want to find that the alert launched with a partial text value without using a specific element.

Hi,

Can you place a screen and what exactly you want to query? name, label, value?

This is it: “Invalid username or password. Reference ID:" There is a variable reference ID as part of the message that changes on every launch so you can only use the static text.

You would have to search for that string and verify it is displayed as part of an alert without using any additional information except the partial text in the message. No elements names and no xpath. Accessibility ID, Name, Value, Label all have the exact same text. I’m not seeing how to do it with those constraints.

Hello,

Predicate string could be an option, below is java code for same (Sorry, not familiar with Ruby, believe same would be there)

driver.findElement(MobileBy.iOSNsPredicateString(“propertyName CONTAINS propertyValue”));

"propertyName" can be name, value etc, and “propertyValue” would be corresponding value.

predicate matcher could be CONTAINS, ENDSWITH, BEGINSWITH, LIKE and many more

more info: https://github.com/facebook/WebDriverAgent/wiki/Predicate-Queries-Construction-Rules

2 Likes

@in-ishan - Thanks. I’ll look into this option.

Yeah I also referred you the same as @in-ishan , you can use xpath axes.
one more thing, if you don’t care about the text, you just have to check that alert is present with the text or not, so you can make a xpath with the class of text under the class of Alert.

Works perfect for me! Thanks.

For Android, try below:

  • //android.widget.TextView[@text="Your Text"]
  • //*[@text="Your Text"]
  • //android.widget.TextView[contains(.,"Your Text")]

For iOS, try below:

  • //XCUIElementTypeStaticText[@name="Your Text"]
  • //*[contains(.,"Your Text")]