DEV Community

Gaurav Singh
Gaurav Singh

Posted on • Originally published at automationhacks.io on

Using adb and cmdline to ease android app testing

Photo by Christian Wiediger on Unsplash

Hello there,

If you have been testing mobile apps on android platform, chances are there are some common workflows/steps that you follow to enable you to test effectively.

Most of these are done as you get setup or are in the middle of actually testing the feature/functionality on the app.

Some of these include:

  • Copy the apk file over to the real device using Android File Transfer (on mac) while ensuring that the MTP mode is enable the file transfer in the first place
  • Installing the build by finding it in the sdcard folder
  • Capture any crash logs that occur while you are testing the app
  • If you notice a bug, then take a screenshot
  • While pairing with another dev/tester, require to share the real device screen, (You might be using Vysor for this but the loss of quality in the free version is simply annoying)
  • Take videos of the test execution cycle.

Now, some or most of these flows can be performed via Android studio by using the logcat control, however the caveat is that Android Studio/running emulator is quite performance intensive task and captures a lot of memory

If you are running other demanding applications like IntelliJ, Pycharm and possibly Docker then it can quickly slow down your machine considerably and bring you close to the point of frustration.

Well we don’t want that to happen to us do we?

As testers, we owe it to ourselves to solve some of these pains or to make it easy. Turns out after a little bit of digging and some pointers from friendly android devs in my current team, I was able to figure out some easy workarounds to accomplish all these tasks All via command line and adb… 👨🏻‍💻

Sweet. Lets get started.

Pre-requisite

Most of the commands in this section requires adb to be setup and available in your path.

Also ensure the device has USB debugging enabled in Developer Options

Open emulator via adb

What if you wanted to open an emulator directly via command line? A way to do this is to go to AVD manager in android studio and then click on play for the relevant emulator image you want to start.

However this could just as easily be done via CMD line using emulator with -avd option

If you are on other platforms like Windows/Linux, then find the directory where the Android/sdk/emulator is present and replace that in below command.

Library/Android/sdk/emulator/emulator -avd <emulator_image_name> -netdelay none -netspeed full
Enter fullscreen mode Exit fullscreen mode

How to copy a file over to you device

Assuming you have a single android device connected via USB, You can execute below command, this would copy the any file from your local dir to a connected emulator or real device.

Huge time saver ⏰ and very quick compared to manually dragging the file over using Android File Transfer mac app.

adb push <file> /sdcard/
Enter fullscreen mode Exit fullscreen mode

However, If you have multiple emulators/devices connected on executing above command you would get an error like below:

adb: error: failed to get feature set: more than one device/emulator
Enter fullscreen mode Exit fullscreen mode

To workaround, We need to tell adb, which device we want to send the file over to.

First, we need to know what devices are connected. Execute below

adb devices -l
Enter fullscreen mode Exit fullscreen mode

This would return a list of connected device. Notice adb provides a unique string for all real devices (5200d275428186b3) and for all the running emulators (emulator-<TCP_PORT>)

List of devices attached
5200d275428186b3 device usb:342097920X product:m30ltedd model:SM_M305F device:m30lte transport_id:1
emulator-5554 device product:sdk_gphone_x86_arm model:AOSP_on_IA_Emulator device:generic_x86_arm transport_id:2
Enter fullscreen mode Exit fullscreen mode

Cool, so to transfer the file over, we need to tell adb to use the device that we are interested in with the -s option and the remaining command remains same.

adb -s <emulator_or_real_device_id> push <file> /sdcard/
Enter fullscreen mode Exit fullscreen mode

Installing APK via adb

Given that you have the APK file in a dir on your local machine, you can use install command in adb to directly stream the file over and install on the device

adb -s <emulator_or_real_device_id> install <file_name>.apk
Enter fullscreen mode Exit fullscreen mode

Simple and sweet! 🙌🏼

Uninstalling the previously installed app

Running the above command would install the app every time, but if you wanted to do a clean install and ensure the app is not already installed (Simulate a first time users experience)

Then we can easily uninstall the app via pm (package manager) command

You would first need to know the package name for the app that you want to uninstall, Run below

adb -s <emulator_or_real_device_id> shell dumpsys window windows | grep -i "mCurrentFocus"
Enter fullscreen mode Exit fullscreen mode

This would give an output like below:

mCurrentFocus=Window{253d2d3 u0 com.example.app.staging/com.example.app.RootActivity}
Enter fullscreen mode Exit fullscreen mode

Here the package name would be com.example.app.staging

To uninstall, we can run below command:

adb -s <emulator_or_real_device_id> shell pm uninstall <package_name>
Enter fullscreen mode Exit fullscreen mode

So, now we know how to copy a file over, and also install an app on any device (real or emulated).

Testing the app

Given we are testing the application, the 3 main tasks that we need to perform are:

  • Take a screenshot
  • Record a movie
  • Capture crash logs
  • Share the real device screen on your system (during a pairing session)

Capturing a screenshot

Lets say, I have the Fake GPS on the real device screen and I noticed a bug, I would want to share this with the devs in the bug report

We can use screencap utility to capture and store the screenshot

To directly take the screenshot and copy to your local machine execute below:

adb exec-out screencap -p > <file_name>.png
Enter fullscreen mode Exit fullscreen mode

Recording a video:

To record a video we can use screenrecord command with adb

Firstly open adb shell

adb -s <emulator_or_real_device_id> shell
Enter fullscreen mode Exit fullscreen mode

Then execute below to start recording

screenrecord --verbose /sdcard/demo.mp4
Enter fullscreen mode Exit fullscreen mode

Finally when you are done, Press Control + C to exit

and finally copy the file to your local machine via adb pull

adb pull /sdcard/demo.mp4
Enter fullscreen mode Exit fullscreen mode

Sharing your screen

Often when you are pairing with a dev or doing a mob testing session with the team, You would want them to see what you are trying on the real device. Android studio currently does not include any tool to cast the screen.

Many people use Vysor tool for this, however the free version gives a very bad casting experience.

Recently I came across scrcpy a wonderful open source tool to do this without any quality loss.

You can quickly install it via homebrew

brew install scrcpy
Enter fullscreen mode Exit fullscreen mode

And once installed to share your screen, just run:

scrcpy --show-touches
Enter fullscreen mode Exit fullscreen mode

And you quickly get a screen on your machine with your device screen. Enjoy pairing and sharing stuff with ease. 🙆🏻‍♂️

A screen shared via scrcpy

scrcpy is a quite powerful and useful tool and I would highly recommend checking it out. It made my life so much simpler and better while testing on android real devices

Logging crashes via logcat

What if you are testing and the app crashes in the middle. Unless its a easily reproducible crash, sometimes its very difficult to isolate that exact log in the middle of a sea of logs that logcat utility generates.

However fret not, with a single command we can get the logs for the last crash via this simple command.

adb -s <emulator_or_real_device_id> logcat -b crash > <file_to_which_you_want_to_write>
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Challenge yourself to use more of command line utils and you would realize there is immense value and comfort in using them.

In case you want to learn more about the wonders of adb, Checkout developers.android.com

If you found this post useful, Share it with a friend or colleague. Until next time. Happy testing!

Top comments (0)