Android, ClickOnScreen(x,y) shell script function for automated testing, part #2

Hello everybody,

Part 1 with essential basics here.

Now it is time to create a simple and useful shell script with one function – ClickOnScreen(x,y).

PART 2: implementing simple shell script with ClickOnScreen(x,y) function.

Prerequisites:
1. Terminal;
2. You favourite text editor;
3. Configured and run Android environment + test device/emulator.

Okay, so our main point is to create an automated script which will be producing random number of clicks with random delay to random points at the screen.

Lets start! 🙂

Open your favourite text editor (I prefer gedit), and enter following snippet:

#!/usr/bin/perl -w

sub main {

system(“adb shell sendevent /dev/input/event2 0003 0048 000000104“);
system(“adb shell sendevent /dev/input/event2 0003 0050 000000010“);
system(“adb shell sendevent /dev/input/event2 0003 0053 000000558“);
system(“adb shell sendevent /dev/input/event2 0003 0054 000000057“);
system(“adb shell sendevent /dev/input/event2 0000 0002 00000000“);
system(“adb shell sendevent /dev/input/event2 0003 0018 00000047“);
system(“adb shell sendevent /dev/input/event2 0003 0020 00000001“);
system(“adb shell sendevent /dev/input/event2 0000 0000 00000000“);
system(“adb shell sendevent /dev/input/event2 0003 0048 00000000“);
system(“adb shell sendevent /dev/input/event2 0003 0050 00000010“);
system(“adb shell sendevent /dev/input/event2 0003 0053 000000558“);
system(“adb shell sendevent /dev/input/event2 0003 0054 000000057“);
system(“adb shell sendevent /dev/input/event2 0000 0002 00000000“);
system(“adb shell sendevent /dev/input/event2 0003 0018 00000032“);
system(“adb shell sendevent /dev/input/event2 0003 0020 00000000“);
system(“adb shell sendevent /dev/input/event2 0000 0000 00000000“);

0;
}

exit main(@ARGV);

Save it as Click.sh in the android/tools folder (or anywhere if you have exported adb variable into the PATH).

Run it:

~$ ./Click.sh

(./ is used for running script from current folder)

Find a coordinate on your screen you want to click, to check the result of the clicking and modify this script for your own coordinate.

I guess our current single click script is not very cool now 🙂 But, we can easily modify it and create a useful and flexible click method, to do that, we need to start with introducing a function which will be sending events:

sub adbSend {
my ( $device, $type, $code, $val ) = @_;

system(“adb shell sendevent $device $type $code $val“);
}

It is getting 4 params, if you like, you can cut $device parameter and use it as constant. By this way, our click transforming into the following sequence:

sub main {

adbSend(“/dev/input/event2“, 3,48,104);
# looks like next event is optional:
#system(“adb shell sendevent /dev/input/event2 0003 0050 0010”);
adbSend(“/dev/input/event2“, 3, 53, 558);
adbSend(“/dev/input/event2“, 3, 54, 57);
adbSend(“/dev/input/event2“, 0, 2, 0);
# looks like next event is optional:
# adbSend(“/dev/input/event2”, 3, 18, 47);
adbSend(“/dev/input/event2“, 3, 20, 1);
adbSend(“/dev/input/event2“, 0, 0, 0);
adbSend(“/dev/input/event2“, 3, 48, 0);
# looks like next event is optional:
#system(“adb shell sendevent /dev/input/event2 0003 0050 0010”);
adbSend(“/dev/input/event2“, 3, 53, 558);
adbSend(“/dev/input/event2“, 3, 54, 57);
adbSend(“/dev/input/event2“, 0, 2, 0);
# looks like next event is optional:
# adbSend(“/dev/input/event2”, 3, 18, 32);
adbSend(“/dev/input/event2“, 3, 20, 0);
adbSend(“/dev/input/event2“, 0, 0, 0);

0;
}

Now we need to introduce some more cool functions:
1. Set x,y coords;
2. Finger touched screen;
3. Finger released screen;

After additional investigation I have found 4 optional events, so we able to combine finger down/up into one function:

#!/usr/bin/perl -w

sub adbSend {
my ( $device, $type, $code, $val ) = @_;

system(“adb shell sendevent $device $type $code $val“);
}

sub Click {
my($x, $y) = @_;
adbSend(“/dev/input/event2“, 3,48,104);
adbSend(“/dev/input/event2“, 3, 53, $x);
adbSend(“/dev/input/event2“, 3, 54, $y);
FingerDownUp(“/dev/input/event2“, 1);
adbSend(“/dev/input/event2“, 3, 48, 0);
adbSend(“/dev/input/event2“, 3, 53, $x);
adbSend(“/dev/input/event2“, 3, 54, $y);
FingerDownUp(“/dev/input/event2“, 0);
}

sub FingerDownUp{
# $isDown: 1 – finger down, 0 – finger up
my($device, $isDown) = @_;
adbSend($device, 0, 2, 0);
adbSend($device, 3, 20, $isDown);
adbSend($device, 0, 0, 0);
}

sub main {
$x = 10;
$y = 1000;
for($stepsIndex = 1; $stepsIndex < 25; $stepsIndex++) {
# Kinda of random xDD
$x = $x * $stepsIndex;
$y = $y/$stepsIndex;
print(“X:$x;Y:$y“);
Click($x * $stepsIndex, $y/$stepsIndex);
sleep 1;
}
0;
}

Now you are able to loop “random” clicks 🙂

Use sleep s; where s – seconds for timeouts. For debugging you can use print(“message”); function.

Part 3: Unlocking device using shell script, introducing swipe motion

Happy clicking (:

Best regards,
Yahor