Introduction to Android UI Test Automation
By Neha Vaidya, Community Contributor - September 21, 2021
User Interface (UI) testing ensures that an application meets its functional requirements and achieves the high-quality levels required for it to be successfully adopted by users.
One approach to UI testing is to have a human tester perform a set of user actions on the target app and verify that it is behaving correctly. However, this is tiresome, time-consuming, and prone to error.
A more efficient approach is to automate UI tests by replicating user actions via an automation framework. The automated approach allows QAs to run tests quickly and reliably as many times as they require.
This article will explore the process of running automated UI tests on Android apps and the tools useful in executing the same.
What is Android UI Testing?
UI Testing of an Android app is essential to ensure that no part of the app malfunctions. Android UI Testing must verify the functions, behaviors, correctness, and usefulness of an Android application.
Advantages of automating UI tests are as follows:
- Test scripts once created can be reused; this makes testing easily scalable
- Improves performance as efficient test scripts deliver fast and accurate results
- Simplified testing by letting QAs refer to previous builds/test results and proceed further
- Machines are less prone to errors than humans, which leads to a higher likelihood of accurate results even with a large number of tests
Popular Android UI testing tools
A few commonly used tools for Android app testing include:
- Espresso
- Detox
- UI Automator
- Calabash
Espresso
Espresso is an Android test automation framework open-sourced by Google. Its aim is to provide testers and developers with a user-friendly UI. Espresso uses an API that is small, predictable, easy to learn. It is built on top of the Android instrumentation framework.
Advantages of Espresso
- Easy to set up
- Highly stable test cycle
- Supports testing activities outside application as well
- Supports JUnit4
- Suitable for writing black-box tests
Limitations of Espresso
- Restricted support for programming languages. The scripts are mainly written in Java and Kotlin and don’t support other languages.
- Only compatible with Android UI testing
Use case scenario:
In the example below, the code intends to display the message Hello by locating the relevant element using the Id locator and performing the steps for the main activity.
// MainActivityInstrumentationTest.java import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; // Tests for MainActivity public class MainActivityInstrumentationTest { @Rule public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class); // Looks for an EditText with id = "id.tInput" // Types the text "Hello" into the EditText // Verifies the EditText has text "Hello" @Test public void validateEditText() { onView(withId(id.tInput)).perform(typeText("Hello")).check(matches(withText("Hello"))); } }
Detox
Detox is a JavaScript mobile testing framework that is built into the application in order to allow test execution to begin when the app is launched.
Detox offers some great features:
- It monitors the asynchronous operations in the application and reduces flakiness involved in finding async elements in the app UI
- It is a grey-box testing tool that allows users to access code and data from mobile apps
- It can be easily connected to any CI system and popular cloud testing services
- It offers fast feedback on E2E tests
- React Native developers prefer Detox as it is faster, debuggable, and can be used with any test runner like Jest or Mocha
Advantages of Detox
- Integrable into apps written with React Native
Limitations of Detox
- It doesn’t support real-time testing for iOS
- It doesn’t support Webapp Views and hybrid applications
- The features for test reporting and screenshots requires improvement
Use case scenario:
In the code below, the intention is to display the message Hello. The script demonstrated how to display the message by calling all the necessary steps before that action.
It denotes that the program should wait for the method, locate the welcome screen, display the message and then display the detox screen.
describe('HelloTest', () => { beforeEach(async () => { await device.reloadReactNative(); }); it('should have welcome screen', async () => { await expect(element(by.id('welcome'))).toBeVisible(); }); it('should display hello after tap', async () => { await element(by.id('hello_react')).tap(); await expect(element(by.text('React!!!'))).toBeVisible(); }); it('should display Detox screen after tap', async () => { await element(by.id('detox_button')).tap(); await expect(element(by.text('Detox!!!'))).toBeVisible(); }); });
Calabash
Calabash is a cross-platform automation framework for Android, iOS native and hybrid applications. It has an easy-to-understand syntax that allows individuals without technical expertise to create and execute automated acceptance tests for apps on Android and iOS.
These tests are described in Cucumber similar to the Gherkin language. They are then converted to Robotium or Frank in the run time environment – a process that enables simple understanding.
Note: After delivering support for the final releases of iOS 11 and Android 8 operating systems, Microsoft will discontinue its contributions to developing Calabash.
Advantages of Calabash
- It helps increase productivity
- Improves robustness of processes and product
- Offers increased consistency of output
- Reduces efforts and expenses due to its cross-platform relevance
Limitations of Calabash
- Debugging test scripts is a major issue
- Test maintenance is costly in the case of playback methods
- Maintenance of test data files is difficult if there are more test screens to deal with
- No more development to support the latest OS
Use Case Scenario:
The code below resembles the Cucumber Gherkin language text used by Calabash. It instructs the system to login using the username and password and then view the menu compose tweet.
Feature: Login feature Scenario: As a authenticated user I can log into my app And wait for the text "Hi, How are you?" Then I press view with id "Sign in" Then I enter text "username" into "login_username" Then I enter text "password" into "login_password" Then I wait for activity "HomeTab" Then I press view with id "menu_compose_tweet" Then I enter text "Bitbar" into field with id "edit"
UI Automator
UI Automator enables UI testing of Android applications and games. With Google’s test framework, users can test the user interface (UI) of native Android apps on more than one device.
It executes JUnit test cases with special privileges and also offers different classes for developers to use, including:
com.android.uiautomator.core.UiCollection; com.android.uiautomator.core.UiConfigurator; com.android.uiautomator.core.UiObject; com.android.uiautomator.core.UiScrollable; com.android.uiautomator.core.UiSelector
The significance of the above classes according to the official docs are:
- UiCollection: Enumerates a container’s UI elements in order to count or target sub-elements by their visible text or content-description property
- UiObject: Represents a UI element that is visible on the device
- UiScrollable: Provides support to search for items in a scrollable UI container
- UiSelector: Represents a query for one or more target UI elements on a device
- Configurator: Allows users to set core parameters for running the UIAutomator test
Advantages of UIAutomator:
- It is easy to setup and use
- Interaction with the system components is easy
- Easy to work with UI elements directly
Limitations of UIAutomator:
- It supports only Java/Kotlin
- Web view is not supported
- Requires Android 4.3 and above, SDK version 21 or above, and API version 16 or higher.
- Working with lists using the API is a complicated process
Like any other software test, UI tests are best executed on real browsers, devices and operating systems. Testing in real user conditions allows the tester to see exactly how the website or app will behave when being used by actual customers. If testers do not have access to an in-house device lab, they can simply use a real device cloud to run the tests.
BrowserStack offers 3000+ real browsers and devices for manual and automation testing. QAs can test Android apps on thousands of real mobile devices, both latest and legacy. They can integrate seamlessly with the BrowserStack cloud via numerous testing frameworks – Appium, Espresso, XCUITest, or EarlGrey.
Testers can execute Android UI Test automation in order to verify multiple native device features – geolocation testing, push notifications, preloaded images, network simulation, in-app purchase, time zones, languages, etc.
Simply sign up for free, select the device-operating combination required and start testing. The entire process is built to provide convenience, efficiency, speed, and accuracy to testers and developers alike. With a wide range of integrations and debugging tools, BrowserStack is built to not just identify bugs, but resolve them at the quickest.