Cross-Platform Android and iOS Automation Testing with Appium

Did you know that Appium allows you to write a single test script and execute it on both Android and iOS for automation testing? There is no need to duplicate a ton of code by creating two separate, almost identical test scripts! Keep on reading to find out how.

How does writing a single script that runs on two different platforms work?

The first tool you will need for this is Appium.

There are different Appium setups you can use, but PageObject is the ideal approach for this specific testing purpose. You can read more about PageObject setup in our documentation.

So let’s imagine you are using a PageObject-based setup to write your test scripts for your Android calculator app. At this point, you are probably referencing the majority of the UI elements you are interacting with, using annotations, possibly like the ones shown below:

@AndroidFindBy(id = "net.ludeke.calculator:id/plus")
private MobileElement buttonPlus;

@AndroidFindBy(id = “net.ludeke.calculator:id/equal”)
private MobileElement buttonEquals;

Now let’s image that you have build a calculator application for iOS as well and you want to reference these same elements on it. How do you do this?

Well it is in fact quite simple: you can just add another annotation on top of the existing ones, like so:

@iOSFindBy(id = "plus_sign")
@AndroidFindBy(id = "net.ludeke.calculator:id/plus")
private MobileElement buttonPlus;

@iOSFindBy(id = “equal_sign”)
@AndroidFindBy(id = “net.ludeke.calculator:id/equal”)
private MobileElement buttonEquals;

So what the code above does is: whenever an Android driver is being instantiated for your test, the @AndroidFindBy annotation will be used; when the iOS driver is being instantiated, the @iOSFindBy annotation will be used.

There is also no need to stick to IDs: you can specify a different referencing strategy depending on the platform and it will look like so:

@iOSFindBy(accessibility = "plus")
@AndroidFindBy(id = "net.ludeke.calculator:id/plus")
private MobileElement buttonPlus;

So you can choose the referencing strategy you like most.

The big benefit in writing a single script for both Android and iOS automation testing, is that you are not duplicating as much code. This means: easier maintenance of your test scripts and less headaches!

Written by

Ely Hechtel

Topics

AppiumAutomated testing