Android Testing: UIAutomator vs Espresso

后端 未结 7 1267
醉酒成梦
醉酒成梦 2020-12-04 15:32

I was seeking for test frameworks for Android UI automation and I stumbled upon UI Automator and Espresso and this is the part I am confused about

相关标签:
7条回答
  • 2020-12-04 15:34

    Actually, you don't need to choose. UIAutomator and Espresso use the same instrumentation runner, so you can use commands from both of them in a single test. Espresso has variety of matchers, assertions and actions on UI, that automator doesn't have:

    Espresso Cheat Sheet

    Espresso is running in separate thread and it is really fast comparing to other test frameworks.

    as Summary: use both of them in your tests, Espresso is main and UIAutomator as an additional tool for OS integration.

    0 讨论(0)
  • 2020-12-04 15:34

    To get a quick notion how both things work let's give an example. Let's try to find and click a button with title "Start" on Lollipop using UIAutomator and Espresso:

    • UIAutomator: You have to search for uppercase "START" because on Lollipop buttons are rendered uppercase. device.findObject(new UiSelector().text("START")).click();
    • Espresso: You would just use R.string.start and wouldn't need to care how the string is actually rendered by the platform. You don't care if the view has textAllCaps=true or it is ellipsized. onView(withText(R.string.start)).perform(click());

    TL:DR;

    UIAutomator searches views in lower-level style than Espresso - via the Instrumentation mechanism and traversing the AccessibilityNodeInfo tree of the view hierarchy. Espresso on its turn typically traverses the view hierarchy itself.

    0 讨论(0)
  • 2020-12-04 15:35

    I've found an interesting article, which talks about why you should use them both. Take a look at:

    http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

    Already Espresso is enough for me, but for some reasons like testing app notifications I would in just a few weeks learn uiautomator.

    Spend some time to

    • check Google's examples of using these frameworks:

    https://github.com/googlesamples/android-testing/tree/master/ui

    • read a documentation of these frameworks:

    http://developer.android.com/training/testing/ui-testing/espresso-testing.html

    http://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

    0 讨论(0)
  • 2020-12-04 15:40

    If you are testing only one application, then Espresso.

    If you are testing more than one application or its integration with other applications or system, then UiAutomator.

    0 讨论(0)
  • 2020-12-04 15:40

    A key benefit of using Espresso is that it provides automatic synchronization of test actions with the UI of the app you are testing. Espresso detects when the main thread is idle, so it is able to run your test commands at the appropriate time, improving the reliability of your tests. This capability also relieves you from having to add any timing workarounds, such as Thread.sleep() in your test code.

    The Espresso testing framework is an instrumentation-based API and works with the AndroidJUnitRunner test runner.

    Source: https://developer.android.com/training/testing/ui-testing/espresso-testing.html

    0 讨论(0)
  • 2020-12-04 15:47

    When using UIAutomator you must use an 18+ API while with Espresso, you can go as low as API 8.

    As the Android documentation states, multi-application tests should be done with UIAutomator while if you only want the tests to run inside of your own app package you can use Espresso.

    At the bottom of this documentation's page, you can find the introduction, as well as examples to both of these testing styles.

    0 讨论(0)
提交回复
热议问题