In my first post, I explained a simple three-step process to start any automation testing. In the second post, I explored REST API testing, its importance, how to create tests using Rest-Assured and how to integrate them with your ongoing development efforts. In this third and final installment of the automation blog post series, we will cover mobile automation testing, specifically using Appium. When it comes to creating mobile automated tests, there is no better option than Appium. We will discuss why this is the case, where and how we can apply these tests and, as always, integrate these subjects into current development efforts.

Overview

Mobile development has become an increasing industry over the past years, whether it’s native or hybrid-based applications, Android or iOS, apps are everywhere and it is very important to ensure that they work as desired. To achieve certainty in this regard, automated tests can be easily built on top of these applications to continuously test their overall features and confirm that the outcome is as expected. Now, let’s dig into Appium and show how to put it to good use.

Appium

Appium is an open-source testing automation tool used specifically for testing mobile applications and mobile web pages on both iOS and Android platforms. Since it has the advantage of being cross-platform, it allows for testing of the platforms, applications and mobile browsers to be done using their common API. What does this mean specifically? That code can then be reused to perform the same tests on different platforms. However, to run these tests on multiple platforms there are a few things to be taken into consideration—setting up your capabilities properly, for example, which will be discussed later in more detail. First off is installing Appium, which can be easily accomplished with the package manager of your preference. For an example using npm, installation would be done with the following command:

1

npm install-gappium

With Appium properly installed, I would also highly recommend installing the Appium desktop application, as it will help tremendously in identifying UI elements within our applications, which is very important when trying to interact with them. Now, let’s begin with our example code. The first step is to create the Desired Capabilities. Let’s recall that the capabilities act as our “settings” and define a number of these so that tests can run accordingly. This means that capabilities are in charge of making a test run on one platform or another, on a simulator, emulator or real device of our choosing, as well as several other properties. Let’s take a look at how the capabilities setup would be on an iOS platform.

Keep in mind that the Appium API works on multiple platforms, meaning that tests can be easily manipulated via a conditional or initial setup to run on both of the platforms mentioned above and still execute the same test, which presents a major advantage for applications developed using different mobile OS systems. After defining your capabilities, you need to identify your UI elements and define them in your code by using a “locator,” or a means of recognizing these elements so that users can interact with them and build their test. Popular locator strategies include Id, Class or XPath. These elements can then be stored into variables and used later.

But now the question is: “How do we know what locators to use on which element if we can’t see the code?” That’s where the Appium desktop client application comes in handy. This tool can be used not only to interact with an application on a specific platform but also to identify each mobile UI element on any screen and display details for that element, as shown below.

Now that we have defined our UI elements to be interacted with, let’s create our test. As done before in this series, we will use Cucumber and take a BDD approach to the creation of these examples. In this particular case, let’s do a simple login scenario.

1

2

3

4

5

Scenario:Validate successful login tothe app

GivenIlaunch the application

AndIenteravalid username"test.user"with password"tester123"

WhenItap on Sign In

Thenwelcome page should be displayed

Now that we have defined our feature file and the structure of the test, let’s create the essence of our test, which would be the mobile login page object class. If we are to follow a page object-oriented approach, in this class we would define the interactions of the UI elements defined earlier to achieve the goal of the test, which is to properly log in to the application.

Now with our step definitions ready, we make assertions from the results that are given from the UI interaction. These assertions can vary between asserting if an element is present, or a specific page was shown, or the opposite: that no error happened. The sky is the limit here; assertions are made on demand.

1

2

3

4

5

6

7

8

9

10

11

@Then("^welcome page should be displayed$")

publicvoidwelcomePageShouldBeDisplayed()throwsThrowable{

loginMobileObject.assertLogoIsDisplayed();

}

publicLoginMobileObject assertLogoIsDisplayed(){

Assert.assertTrue(logoDashboard.isDisplayed());

deviceDriver.quit();

returnthis;

}

Finally, all that is left is to run our test. As mentioned at the beginning, Appium is an open-source tool and can be used in a number of different programming languages, including Java. Given that fact, and just as stated in the previous blog post, it can easily be integrated into any build tool of your preference: Gradle, Maven or any other that’s executed in an automatic parallel way.

Also, as we mentioned before, using a BDD approach and Cucumber makes it easy to integrate a report runner like Cucumber-Reporting. This, integrated into a Jenkins job that executes the task from the chosen build tool, will have resulted in a successful automated tool base to test our mobile applications and their desired behavior.

Conclusions

Appium is by far one of the best tools out there for mobile automated testing, as it has an easy, standards-driven approach for creating tests, supports multiple platforms using the same code base, and is open-source code, allowing for it to be run with a number of programming languages. With all of these advantages, the Quality Assurance department of any team can take advantage of this and begin creating automated test suites for their mobile applications without the need to separate the code base or their methodologies, ensuring high-quality tests in a very short period of time.

Rodolfo has been a .NET/backend developer for around five years now. He started working on automation testing technologies in 2014 with a focus on UI/UX. Rodolfo enjoys watching movies and solving jigsaw puzzles in his free time.

Thank you for your kind words Tom. I have read through the post suggested and I find it fascinating. The variety of tools that can be used to leverage different types of mobile tests is astonishing as well as the vast pros it presents to have cloud based-testing such as parallel execution on multiple devices is a great advantage.