Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Tips for Android Application Automation Testing

0.00/5 (No votes)
5 Oct 2012CPOL3 min read 22.6K  
Testing for Android is especially important because of differences between the devices. Here are some tips.

Introduction

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
  • Version
  • 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: <android_sdk>/ platform-tools /. The path to this directory is recommended to write in PATH environment variable.

Checking the ADB Running

Install and customize the Android SDK, connect Android devices to your computer and run the next command:

PHP
adb devices

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 –s key:

PHP
adb-s <serial_number> <command /></serial_number>

For example:

PHP
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:

PHP
adb shell

Running command on the device:

PHP
adb shell <command />

There are many standard Linux utilities in Android, such as ls, cat, dmesg, etc.

Installing application from apk file:

PHP
adb install example.apk

Uninstalling application:

PHP
adb uninstall <package />

Getting the name of the package from apk file:

PHP
aapt dump badging example.apk | grep "package"

Receiving files from the device:

PHP
adb pull <path-on-device> <file /></path-on-device>

Sending files to the device:

PHP
adb push <file> <path-on-device /></file>

Note

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:

PHP
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:

PHP
aapt dump badging example.apk | grep "launchable-activity"

Reading Logs

logcat utility allows reading additional log messages in Android. Read logs from the device (locked until you press Ctrl+C):

PHP
adb logcat

Clean log buffer on the device:

PHP
adb logcat-c

Dump the log to the screen and exit:

PHP
adb logcat-d

Screen Capturing by Utility screenCap

ScreenCap utility saves the current contents of the screen to a graphic file:

PHP
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)