
“R2D2! You know better than to trust a strange computer!” – C3-PO
Wise words there from one-half of the comedy-duo element of the Star Wars franchise. It’s why we test stuff… but where to start?
Goal
This tutorial will hopefully help to give a ‘start-from-scratch’ overview of testing Android apps using arguably the most popular tools for the job – Android Studio, and Appium.
Step 1 – Tool Up
Grab your tools. You’ll need the following:
A) – Android Studio – download from https://developer.android.com/studio/
You’ll have several options. For the purposes of this tutorial I’m on a Windows machine, so I’ll be using that download; but to give you some idea, here are the options:

Click on the option you need, and then follow the installation wizard’s instructions. You can keep the defaults for the locations etc, I’ve changed them to my preference, but it’s optional.
I then added the Android home directory into my ‘.bash_profile’ file:

B) – Appium – download from http://appium.io/
Simply click on the ‘Download Appium’ button in the centre of the screen. Again, once that is downloaded, simply follow the installation wizard’s instructions.
Step 2 – Create Your Project
Open Android Studio, create a new project.

You’ll be given some options:

For the purposes of this tutorial, we’ll stick to the basics. We just want the Android Virtual Device manager so we can create an emulator, so let’s just choose the simplest option to get there. From those options above, we can see that we want to choose ‘Phone and Tablet’ and ‘Basic Activity’. So choose those, and you’ll see the project dialog box appear…

Once you’re happy with the setup, click ‘Finish’.
Your project in Android Studio will look something like this:

Now we can start the process of creating our Android emulator….
The emulators are the virtual-machines that look and behave the same way as Android devices. They have the same features, same OS and same default apps. You can build these to be different models, have different versions of the Android OS etc. Let’s build one now….
Step 3 – Create An Emulator
In the top right-hand corner of the Android Studio IDE you’ll find a button that looks like a mobile device with a green Android under it; that’s the button to open the AVD Manager, where you create your Android Virtual Devices. Click it, and you will see the following…

You’ll notice there is already an emulator present. If you want to create a new one, click the ‘Create Virtual Device…’ button in the bottom-left corner. Once you have one created, you can click the green ‘play’ button in the devices ‘Actions’ column. Mine complained about HAXM not being installed at this point. HAXM is an accelerated processor, and I simply followed the instructions to install it, and enable it.
I also encountered this error:

This is to do with the computer’s virtualization and may well be disabled in your BIOS. To get rid of that error, I restarted my machine and entered the BIOS, and enabled VT-x. (Nb: if you have to do this, Google your machine’s specific BIOS instructions as they differ. For me, I hit the ESC key on boot-up until I saw a menu to enter BIOS; hit F10, and changed the Vt-x value to ‘enabled’).
Once Android Studio is a happy bunny again, you should see the emulator appear once you click ‘play’ as mentioned above:

Ok, now we’re ready to play. You can see the emulator is quite literally a virtual representation of a real Android device.
Now open a terminal window. I use Git Bash, but Command Prompt, or iTerm2 on a Mac, or whatever you use will do. Go into the drive where you installed Android Studio; in my case, I put it on my D: drive and set the ANDROID_HOME in my .bash_profile (see above).


When I type the following command ‘adb devices’:

…we can see that there is a device attached; it’s our emulator, and it has the name ’emulator-5554′.
Step 4 – Appium
Ok, so now let’s go ahead and open Appium.

In simple view, you can see the Host is localhost (you can put 127.0.0.1 but no need), and the Port is 4723 – this is the default port for Appium. We can also see the start button which tells us the version we have downloaded.
You may need to setup some environment variables such as where your Android Home is, so click on ‘Edit Configurations’ and add them accordingly. Mine are…

With that done, let’s start it up by clicking the blue ‘Start Server’ button….

In the image above, we can see the Appium REST http interface listener is started on the host and port combo we chose. Where I have added a white arrow in the top right-corner, you can see ‘Start Inspector Session’… click that button.

I’ve filled in the details we need. You can see the desired-capabilities include the device name we saw above, ’emulator-5554′ which is our virtual android phone. The ‘appPackage’ name and ‘appActivity’ name can be found using a command ‘adb logcat’ in the following fashion:

… I really do advise you pipe the output to a file like above, because the log output is, er, verbose! Putting it in a file will make it easier for us to find the app name in that log output. Hit ‘Enter’ on the above command, and the log will be running its output into that file for us….
…now we want to open the app in our virtual Android machine…. So open the Google Maps app in the emulator…

We can stop the output to that log file already at this point. So let’s go back to the bash window, and hit ‘Ctrl-C’ to stop the output. Then we can open that file and search for the words ‘for activity’… specifically with the words ‘google’ and ‘maps’ 😉 … we should be able to find our app name. In our case, it’s ‘com.google.android.apps.maps’. The activity is ‘com.google.android.maps.MapsActivity’.
The other value I’ve added to the desired-capabilities is ‘noReset’ set to true, which means we won’t be resetting the app state before this session. (For a full list of desired-capabilities, go to ‘http://appium.io/docs/en/writing-running-appium/caps/‘).
Step 5 – Start Playing With Elements
So now we have those values in our desired-capabilities, let’s go ahead and click on the ‘Start Session’ button, and we should see the following:

We can see that the ‘Select Elements’ button is already clicked for us. If we motion over the app on the left of the above window, a yellow box appears over the elements as we hover over them. In the below example, I’ve motioned over the search input field of the map:

If we click on the search bar element, the inspector will give us information about the element like below:

It even tells us that using XPath is a bad idea and perhaps the developers should have thought of making the app more testable; well, I’m paraphrasing 😉
Other cool features in this inspector window include being able to swipe. Now, if you arrange your windows so that you can see the inspector window and your Android emulator side-by-side, you’ll see that if we swipe the app in the left-hand-side of the inspector, it actually performs that action in the Android emulator as well. E.g.

How cool is that? Another one is ‘tap by co-ordinates’, which on our maps app for example, allows us to tap on marked locations on the map like a parking lot, and Google Maps will highlight it both in the app shown in the inspector window, and in the Android Emulator that is attached to it via our desired-capabilities, like so:

You might ask what use is all this beyond giving us the element locators? Well, you can hit ‘record’ and perform actions like the ones above, and the inspector window will output code to perform those actions, which we can then port into our tests. I’ve shown this below, while also showing you the drop-down that lets you change the language of that code to suit your tastes…

So, all of the above puts us into the position of being able to start playing with Android apps and interacting with the elements in the app, so that we can perform the actions we need in order to test the app.
Step 6 – Let’s Write a Test Finally!!
Please follow this link to go to the next part of this tutorial, where we’ll be putting the lessons from here into action, and writing some tests for the app.
Thank you for reading this far!
© Copyright 2022 Cognito Square Ltd

Like what you see? Please support us with a donation.
Support Cognito Square Ltd with a one-off donation in order to help us continue to publish helpful QA Automation and Agile Project Delivery Articles.
£5.00