Development

Automated mobile testing with Appium, JUnit and Maven

Publicado por
Gerardo Carrillo
Automated mobile testing with Appium, JUnit and Maven

Context:

Due to the constant technological change and different platforms, it has become a very valuable task to include automated regression tests in IT projects. These tests help to improve the quality of the product with functional tests that simulate the actions of a user, in addition, these tests are executed automatically in each update of the system, which allows finding defects before adding these changes to the final product.

About Appium...

Appium is an open-source test automation framework that can be used with native, hybrid and mobile web applications, and controls iOS, Android and Windows applications using the WebDriver protocol.

How does it work?

Appium is a web server that exposes a REST API. It receives connections from a client, listens for commands, executes those commands on a mobile device, and responds with an HTTP response representing the result of the command execution.

appium

Before we begin...

To enhance the understanding of this tutorial it is suggested to be familiar with:

•  Webdriver

•  XPath

•  Java

•  Junit

•  Maven

Let's get started!!!

•  Step 1

Our first step will be to set up the machine with all the dependencies, software, plugins, environment variables, etc. For this we need to install the following:

•  Java (JDK) (Link here)

•  Android Studio (Link here)

•  Appium desktop (Link here)

•  IDE IntelliJ (Link IntelliJ here)

Optional: We install Android studio in order to have sdk tools to virtualize Android devices. In addition, we will have to configure some environment variables:

•  JAVA_HOME: (folder jdk)

•  ANDROID_HOME: (folder sdk tools)

Finally, we will have to download a version of chromedriver according to the version of Google Chrome that will be installed on our virtual device.

•  Chromedriver (Download web site here)

•  Step 2

We open Android Studio to virtualize an Android device using the "AVD manager" tool.

Android Stidio

Selecting this option will open a window where we can create our Android device.

Virtual devices

Select the "Create Virtual Device" option, which will take us to a series of screens that will allow us to select the Android version, model and whether or not the device has the App Play Store.

Select device to virtualize

Select Hardware

Select Android version, you must download the desired version by clicking on the "Download" hyperlink.

System Image

License Agreement

Component Installer

Component Installer

On the last screen, enter the device name (we will use it later) and finish.

AVD

We finished and checked that the configuration was successful by virtualizing the configured cell phone with the "Play" button.

Virtual Devices

Virtual Devices

•  Step 3

In this step we will open and configure the "Appium" application to be able to connect to our device in order to run our tests.

Open Appium desktop

Appium

Select the "advanced" tab and go to the end of the options looking for the item "Chromedriver Bynary Path" where we will add the path in our computer where our "chromedriver.exe" is downloaded.

Apium Advance

Since we will use the default configuration, after adding the path we will be able to start our appium server by pressing the "Start Server..." button showing the Appium console.

Appium server

Step 3.1

Now that we have our Appium server running, we will be able to connect to our virtual appliance without any problems.

For this example we will test the cleveritgroup website in the Chrome browser, for this we need to interact with the elements of our mobile so we need to be able to identify these elements.

Appium offers us a way to connect with the phone to be able to inspect each of the elements that it shows on the screen.

To be able to connect we will need to create a new inspection session using the "magnifying glass" icon or from the File -> New Window Session tool menu.

New session windows

A new window will appear where we must configure the connection parameters to our mobile device, these parameters will be the same that we will use in our automation script. These parameters are:

•  platformName : "[Android or IOS]"

•  deviceName: "[name of the virtual device we used in step 2]"

Automatic Server

Once these parameters have been entered, we will start our virtual device (in case it is not turned on) we will press the "Start Session" button and we will obtain a result like this:

Start Session

When selecting an element, from the Appium interface, it will show us the details where we will be able to see the attributes of this selected element, as in the following image:

Start Session

With this we will be able to identify the elements we need to interact with.

NOTE: This is most useful when testing on created Apps as we could identify the browser elements using the desktop browser together with the developer tools.

developer tools

Step 4

In this step we will start creating our automation script using the intelliJ IDE. We will start by creating a new Maven project

new project intelliJ IDE

Enter a name for the project

new project intelliJ IDE

Click on "Finish" and you will get a result like this.

Finish result

Step 5

After creating the project, we will add the dependencies to our "pom.xml" file, these dependencies can be searched for in the official Maven repository, the dependencies we will use will be:

•  Java-client

•  Selenium-java

•  Junit

Our file would look like this:

file

Now we must make a "Build" of our project so that the corresponding dependencies can be downloaded and continue with the creation of the classes and tests.

Step 6

After adding the dependencies we will create the "BaseConfig" class, in the location "../src/test/java/", where we will make the initial configuration of the "WebDriver" to generate the connection with our Android device.

BaseConfig

Our class will have 2 methods:

•  setUp()

•  tearDown()

To these methods we will add the annotations "@Before" (setUp) and "@After" (tearDown). These annotations come from Junit. The annotations will be used to ensure that at each start (@Before) and at the end (@after) of each test cycle, these methods are executed.

BaseConfig

Step 6.1

The "setUp()" method will have the connection parameters to our android device, for that we must create an attribute to the "BaseConfig" class called "driver" of the type "AppiumDriver< MobileElement> " .

BaseConfig

Then we will add some parameters of type "DesiredCapabilities", these are:

•  Automation name: automation engine (UiAutomator1, UiAutomator2 , XCUITest,etc)

•  Platform name: Platform (Android, IOS)

•  Platform version: Platform version (Android version, IOS version)

•  Device name: Name of our virtual device

•  Browser name: Browser we will use for testing (Chrome, Gecko, etc.)

DesiredCapabilities

After the configuration parameters, the connection to our Android device will be created.

configuration parameters

With these steps we could already connect to our Android device, but not perform any action.

Step 6.2

Inside our tearDown() method we will add the script to close the connection to our device at the end of the execution.

Step 7

Now that we have all the necessary configurations, we will create the "SimpleTest" class that extends the "BaseConfig" class where we will add our test.

BaseConfig

Inside, we will create a method called "show home page" along with Junit's "@Test" annotation.

show home page

We add our code that will consist of 2 instructions and a validation with which we will be able to evaluate if our test behaved as expected. The instructions will be:

•  Google "cleverit Group" search

•  Open search result

•  Check that the URL is that of our official site.

show home page

To be able to do these steps it is necessary to identify the elements with which we must interact, in this guide we will use the method "findElementByAndroidUIAutomator" that helps us to identify elements by using the UISelector class.

Once we have identified our elements and actions, we will add a small log in the console that will allow us to follow our automation.

findElementByAndroidUIAutomator

findElementByAndroidUIAutomator

Step 8

Finally, we have to run our project and visualize the results. For this we will need to have started our Appium desktop and virtual device, right click on our "SimpleTest" file and press "Run SimpleTest" obtaining a result like this.

SimpleTest

Conclusions

Mobile test automation is something not yet exploited as much as web automation. Appium is a good start in this field as it precedes selenium which helps to have a better understanding of the framework.

For those who are starting to test on mobile, I would recommend that they determine an expected result of the automation because there are many tools in different languages, and it is necessary to identify the objective of the automation in order to select an appropriate tool. This is because each one has its limitations and benefits at the time of use.

Repository with example project:

appium-junit-maven-tutorial

What’s a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.

Descarga nuestro Clever UI KIT 👇

Acá en Clever Experience trabajamos en un pequeño UI KIT que puede ayudarte en la próxima propuesta rápida, idea o proyecto que debas o quieras desarrollar.
Ingresa tu nombre y correo para descargar.

Gracias. Te será enviado un mail confirmando la inscripción
¡Ups! Algo salió mal al enviar el formulario.