App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Android App Automation using UIAutomator

Android App Automation using UIAutomator

By Neha Vaidya, Community Contributor -

Application testing is an integral part when it comes to app development. This helps to ensure the functional behavior, user experience, and many other factors of the application are operating as expected. Android is a widely used operating system for mobile applications with a market share of over 71%, hence it becomes essential to test Android apps thoroughly.

This article discusses how to perform Android app automation using the UIAutomator tool. 

Introduction to Android UI Automator Framework

UI Automator is a framework suitable for cross-app functional UI testing across the installed apps. The APIs of UIAutomator lets you interact with visible elements on a device, regardless of which Android Activity is in focus. Thus, this lets you perform operations like opening the Settings Menu or the App Launcher in a test device.

UI Automator doesn’t need access to the source code of the application in order to work. Due to this, the script can navigate and interact with the application tray, settings application, third-party applications, or any other app you want to.

The key features of the UIAutomator testing framework are as follows:

  • An API to retrieve the state information and perform necessary operations on the target device.
  • APIs which support cross-app UI testing.

UI Automator APIs in Android

With UIAutomator APIs, you can write robust tests without knowing the implementation details of the app that you are testing. Use the below set of APIs to capture UI components across multiple apps:

  • UiCollection: This API helps to enumerate a container’s UI elements for counting or targeting sub-elements by their visible text or content-description property.
  • UiObject: Represents a visible UI element on the device.
  • UiScrollable: Searches for items in a scrollable UI container.
  • UiSelector: Used to query more than one target UI element on a device.
  • Configurator: Let you set the key parameters for UIAutomator tests that are running.

Now that you know what UIAutomator is and its various APIs, let’s understand how to perform Android App Automation using UIAutomator.

Test Android Apps using Appium

Android App Automation using UIAutomator

Before starting with creating a test with Android UI Automator, configure the test source code location and project dependencies.

Step 1: In the build.gradle file of your Android app module, set a dependency reference to the UIAutomator library by using the below code.

android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
...
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile 'com.android.support.test:runner:0.5'
}

Read More about AndroidJUnitRunner

Step 2: Ensure that the project compiles correctly. After that, add a new class in module/src/androidTest: using the code below.

@RunWith(AndroidJUnit4.class)
public class YouTubeTestScenario {
@Test
public void watchVideo() throws Exception {
// TODO
}
}

UI Automator allows the user to interact with visible elements of the screen. In order to optimize testing, inspect the target app’s UI components and ensure that they are accessible. Each element has certain characteristics, such as content-description, class, resource-id, text, etc.

Step 3: To interact with the visible elements, launch the uiautomatorviewer tool follow the below steps:

  • Launch the target application. Let’s take a youtube app in this case on a physical device (Mobile phone)
  • Connect the device to your development machine.
  • Open a terminal or a command line and navigate to the <android-sdk>/tools/ directory.
  • Run the tool with this command:
$ uiautomatorviewer

Step 4: Let’s build a scenario for app automation.

  1. Launch the Youtube application.
  2. Click on the search icon in the search field to initiate a search.
  3. Enter the text “Browserstack” in the search field
  4. Launch the Webinar “The pesticide paradox” ;
  5. Watch the initial 40 seconds of the video.

Step 5: Before starting any interaction with the screen elements, initiate a UiDevice object to represent the current state of the mobile screen using the below command.

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

Step 6: As a next step, click on the search icon in the search field of the YouTube App to initiate the search.

Test YouTube Android App using UIAutomator

Step 7: After finding the icon resource-id, use the waitForExists method to make the element clickable from the moment it is visible.

UiObject icon = mDevice.findObject(new UiSelector().resourceId("com.google.android.youtube:id/menu_search"));
icon.waitForExists(TIMEOUT);
icon.click();

Step 8: Use the setText method to type the desired search keyword.

UiObject editText = mDevice.findObject(new UiSelector().text("Search YouTube"));
editText.waitForExists(TIMEOUT);
editText.setText("browserstack");
mDevice.pressEnter();

In this example, we’ve used a single criterion for searching web elements on the screen. However, It is also possible to combine a few more elements as seen in the code below

UiObject video = mDevice.findObject(new UiSelector()
.resourceId("com.google.android.youtube:id/title")
.text("The pesticide paradox"));

Step 9: At last, let’s now wait a few seconds and start watching the video.

synchronized (mDevice) {
mDevice.wait(40000);
}

Executing Android UI Automator Test Case

You can run UIAutomator tests either from Android Studio or from the command line. Ensure to specify AndroidJUnitRunner as the default instrumentation runner in the project.

Advantages of UIAutomator Framework

  • User-friendly setup and use
  • System components interaction is quite easy
  • Efficiently works with UI elements directly

Limitations of UIAutomator Framework

  • It supports only Java/Kotlin
  • Requires Android 4.3 and above, SDK version 21 or above, and API version 16 or higher.

Testing in real user conditions allows the tester to verify and understand web app behavior when used by the customers. If testers do not have an in-house device lab, 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 app automation 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 quickly.

Run Appium Tests on Real Devices

Tags
Appium Automated UI Testing Automation Testing Mobile App Testing

Featured Articles

How to Inspect Element using UIAutomatorViewer in Appium

How to run Appium tests on Android devices

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.