Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud
  • Home
  • /
  • Blog
  • /
  • Automate Android Apps With UI Automator
Mobile App TestingAutomationTutorial

Automate Android Apps With UI Automator

Learn how to automate Android apps using UI Automator to perform functional UI testing across the device and installed apps.

Author

Yogendra Porwal

January 11, 2026

UI Automator is an Android testing framework that allows you to automate UI testing of Android apps. It enables interaction with UI elements, sending input events, and verifying mobile app behaviors on emulators or real devices.

Overview

UI Automator is a framework to automate UI testing of Android apps. With UI Automator, you can perform interactions with UI elements and test app behaviors on emulators or real devices.

How to Run UI Automator Test

Here are the high-level steps to run Android UI Automator tests:

  • Install Android Studio and JDK 8 or above, then set up ADB along with an emulator or a real Android device running API level 18 or higher.
  • Add UI dependencies in the build.gradle file.
  • Write a test:
    • Use the UiDevice API to launch the app via ADB.
    • Find web elements using the By.res() method.
    • Interact (e.g., click the button) and assert test results.
  • Use Android Studio’s test runner to execute tests.

What Is UI Automator?

UI Automator is a framework for automating user interactions on mobile apps and system UI components on Android devices, such as launching the settings menu or interacting with the app launcher.

You can also locate UI components using intuitive attributes like visible text or content descriptions, without relying on internal implementation details of the mobile app.

UI Automator is based on instrumentation APIs and works with the AndroidJUnitRunner test runner. So, it is suitable for black-box testing scenarios where tests simulate user behavior without accessing the app’s codebase.

Prerequisites

Before writing your first Android UI Automator test for Android app testing, ensure you meet the following prerequisites:

  • Install Android Studio and make sure the Android SDK tools are downloaded during setup.
  • Install Java Development Kit (JDK) v8 or above.
  • Set up an emulator (or real device) running Android 4.3 (API level 18) or above for testing.
  • Add Android Debug Bridge (ADB) to your system path so you can run ADB commands from the terminal.
  • Enable and use Layout Inspector in Android Studio to inspect UI elements and identify reliable selectors.
Note

Note: Test your mobile apps on real Android devices. Try TestMu AI Today!

Add UI Automator Dependencies

Here we’ll use an Android emulator to execute tests. Once you have set up the Android emulator, let’s add the UI Automator dependencies:

  • Create an Android testing project.
  • To use UI Automator, add dependencies for uiautomator and junit in the app/build.gradle file.
  • dependencies {
      ...
    androidTestImplementation ‘androidx.test.ext:junit:1.2.1androidTestImplementation ‘androidx.test:runner:1.2.0androidTestImplementation ‘androidx.test.uiautomator:uiautomator:2.3.0}
    
    ...
  • After adding these dependencies, sync the Gradle project.

Write Tests With UI Automator

Let’s write a test to open the proverbial app and verify the button functionality.

Test Scenario:

  • Open the TestMu AI proverbial app.
  • Assert the initial text displayed.
  • Click the button with the label “Text”.
  • Verify that the text gets updated to “Proverbial”.

Once your Android emulator is up, run the adb install <path-to-TestMu AI-proverbial-apk-file> command to install the TestMu AI proverbial app.

Implementation:

  • Create a setup method with the @Before annotation.
  • For this use case, we will initialize the UiDevice instance and press the Home button to ensure the test starts from the home screen.

    @Before
    public void setup() {
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        device.pressHome();
    }
    

    This setup will also include the launch of the proverbial app with the ADB command.

    //start Proverbial app with adb command
    device.executeShellCommand("am start -n com.lambdatest.proverbial/.MainActivity”);
    // Validate that activity has been opened.
    Assert.assertTrue(device.hasObject(By.pkg(“com.lambdatest.proverbial”)));
    
  • Use Layout Inspector to inspect and identify selectors/locators. Then, use the resource ID to uniquely identify the welcome message on the home page of the mobile app.
  • Locate the text label element and verify its initial content.
  • // initialize the text label element
    UiObject2 textElement = device.findObject(By.res(“<replace_with_package_name_of_app>”, "Textbox"));
    // Assert the initial text
    Assert.assertEquals("Hello! Welcome to lambdatest Sample App called Proverbial", textElement.getText());
    
  • Simulate a user clicking the button that updates the text.
  • UiObject2 textButton = device.findObject(By.res(PACKAGE_NAME, "Text"));
    //Click on Text button to update the element
    textButton.click();
    
  • Check if the text element updates as expected.
  • //Assert that text has been updated.
    Assert.assertEquals("Proverbial", textElement.getText());
    

Run the Android UI Automator Tests

From the Android Studio, click Run ‘ProverbialTest’ to execute the tests:

You can notice that your test is executed and passed.

ProverbialTest2 (1)_11zon

If you want to run Android automated tests at scale, you can leverage a cloud-based testing platform like TestMu AI. It is a GenAI-native test execution platform that offers a scalable real device cloud to run automated tests on real Android devices without the need for physical infrastructure.

With TestMu AI, you can test across a wide range of Android and iOS devices, covering multiple OS versions and device brands like iPhone, Samsung, Google Pixel, etc. It also supports automation using popular frameworks like Appium, Espresso, and XCUITest, enabling faster, more reliable mobile app testing at scale.

...

Key APIs of UI Automator

Let’s understand the key APIs of UI Automator that help interact with Android apps and their UI components, allowing automation of clicks, text input, scrolling, and more.

Key API
  • JUnitTestRunner: Initializes and runs Android UI Automator tests, coordinating the execution flow of your test class.
  • UiDevice: Represents the full Android device interface, allowing control over hardware buttons and system-level actions.
  • BySelector: Defines criteria to locate UI elements using properties like text, class name, or resource ID.
  • findObject(): Searches for a UI element in the device’s current UI hierarchy that matches the specified BySelector criteria.
  • UiObject2: Represents a UI element and allows interactions like clicks, swipes, typing, and reading attributes.
  • AccessibilityService: Reads and controls UI elements using the Android Accessibility API, often for enhanced automation or accessibility tools.
  • AndroidUI Framework: Displays and manages the visible UI elements on the screen, bridging between the app’s layout and the system.

Best Practices for Writing Stable UI Automator Tests

To write Android UI Automator tests that are both stable and maintainable, you can consider the following best practices:

  • Always Start From a Clean State: Before each test, reset your app or device to a known screen, like the home screen, using the device.pressHome() method.
  • If a test modifies settings or login state, make sure to clean that up afterward using @After methods. This keeps tests independent and repeatable.

  • Use Reliable Selectors: When identifying UI elements, go for stable attributes like resource ID or content description using methods like By.res() or By.desc().
  • Avoid relying on visible text, as it can change or get localized, making your tests flaky. If you’re building the app, add android:contentDescription to key elements to make them both testable and accessible.

  • Don’t Hard Code Waits: Avoid using the Thread.sleep() method. Instead, wait for elements or conditions dynamically using device.wait(Until.hasObject(…)). This makes your tests faster and more reliable across devices with different performance.
  • Use Setup and Teardown Hooks: Structure your test classes with @Before to handle initial setup like launching the app or logging in, and @After to clean things up. This reduces repeated code and keeps tests consistent.
  • Account for Device Differences: Android devices vary in screen sizes, OS versions, and themes, so your selectors should be flexible. Use methods like By.textStartsWith() or By.descContains() when the exact text isn’t ensured.
  • Also, use @SdkSuppress to skip tests that aren’t applicable on certain Android versions.

  • Use Clear Names and Logs: Always use meaningful test method names like testLogin_showsErrorOnInvalidPassword() so others (or your future self) can quickly understand what the test does. Logging with the Log.d() method can also be really helpful when debugging issues, especially in CI pipelines.
  • Reduce Randomness and External Dependencies: Network delays, real-time data, and third-party dependencies can cause flaky tests. Stub responses if you can, or use test environments. If unpredictability is unavoidable, add retry logic or longer timeouts to handle it.

Conclusion

If you’ve made it this far, you now have a solid foundation for using UI Automator to automate Android app testing, from setup and writing tests to understanding key APIs.

UI Automator is great for functional UI testing. However, running Android tests across many devices and OS versions can be complex and time-consuming. That’s where a platform like TestMu AI becomes helpful. It lets you instantly run automated tests on real Android devices in the cloud, no physical labs or maintenance needed.

Author

Yogendra Porwal is a Solution Architect at EPAM Systems with 10+ years of hands-on experience in quality assurance, spanning automation, functional, performance, and security testing. He specializes in tools like Selenium, WebdriverIO, and Playwright, with a strong focus on building robust automation frameworks and integrating them into CI/CD pipelines to improve test coverage and release efficiency. With 5+ years of leadership experience, he has guided small teams and mentored peers across QA initiatives. A regular contributor to testing communities, he recently shared insights at the Appium Conference 2024. His approach has evolved from script-heavy automation to a more strategic focus, leveraging automation as a support to core testing fundamentals for sustainable quality outcomes.

Close

Summarize with AI

ChatGPT IconPerplexity IconClaude AI IconGrok IconGoogle AI Icon

Frequently asked questions

Did you find this page helpful?

More Related Hubs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests