Android App Testing with ADB

Android applications are the prominent software developed in this era. The developers and testers may want to automate the testing of their App with less learning curve, before getting into a full blown test frameworks. Using ADB commands for Android testing is a lean methodology, where with a few commands and little quirks, the tests can be automated. In this article let’s get to know about the ADB, and few categories of UI features that can be tested with ADB. It would be nice to watch, testing the "GALLERY" App automatically with ADB.

What is ADB command

Android debug bridge is a debugging facility provided by Android development ecosystem, to communicate with a Android device. It has ADB client and server running in development machine, and ADB daemon running in the device.

ADB is included in the Android SDK Platform-Tools package. We can download this package with the SDK Manager, which installs it at android_sdk/platform-tools. Details regarding the ADB can be obtained from https://developer.android.com/studio/command-line/adb

We consider the stock Android’s gallery app, for testing. Few user actions that can be performed through ADB is

  • invoking the gallery app

  • pushing images to the device

  • opening photos from a folder

  • swiping pictures left or right

  • closing the app and getting back to home page.

X & Y Co-ordinates

To click or tap on the Android screen we may need to understand Few UI components of Android.

  • Android’s View system is designed to handle layout and view positions automatically. A developer doesn’t need to set the absolute position of a view in a layout.

  • Instead, we rely on layouts to define constraints. However, sometimes especially when handling view test automations, we need to determine the position of a view. Android provides various ways to obtain the position of views and viewgroups.

  • One light-weight option to obtain the X,Y co-ordinates of every touch, is by enabling a debug options as below in the Android phone

  Settings
     --> Enable Developer Option
           --> Enable Pointer Location.

After enabling it, we will be able to see x and y co-ordinates of point touched, on the top of the screen.

/static/images/pointer_location_enable_edit.png
Figure 1. Enable Pointer Location
  • If you swipe upwards in the display, then y will get decremented. If you swipe left, then x will get decremented. Same it can be interpreted for other directions too.

/static/images/XY_Coordinates.png
Figure 2. XY Coordinates
  • The alternate way would be to get the coordinates of views by using the uiautomatorviewer which is not covered part of this article.

Let’s do a simple feature test in gallery APP by viewing the images pushed from external source using ADB commands.

Below command used to push a photo from a PC to the sdcard of the emulator/mobile connected for debugging.

$ adb push /home/user/Downloads/optimistic.jpg /sdcard/Pictures/optimistic.jpg
/static/images/push_pic.png
Figure 3. Pushing Picture to Device
  • to get access to the Android shell we can invoke below command

PC$ adb shell
$ adb shell pm list packages | grep -i gallery

By using monkey command Gallery APP can be opened to view the pictures.

$ adb shell monkey -p com.sec.android.gallery3d -c android.intent.category.LAUNCHER 1
  • After opening Gallery, we can use XY co-ordinates

    • to open the specific folder by tapping as "adb shell input tap 600 500" and

    • to view the image can tap the image as "adb shell input tap 450 500" .

$ adb shell input tap <X> <Y>
  • Also, we can swipe images from Left to Right and Up to Down.

    • to swipe Left "adb shell input swipe 800 500 300 500",

    • to swipe Right "adb shell input swipe 300 500 800 500",

    • to swipe Up "adb shell input swipe 400 800 400 200",

    • to swipe Down "adb shell input swipe 400 800 400 200" and

    • to go back to home screen 'adb shell input tap 500 2200"

$ adb shell input swipe <X Start> <Y Start> <X End> <Y End>
  • To go back to home we can use keyevent as shown below

$ adb shell input keyevent KEYCODE_HOME
Note

The resolution and xy co-ordinates differs for each mobile.

Automation

The adb commands can be written in sequence as a shell scripts, using which app tests can be automated.

Conclusion

ADB is a super utility provided by Android and it has lots of sub options to explore the Android system. With ADB and along with other shell commands, it is possible that we can test more features of the Android system.