samzmann
samzmann

Reputation: 2566

Appium element.text() returns element accessibilityLabel instead of text

I am starting to test my React Native app with Appium. I have a simple login scenario where I expect the app status to be 'Logged in' after some username and password are entered and submitted. I am running the test on an iPhone X 12.2 simulator.

However the test fails with this error:

Expected: "Logged in"
Received: "status"

Somehow the text value is not received correctly. So how to get the inner text of my element?

App.js:

<Text accessibilityLabel="status">{this.state.status}</Text>

appium.test.js:

test('Login success', async() => {
    expect(await driver.hasElementByAccessibilityId('username input')).toBe(true)
    expect(await driver.hasElementByAccessibilityId('password input')).toBe(true)
    expect(await driver.hasElementByAccessibilityId('submit button')).toBe(true)
    expect(await driver.hasElementByAccessibilityId('status')).toBe(true)

    await driver.elementByAccessibilityId('username input').sendKeys('some_username')
    await driver.elementByAccessibilityId('password input').sendKeys('some_password')

    await driver.elementByAccessibilityId('submit button').click()

    const result = await driver.elementByAccessibilityId('status').text()

    console.log(result) // 'status' WHY???

    // the test runs fine until here:

    expect(result).toBe('Logged in')
})

All I can think of is that text() is not the right function to get the inner text of my element, but this is all I see in the docs or tutorials I've been following...

Upvotes: 4

Views: 1662

Answers (1)

dfranca
dfranca

Reputation: 5322

I got in the same issue. It seems that RN0.50 introduced this issue.

It's not a bug on Appium, but a problem with WDA implementation and how it is returning the text value. https://github.com/appium/appium/issues/10349 there's a discussion about this problem.

You can solve it on IOS using testID instead of accessibilityLabel.

let textProps={}
if(Platform.OS==='android')
  textProps.accessibilityLabel = "welcomeLabel"
    ...
    ...
    ...
    <Text
          style={styles.welcome}
          {...textProps}
          testID="welcomeLabel">
   ...
   ...

You can still use elementByAccessibilityId function.

Upvotes: 5

Related Questions