How to use Espresso UI Testing for React Native?

笑着哭i 提交于 2019-11-30 04:53:29

Espresso

Currently there is no way to set resource-id with react-native, so, to make complex actions you need to write some code (like wait for elements), other things seems work quite good via android studio 'record espresso test' button.

  1. use prop accessibilityLabel as id for elements (eg. "elementId")
  2. use onView(allOf(withContentDescription("elementId"), isDisplayed())) to get element
  3. peform actions on that element (like element.perform(click()))

Here you can find full test https://gist.github.com/cybergrind/0a2ad855352a5cd47cb5fb6a486c5eaa

Appium

If you just want to perform actions and capture screenshots, you can do this with appium:

  1. use prop accessibilityLabel as id for elements
  2. in web driver use waitForElementByAccessibilityId
  3. capture screenshots with saveScreenshot('out.png') -> this will create 'out.png' file in directory where you've run tests

In appium you finally will have something like (js example):

driver.waitForElementByAccessibilityId('searchInputAcc', 5000)
      .type('bold\n')
      .sleep(5000)
      .saveScreenshot('out.png')

iOS vs Android accessibilityLabels

It seems that for Android you're free to use accessibiltyLabel on any element (like Text, View and so on), but iOS won't set accessibility on all elements like Adnroid.

If you set label on Text it won't be equal your label

<Text accessibilityLabel="my_text">content</Text>

will give you label equal content on iOS, so basically you can just set accessible attribute on your text nodes for this platform

<Text accessible>content</Text>

Same thing for View - iOS will ignore your labels.

So far, not much elements on iOS will work with your custom accessibility labels.

Below is a list of elements that you can use to test application for crossplatform react-native tests

You can use:

  • TouchableHighlight - will work same on iOS and Android, you can just set accessibilityLabel
  • Text - accessibilityLabel should be same as inner test + you have to set accessible attribute

Won't work (for both platforms altogether):

  • View

P.S. we haven't tested all possible elements yet, so add your results for other elements or wait for our results

Debugging

You can get source of root element, print and read it as xml for debugging purposes (for webdriver.io: http://webdriver.io/api/property/getSource.html)

I have a PR against react-native to add proper support for resource-id. Check it out and up-vote please: https://github.com/facebook/react-native/pull/9942

Once that's merged in testID will add resource-id so long as the Id is defined in res/values/ids.xml.

Currently if you set testID in your react layer that will get translated into a tag in android.

Then with Espresso you can use the provided ViewMatcher withTagValue link

Then all you have to do to find a view is

onView(withTagValue(is((Object) tagValue))).perform(click());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!