Android App Automation using UIAutomator

On This Page Introduction to Android UI Automator FrameworkJune 27, 2026 · 11 min read · Mobile Testing

Android App Automation using UIAutomator

UI Automator helps automate Android app examination. It allows creating tests that can interact not only with the app but also with system-level features like telling, settings, or other apps.

Overview

What is a UIAutomator?

  • Android testing framework for automating app and system-level interactions.
  • Works without needing app source code.
  • Supports cross-app UI examination: notice, background, other apps.
  • Ideal for functional and end-to-end Android UI testing.

UIAutomator Key Features

  • Interact with visible UI constituent across apps.
  • Access system-level UI (permissions, notifications, quick background).
  • Works independently of app setting.
  • Compatible with AndroidX Test libraries.
  • Runs on real devices and ape without root access.

Core UIAutomator APIs

  • UiCollection: Enumerate container constituent; target sub-elements.
  • UiObject: Represents visible UI element.
  • UiScrollable: Scroll through container items.
  • UiSelector: Query and locate UI elements.
  • Configurator: Set key trial parameters.

Advantages of UIAutomator

  • Supports cross-app and system-level testing.
  • Works independently of the app beginning.
  • Runs on real device and copycat.
  • Compatible with AndroidX Test and early fabric (Espresso, JUnit).

Read this guide in particular to learn more about the Android UI Automator, its uses, lineament, and implementation with a step-by-step explanation.

Introduction to Android UI Automator Framework

UI Automator is a fabric suitable for cross-app functional across the installed apps. The APIs of UIAutomator lets you interact with visible constituent on a device, disregardless of which Android Activity is in focus. Thus, this lets you perform operation like open the Settings Menu or the App Launcher in a test device.

UI Automator doesn ’ t need accession to the seed code of the coating in order to work. Due to this, the script can sail and interact with the application tray, scene application, third-party application, or any early app you want to.

The key characteristic 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 examination.

Read More:

UI Automator APIs in Android

With UIAutomator APIs, you can write robust tests without cognise 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 & # 8217; s UI elements for counting or direct 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 prey UI constituent on a twist.
  • Configurator:Let you set the key parameter for UIAutomator tryout that are running.

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

Android App Automation using UIAutomator

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

Step 1: In the build.gradlefile of your Android app module, set a dependence 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 aboutAndroidJUnitRunner

Step 2:Ensure that the project compiles right. After that, add a new class inmodule/src/androidTest:use the code below.

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

UI Automator grant the user to interact with seeable elements of the screen. In order to optimize testing, inspect the mark app ’ s UI factor and ensure that they are accessible. Each element has certaincharacteristic, 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 prey covering. Let & # 8217; s take a youtube app in this case on a physical gimmick (Mobile phone)
  • Connect the gimmick to your maturation machine.
  • Open a terminus or a command line and navigate to the& lt; android-sdk & gt; /tools/ directory.
  • Run the tool with this command:
$ uiautomatorviewer

Read More:

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

  1. Launch the Youtube coating.
  2. Click on the search icon in the search field to start a search.
  3. Enter the schoolbook “ Browserstack ” in the search field
  4. Launch the Webinar “The pesticide paradox” ;
  5. Watch the initial 40 second of the picture.

Step 5:Before begin any interaction with the screen ingredient, initiate a UiDevice object to symbolize the current province of the mobile blind using the below command.

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

Step 6:As a following step, dog on the search icon in the hunting field of the YouTube App to initiate the search.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

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

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

Step 8: Use the setTextmethod to type the desired hunt keyword.

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

In this model, we & # 8217; ve used a single criterion for explore web elements on the screen. However, It is also possible to combine a few more elements as seen in the code below

UiObject picture = 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 moment and depart catch the video.

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

Read More:

Executing Android UI Automator Test Case

You can run UIAutomator tests either fromAndroid Studioor from the command line. Ensure to specify AndroidJUnitRunner as the default instrumentality runner in the project.

Read More:

Advantages of UI Automator Framework

UI Automator supports interaction with Android gimmick UI components at both the covering and system grade. Here are some of the core advantages of use the UI Automator model:

  • Cross-App : UI Automator can interact with multiple apps during a single test flow that regard system settings, notice, or third-party apps.
  • Access to System UI Elements:UI Automator instantly supports the system-level ingredient such as permission dialogue, notifications, quick setting, and more.
  • No App Context Needed:It works independently, i.e, interaction with other apps are possible even if they weren ’ t develop in-house or are not part of the same app under trial.
  • Integrates with AndroidX Test:Works effectively with AndroidX Test libraries and test smuggler that supports smoother mechanization workflow with other testing tools like Espresso or.
  • Runs on Real Devices and Emulators:Works perfectly across real devices and emulator without requiring root access or peculiar device configurations.

Read More:

Limitations of UI Automator Framework

While UI Automator is powerful for system-level and cross-app examination, developer notwithstanding need to face sure limitations, such as:

  • Limited to Android API 18 and Above:UI Automator is supported just on device running Android 4.3 (API level 18) or higher. This restricts its usability on senior devices, which are still available in the marketplace.
  • Slow Compared to Espresso: UI Automator work at the gimmick UI stratum, which makes it slower than frameworks like Espresso, which interact more now with app part.
  • Limited Gesture Support:It lacks support for more complex multi-touch interaction like pinch, zoom, drag-and-drop, etc.
  • Weaker Synchronization with App Threads:It doesn ’ t automatically contemporise with the UI thread or background tasks. This means developer have to explicitly add a wait condition likeUntil.hasObject (& # 8230;)to ensure reliable test execution.

BrowserStack offers 3000+ real browser and device for manual and. QAs can test Android apps on thousands of real mobile devices, both latest and legacy. They can desegregate seamlessly with the BrowserStack cloud via numerous testing frameworks –,,, or EarlGrey.

Testers can execute Android app automation to verify multiple aboriginal gimmick features –,, preloaded images,, in-app purchase, time zone, languages, etc.

Simply, select the device-operating combination required and start testing. The entire process is construct to provide convenience, efficiency, speed, and accuracy to tester and developer likewise. With a all-inclusive range of and, BrowserStack is progress to not just place bugs, but decide them quickly.

How to Use UI Automator for Android UI Testing with Kotlin and AndroidX?

UI Automator is a knock-down testing framework provided by Google for Android UI testing. It let interaction with both your app and system UI components, making it ideal for testing conditions like permissions, apprisal, and multi-app workflow. When combined with Kotlin and AndroidX Test, it offers a concise, modern approach to writing end-to-end UI exam.

Like what you are read?

You can part discuss with our discord community

Here are some of the steps to be followed for using UI Automator with Kotlin and AndroidX:

Step 1: Set Up Your Project for UIAutomator

Firstly, add all the dependencies to yourbuild.gradle (app-level) file:

dependency {androidTestImplementation (`` androidx.test.uiautomator: uiautomator:2.2.0 '') androidTestImplementation (`` androidx.test: core:1.5.0 '') androidTestImplementation (`` androidx.test.ext: junit:1.1.5 '') androidTestImplementation (`` androidx.test.espresso: espresso-core:3.5.1 '')}

Step 2: Create an Android Instrumentation Test Class

Now, make a new test class inside the androidTest directory. Consider the file locating assrc/androidTest/java/com/yourpackage/ExampleUiAutomatorTest.kt.

package com.yourpackage import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.By significance androidx.test.uiautomator.UiDevice importation androidx.test.uiautomator.Until import org.junit.Before importee org.junit.Test import org.junit.runner.RunWith @ RunWith (AndroidJUnit4: :class) class ExampleUiAutomatorTest {individual lateinit var device: UiDevice @ Before fun setUp () {device = UiDevice.getInstance (InstrumentationRegistry.getInstrumentation ()) device.pressHome ()} @ Test fun openAppAndClickButton () {val setting = InstrumentationRegistry.getInstrumentation () .targetContext val packageName = context.packageName val intent = context.packageManager.getLaunchIntentForPackage (packageName) intent? .addFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK) context.startActivity (purpose) device.wait (Until.hasObject (By.pkg (packageName) .depth (0)), 5000) val startButton = device.findObject (By.text (`` Start '')) startButton.click ()}}

Step 3: Use Selectors to Locate UI Elements

To help you locate UI elements, UI Automator indorse efficient selectors through the BySelector API, such as:

  • By.text (& # 8220; & # 8230; & # 8221;) & # 8211; situate by visible textbook
  • By.desc (& # 8220; & # 8230; & # 8221;) & # 8211; for content-description
  • By.res (& # 8220; com.example: id/button & # 8221;) & # 8211; by resource ID
  • By.clazz (& # 8220; android.widget.Button & # 8221;) & # 8211; by class gens

Combine multiple selectors to simplify the hunting and target the correct UI constituent. For instance, to bump a button with a specific resource ID and class name, you can conflate the selectors like this:

val push = device.findObject (By.res (`` com.example: id/start_button '') .clazz (`` android.widget.Button '')) button.click ()

Step 4: Run the Test

You can run the test in two mode:

  • From Android Studio: Right-click on the exam family or method and then select Run ‘ExampleUiAutomatorTest ’. Choose a physical device or emulator with API 18+.
  • From the command line: Run the command line:
./gradlew connectedAndroidTest

Step 5: Wait for UI Changes

Use device.wait (& # 8230;)with a condition and timeout to wait for ingredient or change in the UI before interacting with elements.

device.wait (Until.hasObject (By.text (`` Next Screen '')), 3000)

Here, the following blind appears with changes after 3 bit.

Step 6: Work with System UI

In real-world testing, there can be a need to handle system-level elements like permit popups, notification jury, or quick scope toggles. In this case, UI Automator can interact with both your app and the system UI, which most of the testing tools can ’ t do effectively.

These are mainly useful in cases like:

  • App requests permissions like location, camera, etc.
  • Validation of behavior initiated by the notifications.
  • Test interactions regard scheme dialogs or settings.

Example 1: Dismissing a permission dialog

val allowButton = device.findObject (By.text (`` Allow '')) allowButton? .click ()

Example 2: Opening notifications

device.openNotification () device.wait (Until.hasObject (By.text (`` New Message '')), 3000)

Read More:

Why Test Android Apps Using BrowserStack App Automate?

Testing Android apps on different device, screen size, and OS versions is crucial for delivering a seamless user experience. However, maintaining such a diverse trial environment in-house is costly and complex. This is where become a feasible solution. It allow you to run automated tests on real Android devices that are hosted on the cloud.

By integrating with popular testing frameworks like,,, and UI Automator, App Automate offers better compatibility with your existing tryout suite. It becomes ideal for catching bugs that ape might miss.

Key Features of BrowserStack App Automate

Some of the master features of expend BrowserStack App Automate are:

  • : Run automated tests on a wide range of existent Android devices with different OS adaptation, screen resolutions, configurations, etc.
  • Framework Compatibility:Supports Appium, Espresso, UI Automator, XCUI Test, and Detox, let you to use the test framework of your choice without changes.
  • CI/CD Integration:Integrates with popular like, GitHub Actions, GitLab,, and Azure DevOps for automated workflows.
  • Device Logs & amp; Video Recording:Get detailed gimmick logarithm, video recordings, screenshots, and execution data for every test session, which is used for debug failure.
  • Validate native device features:Testers can execute Android app mechanization to verify multiple aboriginal device features –,, preloaded images,, in-app purchase, time zone, languages, etc.

Conclusion

Automated UI testing helps to check that Android apps function perfectly across diverse devices and user conditions. Tools like UI Automator support both app and scheme UI, making them ideal for end-to-end testing.

While it has certain limitations, pairing it with modernistic frameworks and cloud platforms boosts and dependability. Advanced creature like BrowserStack App Automate provide existent device access, unlined consolidation, and detailed examination reports by maintain device shape.

Tags
14,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free