Android UI testing using ADB

How about I say that you can test your app just by running some commands rather than clicking on buttons and enter values to the edittext? that would be cool right? Like for example if you want to test your login UI by entering new values each time by this method you simply input the text and it will be automatically added to your edit text and even it can click on login button and so on.

First things first, what is an ADB?

So from the official android developers website Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device. you can view the full source here.

In this tutorial we are going to Send text, key pressed and touch events to Android Device via ADB.

For using this first you need to turn on developer options you can do this easily by clicking on your build version or number multiple times. To locate builder number(In my case) go to settings->about

Under the developer options, make sure pointer location is enabled. this is to get the x,y coordinates of touch point which we will later use to get the position of the widgets in the screen. we add click to the widget using this position with the help of touch script.

How to create an adb command?

Navigate to C:\users\yourname\AppData\Local\Android\Sdk this is the default path of sdk while you first install android studio. If you installed sdk on a different path choose that path. inside you will find platform-tools folder.

click on the addressbar and type in cmd and press enter. You will see a command prompt opened within current location. now you can run adb commands easily. Now lets create our first cmd script which will send a tap event in a particle location in the screen. to get the location aka x,y coordinates simply press on the screen where you want the tap event to occur and you can see the x and y coordinates at the top left of the screen.

we are going to use the command adb shell input tap x y where x and y represents the clicked coordinates.

So the command would be

adb shell input tap 153 1053

it will launch the keyboard with username edittext focused. Now if you want add text automatically the username field simply add the following script.

adb shell "input keyboard text 'yourname@gmail.com'"

Now you can do the rest of the commands just like this like clicking on the password edittext and input a sample password. then you could also add script to close keyboard by pressing the onscreen back button and login by adding a tap event on to the login button. next time you want to test another user simply add the username text and password and rest will be handled by the script.

If you like to run all the scripts at a time, Create a text file with name login on the same location and open it. copy paste below commands into the txt file and save it as cmd file(or rename extension as cmd later).

@echo off 
adb shell input tap 153 1053
adb shell "input keyboard text 'yourname@gmail.com'"

now if you run the command you should get something like this

Last updated