Combine adb commands into bash scripts for Android app testing.

For those regular actions in the app that you have to manually execute every day it’s very easy to write 1 bash script and execute it directly on the device. You can also combine different bash scripts depending on the path of your manual test. To use this technique you don’t need to know programming or configure test automation framework, download the application project or any other tools, and you can perform execution on actual app under your manual testing, not a tweaked app that supports test automation framework. Please note that I am a MAC user and commands might be different for Windows machine.

To avoid app uninstallation and installation steps (although that could be a separate intentional test), we can use  the command to clear data from the app. This way all the subsequent executions of the bash script will clear the data of the app, meaning if for example you have registered the user or you have logged into the app,  user data will be gone after running:

 adb  shell pm clear <package name>

Open Text Editor. Your every bash script file should start with:

#!/bin/bash

Let’s look at the possible bash script for Facebook app Login flow:

#!/bin/bash

echo Clear the app

adb  shell pm clear com.facebook.katana

echo Launch the app

adb shell am start -n com.facebook.katana/com.facebook.katana.dbl.activity.FacebookLoginActivity

echo Tap into email field

adb shell input touchscreen tap <X> <Y>

echo Enter E-mail address

adb shell input text <email address value>

echo Tap into Password field

adb shell input touchscreen tap  <X> <Y>

echo Enter passport

adb shell input text <password value>

echo Tap on LOGIN button

adb shell input touchscreen tap  <X> <Y>

For  how to find <X> <Y> coordinates please see my other article

Save this file giving the short meaningful name, for example lgG4_login. If you have multiple devices you plan to use bash script on, i recommend phone model as part of the file name.  To make this plain text executable, open the directory if the file in the terminal and run:

chmod 700 lgG4_login

To execute your login bash script, attach the phone to computer, and in the terminal within the directory of the bash script run:

./lgG4_login

Android ADB-supported Interruption Testing.

Kill the app:

adb shell am force-stop <package name>

Reboot the device:

adb reboot

Turn off/turn on screen (Power button):

adb shell input keyevent 26 && adb shell input keyevent 26

Open WiFi settings manager:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings

Open Recent Apps:
adb shell input keyevent KEYCODE_APP_SWITCH
Home button press:
adb shell input keyevent 3
Back button press:
adb shell input keyevent 4
Open Dialer with phone number entered:
adb shell am start -a android.intent.action.CALL -d tel://000-0000
Call:
adb shell input keyevent 5
End Call:
adb shell input keyevent 6
Open Notifications:
adb shell input keyevent 83
Send sms message:
adb shell am start -a android.intent.action.SEND -d “some message” -t text/plain
Launch default browser at the URL:
adb shell am start -a android.intent.action.VIEW -d https://mobileqablog.wordpress.com/

Android app automatic launch.

Find the name of the activity that should be launched.

Manually launch the app on the device, then run this command in the terminal:

adb shell dumpsys activity activities | grep mFocusedActivity

Launch your application automatically with this command:

adb shell am start -n <full name of the activity>

Example:

adb shell am start -n com.testapp.android.debug/com.testapp.android.ui.login.LoginActivity

Android Automatic touchscreen taps. adb shell input touchscreen tap

Getting coordinates enables us to use adb shell input touchscreen tap <X> <Y> command that requires coordinates of the exact position <X> and <Y>  on screen for automatic interaction. Please note that you will only be able to execute those commands on device with that specific screen resolution and density on which you captured those coordinates.

How to get the <X> <Y>coordinates.

Developer options ->  Show touch data.

ShowTouchData

Example: execute automatic touch event in Facebook app:

  1. Launch clear Facebook app (user is not logged in);
  2. Perform long press on Email field;
  3. Note on top of the screen values of X and Y:

FB

On execution of this command, touch is automatically done into “Email or Phone “field and keyboard gets invoked:

adb shell input touchscreen tap 239 1081

See my other post on how to automate the functional steps using adb commands in a bash script: https://wordpress.com/post/mobileqablog.wordpress.com/220

Wireless Android app testing with adb.

I have recently discovered that you do not need to have your Android device under test to be USB-connected to computer at all times to get access to logs, take screenshots, to install or uninstall the application you are testing. Follow the steps below to run all your routine testing tasks wireless.

While phone is attached via usb run in terminal:

adb kill-server

While phone is attached via usb run

adb tcpip 5555

Disconnect the USB cable from the target device.

Find the IP address of the Android device.

adb connect <device-ip-address>

Confirm that your host computer is connected to the target device

adb devices

Hot to run adb commands on multiple Android devices at the same time

Are you using adb command to run it on one Android device? Here is script to save your time and run any adb command on all USB-attached Android phones.

  1. Find adb file and create a copy of it in the same location.
  2. Open copy of adb file with Text Editor.
  3. Select all and change to this:

#!/bin/bash
adb devices | while read line
do
if [ ! “$line” = “” ] && [ `echo $line | awk ‘{print $2}’` = “device” ]
then
device=`echo $line | awk ‘{print $1}’`
echo “adb -s $device $@ …”
adb -s $device $@
fi
done

4. Save this file as “adb+”

6. Connect to your computer several android devices.

5. Open terminal and run command:

adb+ devices

Now you can install/uninstall apk file on multiple usb-attached devices and execute any adb commands using “adb+” instead of  “adb”.

 

Android ADB Setup (MAC)

Step by step guide how to set up Android SDK on your machine in order to perform effective native apps testing.

1. Download Android SDK: https://developer.android.com/sdk/installing/index.html

At the end that page download the sdk relevant to you.

2. Add the platform-tools directory to the $PATH:

2.1. Start up a terminal and navigate to home directory

2.2. Create file .bash_profile:

touch .bash_profile

2.3 Open file with TextEdit:

open -e .bash_profile

2.4 Insert this line into bash_profile:

export PATH=$PATH:/Users/username/custompath/adt-bundle-mac-x86_64-20130911/sdk/platform-tools/

2.5 Save file and reload file:

source ~/.bash_profile

3. In Command Line (Terminal) check if adb was set into path:

adb version