Run your Espresso Tests on Firebase TestLabs

Krishna Chaitanya
4 min readJan 22, 2018

--

Requirements:

  1. Android Studio
  2. Firebase Account

Espresso Basics:

The Espresso testing framework, provided by the Android Testing Support Library, provides APIs for writing UI tests to simulate user interactions within a single target app. Espresso tests can run on devices running Android 2.3.3 (API level 10) and higher. 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.

API Components:

  1. Espresso — Entry point to interactions with views (via onView() and onData()). Also exposes APIs that are not necessarily tied to any view, such as pressBack().
  2. ViewMatchers — A collection of objects that implement the Matcher<? super View> interface. You can pass one or more of these to the onView() method to locate a view within the current view hierarchy.
  3. ViewActions — A collection of ViewAction objects that can be passed to the ViewInteraction.perform() method, such as click().
  4. ViewAssertions — A collection of ViewAssertion objects that can be passed the ViewInteraction.check() method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.

Procedure:

  1. Download and install Android studio.
  2. Download sample Android project (Source Code)
  3. Open the project in Android Studio
  4. Select “Run” from the Main Menu and select “Record Espresso Test
  1. Select the device on which you want to run the .apk file, if there is no device connected select an emulator as Deployment Target.

2. This will launch both the Recorder and Emulator.

3. You can view that the actions performed on the emulator are captured on the Recorder.

4. You can also add assertions to your test case by selecting the “Add Assertion”

  1. Once the recording is completed, select OK and save the testFile
  2. Right click on the recorded class from the project window and Run the file
  3. Select the Device/Emulator
  4. This will launch the app on the device and will execute the recoded test cases.

Procedure to run the Instrumented tests on Firebase:

  1. Create an account in Firebase
  2. From Android Studio, Select “Run” and Select “Edit Configurations”
  3. Select “Add New Configuration” and select “Android Instrumented Tests”
  4. Select Module as the “App”
  5. Select Deployment Target as “Firebase Testlab Device Matrix” from the dropdown options
  6. Select the matrix configuration which includes the firebase plan and the devices in which you want to test your app.
  7. Select the project in which you want to run the tests or you can also create a new project
  8. Apply the selected configuration and select OK.

9. Select the Firebase Configuration and Run the “Tests”

10. You can check the progress and the result in the firebase dashboard

Conclusion:

Test Lab lets you run Espresso tests written to exercise your app from the Firebase console, Android Studio on multiple devices.

--

--