Skip to content

Category: Perl for Android automation

Perl for Android automation

The tutorials are designed for beginners who have basic knowledge of below points:

1. Mobile application testing.

2. Basic adb(Android Debugging Bridge)commands .

3. If you know basics of automation testing, it would be a plus.

What Will I Learn?

How to automate Android devices also learn how to write automation scripts for Mobile testing (Native application). You will also learn performance testing mainly stress testing.

What you need to install?

1. Install adb drivers(device drivers) click here to download.

2. Install active perl, click here to download.

3. Install notepad++(Optional) click here to download.

Table of contents  –

  1. What is Stress testing wrt a mobile handset and Introduction to perl.
  2. What is adb(Android Debugging Bridge)
  3. How to establish interactions between Perl scripts and android device using adb commands.
  4. Some important scripts to perform stress testing on android native apps.
  5. Advantages and disadvantages.

1. What is Stress testing with respect to a mobile handset?

Testing the stability and response time of an application by applying load which is more than that of the designed number of users. For example we will make the messaging application to send / receive 10,000 msgs continuously for a period of time, by this we are applying stress to the application which in-turn might make the application brake/fail.

1a. What is Perl?

Perl was created by Larry Wall in 1987 when he was working on a bug reporting system “AWK”- a programming language he was using for the purpose was not helping him much. He is still the chief architect and developer of Perl. If we want to define Perl in one statement: Perl is a high level, interpreted, dynamic programming language.

NOTE: In this tutorial I will not teach more about Perl language, But will soon write a tutorial on the same. 🙂

2.What is adb(Android Debugging Bridge) ?

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:

  • A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
  • A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.

You can find the adb tool in <sdk>/platform-tools/.

When you start an adb client, the client first checks whether there is an adb server process already running. If there isn’t, it starts the server process. When the server starts, it binds to local TCP port 5037 and listens for commands sent from adb clients—all adb clients use port 5037 to communicate with the adb server.

The server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections.

Enabling adb Debugging

In order to use adb with a device connected over USB, you must enable USB debugging in the device system settings, under Developer options in system settings.

Syntax

You can issue adb commands from a command line on your development machine or from a script. The usage is:

adb [-d|-e|-s <device serialNumber>] <command>

3.How to establish interaction 

between Perl scripts and adb ?

Android packages which defined in Perl helps to interact with adb and similarly adb interacts with android device. Android packages has package manager as well as activity manager,package manager help to trigger a package and activity manager use in order to trigger any event or operation.Thus, Perl establishes connection between android packages using adb.

Example:

system(“adb -s $dev_id shell am start -a com.android.intent.action.MAIN -n com.android.mms/.ui.ComposeMessageActivity”);

system: Is used to provide the input. Here we perl will take the use of adb and adb intern does the job.

-s: Is used to provide the name of specific device which the user want to execute the test scripts.

Shell: adb provides a Unix shell that you can use to run a variety of commands on an
emulator or connected device. The command binaries are stored in the file system of the emulator or device, at /system/bin/…

Two of the most common command tools are activity manager (am) and package manager (pm). For more: Click here.

am start -a com.android.intent.action.MAIN : Activity Action: Start as a main entry point, does not expect to receive data. Here -a means action.

-n com.android.mms/.ui.ComposeMessageActivity: triggering activity. -n means component name.

4. Example script for applying stress to the device under test:-

Device LCD ON/OFF stress test –

print “###############LCD ON/OFF###########”;
system(“adb devices”); #prints the connected device id’s
print “Enter device id:”;
$dev_id=<STDIN>; #takes the input from user
for($i=1; $i<=50;$i++)
{
print “\\n********* LCD on off Iteration $i on device : $dev_id **********\\n”;
system(“adb -s $dev_id shell input keyevent 26”);
sleep 1; #device sleeps for 1 second
system(“adb -s $dev_id shell input keyevent 82”);
}

 

Device Camera image capture stress test –

print “\\n########Click an Image#########\\n”;
system(“adb shell input tap 300 400”); #give the x and y axis value as per the camera icon is displayed on the device screen.

for($i=1;$i<=100;$i++)
{
print “\\n ************ iteration $i ************\\n”;
system(“adb shell input keyevent 66”);
}

Device Browser stress test –

print “\\n########BROWSER#######\\n”;
system(“adb devices”);
print “Enter device id:”;
$dev_id=<STDIN>;
chomp($dev_id);
for($i=1;$i<=5;$i++)
{
if($i==1){

print “\\n******************* Iteration $i ************** \\n”;
system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘google.co.in’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘androidpolice.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);
}

if($i==2){

print “\\n******************* Iteration $i ************** \\n”;

system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘rhbroms.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘in.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);

}

if($i==3){

print “\\n******************* Iteration $i ************** \\n”;

system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘skype.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘apple.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);

}

else {
print “end”;
}
}

5. How to execute these scripts –

  1. After the complete installation of perl, write your scripts using notepad ++ and save the file with .pl extension.
  2. Connect your android device.
  3. Double click on the saved .pl file and you are good to go.

6. Advantages of using Perl for stress testing –

  1. It is open source, and platform Independent.
  2. Easy to understand and interpret.
  3. Faster execution.
  4. Can be used for Non-functional testing mainly.

7. Disadvantages of using Perl for stress testing –

  1. Not good for Automating the Functional test cases.
    As it is time consuming to write the scripts and we have other
    automation tools which does the job in few steps.
Leave a Comment