If you have followed my series on Installing Appium on Windows then you should have Appium installed on your machine now but you are probably wondering what you are supposed to do next, and how exactly do you go about writing automated tests? Fear not! The next posts in this series on Appium from Scratch are going to guide you through preparing your app for automation, by first teaching you how to interrogate your app.
In this post we are going to continue with our focus on Android on a Windows environment, but look out for a post that covers iOS and Mac in the near future. To be honest, the concepts are very similar on iOS (you need a .IPA file instead of a .APK) file, but the nature of Apple Software Development makes for a few other hurdles that have to be jumped through…
Anyway let’s get started! To inspect your Android app, there are two options available to you; The Appium Inspector and the UIAutomatorViewer. It is actually recommended by the Appium team to use the UIAutomatorViewer, but for completeness I have included instructions for them both. In these examples we are going to use a calculator app . Go ahead and download that APK file now before continuing.
THE APPIUM INSPECTOR
We will begin with the Appium Inspector. First launch the Appium application, you should be able to find it on the Start menu, mine is located at “C:\Program Files\Appium\Appium.exe” . The Appium console should launch as in the screenshot:
Click on the Android icon to open the Android options.
Change the APPLICATION PATH to the location of your APK file.
Make sure you tick LAUNCH AVD and choose the AVD that you created in previously (see this post if you need a reminder).
All of the other options you should be able to leave the same.
Your settings should look similar to the screenshot on the right hand side.
Now click on the WHEEL icon to open the General Settings.
We need to make sure that “Pre-Launch application” is checked.
Everything else we should be able to just leave the same.
Here is a screenshot of those options on the left.
That’s it, we should be ready to go! Click the PLAY button in the Appium console. You should see a wall of text appear and start to scroll through, and eventually the AVD will launch. After the AVD has started (it could take a few minutes depending on your machine spec) the app should be installed and will launch automatically.
The Appium inspector is critical for interogatting iOS apps, but for Android applications it is actually recommended even by the Appium team themselves to use the Android UIAUTOMATORVIEW tool. Fortunately, we will have already installed this tool when we setup Android Automation on our environment, so let’s look how to get it running!
The first thing we need to do is to launch our AVD and install our APK to it. So go ahead and launch your AVD (see this post if you need help on that) and once the AVD has fully loaded open a command prompt, browse to the folder that you downloaded the APK to and type the following:
adb install Calculator_v1.2.3_apkpure.com.apk
This will install the app for us on the virtual device. On the AVD, go ahead and click the options menu button then find the Calculator application and launch it. Now we need to open the UIAutomatorViewer application. In a command prompt, type the following:
cd %ANDROID_HOME%/tools uiautomatorviewer.bat
This will launch the UIAutomatorViewer application. Once it has loaded, click on the “Device Screenshot” icon to load the details of the currently displayed page:
After a few seconds, an image of the current screen will load in the UIAutomatorViewer and you can use the mouse to click on any element, to see the details of that element. You can also use the hierarchical tree in the top right to browse to a particular element. Again in the screenshot we can see what it looks like when we target the number 7:
For Android automation, the UIAutomatorViewer is the way to go, as it gives us extra control by being able to click on elements. Go ahead and click on a few different elements on the screen and have a look at the details that are updated for each element.
To summarise this post, we have made an important step on our journey to automate our apps, by learning how to interrogate our app to find the details of the various elements that are displayed. However, we still don’t know what we need to do to actually target those elements in the code. In the next post we will explore how we can use the detail of elements identified in the Appium Inspector / UIAutomatorViewer to begin writing XPath code that will ultimately target the elements in our code. See you there!