Testing is a very important part of the application development process. Testing for Android is especially important because of differences between the devices:
- Screen size and resolution
- Form factor
- The presence of front-facing camera, NFC, external keyboard, etc.
That’s why you need test applications on plenty of devices. Testing process consists of different testing types.
Let’s consider manual execution of functional testing. The tester sets up the application into the device, thoughtfully checks all the functionality, and then returns the device to its original state. The same actions are for each application and device. As you know, regular testing is time consuming and that is a big disadvantage of this technique.
Regular execution without big expenses is an obvious benefit of automated testing. For example, you can test a new application build on the available devices every night and analyze the results and fix the bugs in the morning.
Below we’ll consider the means of automated testing implementation. These are only included in the Android SDK or released under Open Source license tools.
The Automated Test Concept
Our task is automating the actions which were performed by the tester in the most accurate way. We have several applications and Android devices. Let’s apply the next steps for each of the items:
- Install application on the device
- Run the application
- Test the application by selected technique
- Delete the application
- Reset the device
You need to collect and analyze data like logs and screenshots at every step. After that, you need to generate the test results based on these data form.
Android Devices Management
First, you need to find the computer on which the automated test will be run and customize the Android SDK. Examples are provided for computers with installed Linux.
Disable the lock out screen and maximize the latency-time on all tested devices. For some test techniques, you need to switch off changing screen orientation.
There are two utilities for devices management on the Android SDK: adb and MonkeyRunner.
Controlling by Android ADB Utility
ADB (Android Debug Bridge) is a utility to manage Android devices from the command line.
Adb utility is located in the directory:
Checking the ADB Running
Install and customize the Android SDK, connect Android devices to your computer and run the next command:
The command will show connected devices list. ADB is up and running if the device list is not empty.
Working with Several Devices
To show ADB the device to work with, you need to write the serial number of that device after the
adb-s <serial_number> <command /></serial_number>
adb-s <serial_number> logcat</serial_number>
You can see the serial number of the device by adb devices command. The
–s key allows you to work with several connected devices at the same time, sets the default filter spec to silent. We won’t specify the
-s key in the following commands.
Basic ADB Commands
The next command begins shell connection with device:
Running command on the device:
adb shell <command />
There are many standard Linux utilities in Android, such as
Installing application from apk file:
adb install example.apk
adb uninstall <package />
Getting the name of the package from apk file:
aapt dump badging example.apk | grep "package"
Receiving files from the device:
adb pull <path-on-device> <file /></path-on-device>
Sending files to the device:
adb push <file> <path-on-device /></file>
Only read-access is available for most of the device directories. Write-access is allowed into the next directory:
/ sdcard (you can’t run the program from it) and
/ data / local / tmp /.
Running the application:
adb shell am start-n <package> / <activity /></package>
Running the specified activity. You can get from apk file name of the activity that is run when you select application in the menu, by the next command:
aapt dump badging example.apk | grep "launchable-activity"
logcat utility allows reading additional log messages in Android. Read logs from the device (locked until you press Ctrl+C):
Clean log buffer on the device:
Dump the log to the screen and exit:
Screen Capturing by Utility screenCap
ScreenCap utility saves the current contents of the screen to a graphic file:
adb shell screencap / sdcard / screen.png
adb pull / sdcard / screen.png screen.png
adb shell rm / sdcard / screen.png
ScreenCap utility is available on phones with Android 4.x and the next versions. On previous versions of Android, capturing of screenshots can be made by MonkeyRunner.