Running Instrumentation Tests Programmatically on Android

Running Instrumentation Tests Programmatically on Android

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Developers often face challenges when trying to run instrumentation tests directly from an Android app without relying on a connected PC. This guide aims to provide a solution for running Appium UiAutomator2 tests programmatically within an app, addressing common issues such as permission requirements and test stability.

Understanding the Challenges

When attempting to run instrumentation tests programmatically, one common approach is to use the adb shell am instrument -w command. However, executing this command from within an app can lead to permission errors, specifically the INTERACT_ACROSS_USERS_FULL permission, which is not granted to third-party apps by default.

Common Errors

  • Missing INTERACT_ACROSS_USERS_FULL permission.
  • NullPointerException when trying to retrieve UiAutomation instance.

Proposed Solution

To circumvent these issues, a potential solution involves modifying the Android system itself. Here are the steps you can follow:

Steps to Implement the Solution

  1. Build a custom version of the Android Open Source Project (AOSP).
  2. Add the android:protectionLevel="signature" attribute to the Manifest.xml file for the required permission.
  3. Sign the ROM with the same key as your app to obtain the necessary permissions.
  4. Deploy the custom ROM to your device. Note that this approach is typically feasible for Nexus and Pixel devices, as their drivers are readily available.

While this approach involves significant effort, it ensures that you can run your tests without the limitations imposed by stock ROMs.

Alternative Solutions and Tools

If building a custom ROM is not a viable option, consider using alternative tools that offer more flexibility and ease of use. For instance, Repeato is a no-code test automation tool for iOS and Android that simplifies the process of creating, running, and maintaining automated tests for your apps. Repeato leverages computer vision and AI to provide a robust testing framework that does not require extensive setup or system modifications.

Advantages of Using Repeato

  • Fast test creation and execution.
  • Stable and reliable testing environment.
  • No dependency on a connected PC or custom ROMs.

By using Repeato, you can achieve efficient and effective test automation without the complexities associated with traditional methods. For more information on how Repeato can help streamline your testing process, visit our Getting Started page.

For further reading on related topics, check out our guides on Virtual Test Devices and Running Test Batches.

For any additional questions or support, feel free to contact us.

Like this article? there’s more where that came from!