Execute Your Arbitrary ADB Commands with Appium

      If you’re not a big Android person, you might not know about ADB, the “Android Debug Bridge”. ADB is a powerful tool provided as part of the Android SDK by Google, that allows running all sorts of interesting commands on a connected emulator or device. One of these commands is adb shell, which gives you shell access to the device filesystem (including root access on emulators or rooted devices). adb shell is the perfect tool for solving many problems.

      Appium did not allow running of arbitrary ADB commands. This is because Appium was designed to run in a remote environment, possibly sharing an OS with other services or Appium servers, and potentially many connected Android devices. It would be a huge security hole to give any Appium client the full power of ADB in this context. Recently, the Appium team decided to unlock this functionality behind a special server flag, so that someone running an Appium server could intentionally open up this security hole. This is achieved using –relaxed-security flag. So you can now start up Appium like this to run arbitrary ADB commands,

appium –relaxed-security

      With Appium running in this mode, you have access to a new “mobile:” command called “mobile: shell“. The Appium “mobile:” commands are special commands that can be accessed using executeScript (at least until client libraries make a nicer interface for taking advantage of them). Here’s how a call to “mobile: shell” looks in Java:

driver.executeScript(“mobile: shell”, arg);

arg needs to be a JSONifiable object with two keys:

  • command: a String, the command to be run under adb shell.
  • args: an array of Strings, the arguments passed to the shell command.

      For example, let’s say we want to clear out the pictures on the SD card, and that on our device, these are located at /mnt/sdcard/Pictures. If we were running ADB on our own without Appium, we’d accomplish our goal by running:

adb shell rm -rf /mnt/sdcard/Pictures/*.*

      To translate this to Appium’s “mobile: shell” command, we simply strip off adb shell from the beginning, and we are left with rm -rf /mnt/sdcard/Pictures/*.*

      The first word here is the “command”, and the rest constitute the “args”. So we can construct our object as follows:

List removePicsArgs = Arrays.asList(“-rf”, “/mnt/sdcard/Pictures/.”);
Map removePicsCmd = ImmutableMap.of(“command”, “rm”, “args”, removePicsArgs);
driver.executeScript(“mobile: shell”, removePicsCmd);

      We can also retrieve the result of the ADB call, for example if we wish to verify that the directory is now indeed empty:

List lsArgs = Arrays.asList(“/mnt/sdcard/Pictures/.”);
Map lsCmd = ImmutableMap.of(“command”, “ls”,”args”, lsArgs);
String lsOutput = (String) driver.executeScript(“mobile: shell”, lsCmd);
Assert.assertEquals(“”, lsOutput);

      The output of our command is returned to us as a String which we can do whatever we want with, including making assertions on it.

      In this article, I hope you got the power of ADB through a few simple file system commands. You can actually do many more useful things than delete files with ADB, so go out there and have fun with it in your automation flow.

Reference: Appium Pro

make it perfect!

Android – ADB essential commands

     Android Debug Bridge (ADB) is an application that available along with Android SDK (in platform-tools folder). The ADB is typically accessed through the command-line interface. In Android application environment, ADB plays an important role especially in debug situations. Your Android device should be in debug mode enabled state to get the interaction with ADB services. ADB provides lot of useful services and we can utilize those by running it as ADB commands. Run the ADB commands in the following way in your command prompt (in Windows) or terminal (in Mac OS):

adb <commands>

Following are the prerequisites to run the ADB commands:

1. Android SDK should be available in your system.

2. ANDROID_HOME (of Android SDK) and PATH (of platform-tools and tools) in system environment variables.

3. Make sure that your devices debug mode is enables while running the ADB commands.

Essential ADB Commands

     Here I would like to share some essential adb commands that would be useful in Automation as well as manual testing.

  • To get device list

Purpose: This command helps to get list of all connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool

           adb devices

list of devices

  • To install Android Application

Purpose: This command helps to install Android application into the connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool. In the command section we need to specify location of the .apk file which stored in your system.

adb install <path of the .apk file>

install

  • To uninstall Android Application

Purpose: This command helps to uninstall Android application from connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool. In the command section we need to specify package name of the application which need to uninstall from device.

adb uninstall <package>

unistall

  • To know package name and activity name of Android Application

Purpose: This command helps to get the package name and activity name of current running Android application. This command is very much useful for me for Appium – Android automation. The situation was I already installed the application in device but I need to specify the package name and activity name of the application in Desired Capabilities of Appium configuration. In that situation this command was very much useful for me.

How to use: Run below commands in command-line tool. In this case first we need to switch to the shell after that doing the dumpsys to get the details.

adb shell

dumpsys window windows | grep -E ‘mCurrentFocus|mFocusedApp’

getpackage

  • To capture the screenshot

Purpose: This command helps to capture the current active screen of your application in connected device. Same thing we can do from the Phone itself using the shortcut keys. But in some devices we can’t identity the shortcut keys. So in such a situation this command was helps me to capture the screenshots.

How to use: Run below commands in command-line tool. In this case first we need to create a folder in sdcard. Secondly, run the screencap command to capture the screenshot.

adb shell mkdir /sdcard/screenfolder

adb shell screencap /sdcard/screenfolder/”%time%”.png

You can save this command in a batch (.bat) executable file and run it by double click on it to capture the screenshot instead of writing the whole above commands.

screencapt

  • To record the screen flow in video format

Purpose: This command helps to record the current running flow of any application from connected devices. Sometimes the managers or client may ask the screen-recorded flow of an issue; in such a situation we can do it with help of ADB command.

How to use: Run below commands in command-line tool. In this case first we need to create a folder in sdcard. Secondly, run the screenrecord command to record the screen flow as video.

adb shell mkdir /sdcard/screenrecorded

adb shell screenrecord /sdcard/screenrecorded/test.mp4

You can save this command in a batch (.bat) executable file and run it by double click on it to record the video instead of writing the whole above commands.

video

  • Trace your device logs

Purpose: This command helps to trace the device logs. This command helps me to take the crash logs of the applications.

How to use: Run below commands in command-line tool. Here [<filter-spec>] includes different log priority levels like,

V  – Verbose

D  – Debug

I  –  Info

W – Warn

E  – Error

F  – Fatal

S  – Silent

adb logcat [ <filter-spec> ]

You can save this command in a batch (.bat) executable file and run it by double click on it to trace device logs instead of writing the whole above commands.

  • Run Android Apps over WiFi

Purpose: This command helps to run your Android applications over WiFi connection. I have faced lots of risk in executing my automation test scripts without USB cable. Finally I got a solution to run/debug my scripts in Android devices over WiFi. The concept has already there in Android SDK with help of adb. Now, I can run my test scripts in multiple Android devices without cable.

How to use: We can run the commands in following methods:

Method 1:

1.  Activate debug mode in android

2. Connect to pc via usb

3. Open command prompt type: adb tcpip 5555

4. Disconnect your tablet or smartphone from pc

5. Open command prompt type: adb connect <IPADDRESS> (IPADDRESS is the DHCP/IP address of your tablet or smartphone, which you can find by Wi-Fi -> current connected network)

6. Check for list of devices by using adb devices

Method 2:

1.  Activate debug mode in android

2. Connect to pc via usb

3. Open command prompt type: adb connect <IPADDRESS>:<port>

4. Disconnect your tablet or smartphone from pc.

5. Check for list of devices by using adb devices