2

I have simple XCUIElementTypeStaticText in my iOS app, but getText() doesn't work as expected it doesn't return text inside. Any tips&tricks or workarounds?

  • Appium 2
  • WebdriveriO - 8.3.2
  • iOS: 15
  • Node: v16.17.0

code:

const reportTitleName = await driver.$('~reportTitleName')
const reportName = await reportTitleName.getText() // not working
const reportName = await reportTitleName.getAttribute('value') // not working by any attribute: value or label

console.log('report name is: ', reportName ) // will print reportTitleName not content

Appium inspector: enter image description here

Babu
  • 4,324
  • 6
  • 41
  • 60

2 Answers2

1

Well ...I had more issues to solve to make it work ... it was "only" this:

  • 1. increase snapshotMaxDepth so all elements are returned, default is 50
    • add to capability row: appium:settings[snapshotMaxDepth]: 80
  • 2. switch off animations (we had idle animation)
    • I was getting this error and timeouts: Waiting up to 2s until com.myap is in idle state (including animations)

3. do NOT add accessibilityLabel directly to <Text> component

if you do this:

 <View>
   <Text accessibilityLabel="testID">TEXT_I_WANT</Text>
 </View>

snapshot will look somehow like this:

<XCUIElementTypeOther ...>
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="testID" name="testID" label="testID"/>
</XCUIElementTypeOther>

instead, add it to the parent component:

 <View accessibilityLabel="testID">
   <Text>TEXT_I_WANT</Text>
 </View>

snapshot:

<XCUIElementTypeOther type="XCUIElementTypeOther" name="testID" label="testID">
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="TEXT_I_WANT" name="TEXT_I_WANT" label="TEXT_I_WANT"/>
</XCUIElementTypeOther>

After that, I had to get text from component by additional call:

const element = await driver.$('~testID')
const elems = await element.$$('XCUIElementTypeStaticText')
const myTextElement = await elems[0].getText()
console.log(myTextElement)

What helped me to find such an easy way was to try all hints from this Appium issue thread: App element tree (on IOS) not looking good #10654.

NOTE: still open to better solution

Babu
  • 4,324
  • 6
  • 41
  • 60
  • #3 on this answer did the trick for me. I had the accessibility label on the component which was overwriting the accessible value. – Tristan Heilman Aug 16 '23 at 19:57
0

The tilde in ~ ('~reportTitleName') is used for finding elements by their accessibility id, which if set for the element, will appear above the ios class chain locator in Appium Inspector.

Try using driver.$('-ios predicate string:label == "reportTitleName"'). If there are other elements on the page that use this, you can narrow down the selection by adding the AND value == "reportTitleName" AND name == "reportTitleName" parameters.

  • ...but do you know how to get the text value from this element? This seems just to select the `reportTitleName` but not the text which is displayed to user – Babu Apr 12 '23 at 14:51