With the lessons we learnt from part 1, we can now go ahead and write some example tests.
Let’s say for example, we want to test the ‘hamburger’ menu on the maps app. Using our Appium inspector, we can utilise the ‘tap with co-ordinates’ feature to tap on that menu element, while we record the action to see what the code to repeat that action would look like:
To re-cap, you can see that any action we carry out in the Appium inspector, happens in the emulator. And in the centre of that screenshot we can see our code-snippet to reproduce that action in our tests. (Remember we can choose a language from the drop-down).
For arguments sake, let’s say we choose Java as our language. Let’s create a project in IntelliJ:
In our pom.xml, we’ll add our dependencies like so….
Let’s say we just wanted a quick (and dirty) example of how to automate some of the actions we talked about earlier, and perform a simple test, just to illustrate the end result of all of the steps we’ve gone through.
With that in mind, below is some basic code to perform a simple test….
I’m not joking when I tell you that the XPath for the element is so long it doesn’t fit on the page! I might be wrong, but when I inspected Google Maps, most of the elements are only locatable by XPath 😦
If we run that class, (with Appium still running its server in the background), we should see the code interact with our Android emulator and run through those steps, finally asserting that the Google Maps menu has opened when we tapped on the ‘hamburger’ menu button.
In the left-hand-side of the image above, we can see our execution finished successfully and the test passed.
On the righ-hand-side of the image, we can see our emulator with the menu we tapped still open, showing the menu-option we are looking for “Your Places”.
So there we are, as promised, a very simple, start-from-scratch tutorial on automation testing of Android apps. If you wanted to upload your own app to the emulator, you should be able to drag the .apk file onto the emulator’s screen to invoke the apk installer dialog for you to do that.
What’s missing? Well if I’m honest, we wouldn’t create a test-suite this way. We would write our code in a much more structured and SOLID way, but what we have here is just to illustrate setting everything up in order to get going.
Hopefully this 2-parter tutorial has helped you on your journey to successfully automating Android apps, if you like this tutorial please do leave a comment below, thanks!
© 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.