April 2, 2020 Jonny Steiner

Android Automation: The Complete End-to-End Guide

Overview
Tools and Technologies
Types of Applications
Espresso vs Appium
Appium
Android Studio installation
Installing Appium Server
Getting Mobile Automation started using Appium
Prerequisites
Android device recognition
Starting Appium Server
Using Inspector Session
Basic Automation Test Creation
Test Authoring
Executing the Test

Overview

Mobile Automation refers to automation done on Mobile Devices. In general, this automation is done on Applications that run on Mobile Devices.

This blog focuses on automating applications, covering tools and technologies used primarily in Android automation Devices.

Tools and Technologies

Almost 80% of the Mobile devices in the market are Android-based, thus it becomes of utmost importance for an Automation developer to make use of tools which have following features,

  • Stable and easy to use.
  • Large developer community base.
  • Support of a broad set of programming languages.

 

Types of Applications

Before we delve on Automation tools, let’s classify Applications.

  • Native Application – Application developed for a specific platform.
  • Web Application – Applications built to run on Browser (In this case, Mobile browsers).
  • Hybrid Application – Applications that have both Native and Web components but run as an application.

Following are the most popular Mobile Automation tools available,

  • Appium
  • Espresso
  • Robotium
  • Selendroid
  • UI Automator

Based on the features mentioned before, Appium is the most popular and preferred choice for Mobile Automation for Android devices. The most important reason for Appium as a preferred choice is because the same programming language can be used to automate both iOS and Android operating systems.

Espresso provides excellent performance on test execution.

It can be a good option when,

  1. Application developers are of the opinion that automation tests are an integral part of development. This enables rapid speed in implementing automation since the developers have familiarity with the codebase and also the fast execution speed of Espresso tests.
  2. If Application will only be supported in the Android Operating System.

Espresso vs Appium

FeaturesAppiumEspresso
OwnerOpen SourceGoogle/Open Source
ScopeCross-PlatformAndroid
App TypeNative, Hybrid, WebNative, Hybrid
LanguagesJava, C#, Ruby, Python, etc
( Complete list)
Java/Kotlin
Source RequiredNo
Yes
Out of AppAndroid – Yes
iOS – No
No
SpeedSlowFast
SetupHardEasy
UsageHard (previous Selenium usage helps)Easy for Dev, Medium for SDET
StabilityLowHigh
CI IntegrationHardMedium

Appium

Appium is by far the most widely Android Automation tool, it is Open source and Cross-Platform. Appium is actually developed as an HTTP server using NodeJS.

Basic Architecture

At the heart of Appium tool, its key components are,

  1. Appium Server – An HTTP Server that listens to the Appium Http based clients and uses its UI Automator driver to translate automation commands to language which Android devices can understand.
  2. Appium framework exposes Appium Clients for different languages like Java, Python. Supported languages are listed in Appium clients.
  3. Appium supports different drivers to automate various end platforms. UIAutomator2 Driver is used to support the Android Operating system.

Appium Clients and Appium Server communication

Appium Client and Server use JSON Wire protocol over HTTP to communicate with each other. Communication between Appium clients and Appium Server could be complex and to overcome this complexity, the data is exchanged in JSON format which is structured.

To kick off the communication, a session is created as an initial step. Desired Capabilities are key-value pairs that are preferences for this session. These preferences include mandatory information (apart from other optional information) like the platform name, device name, application path which is the key information in automation.

Here is an example of the data sent via the JSON Wire Protocol with the desired capabilities to establish a session to Appium Server.

{
"deviceName": "a3ae1c63",
"platformName": "Android",
"app": "C:\\Users\\example.apk"
}

Android Studio installation

Appium Studio is the Integrated Development Environment for developing an Android application. It is built on JetBrains IntelliJ which has a large developer community base and also can be used for automation using UI Automator and Espresso.

Please download the Android Studio Software

Windows

  1. If downloaded as an executable file, double click and launch it. If downloaded as an archive file, extract it to a directory and launch studio.exe in the <extracted directory>/android-studio/bin
  2. Follow the setup wizard, install any recommended SDK packages during installation.

Mac

  1. Launch the Android Studio DMG file
  2. Drag and drop Android Studio into the Applications folder, then launch Android Studio.
  3. Select whether you want to import previous Android Studio settings, then click OK.
  4. Follow the setup wizard, install any recommended SDK packages during installation
  5. For detailed instructions please refer to the official page of Android Studio.

Installing Appium Server

Appium Server

Appium Server can be installed using two ways explained below.

Using NPM

  1. Download Node
  2. Double click the downloaded file and follow the steps in the wizard to finish the installation of Node.
  3. Once NodeJS is installed execute, npm install -g appium
  4. To run Appium Server execute, appium

Using Appium Desktop

  1. Download the Appium Desktop
  2. Launch the Installer of the platform of your choice and follow the setup wizard.
  3. Run the Appium Desktop installed in the machine, this also launches the Appium Server.

For detailed instructions on how to install refer to the official page of Appium.

Appium Clients

Appium Clients are actually libraries that Appium provides for various languages it supports. There is no installation required, these libraries have to be used by the Test Developers in their tests.

Getting started Mobile Automation using Appium

This section explains

  1. Getting started with basic test creation using IntelliJ with Java as a programming language and Appium Java Client library.
  2. Execute the developed test against Appium Server.

 

Prerequisites

This section needs following as prerequisites

  1. Installation of Android Studio with SDK, because ADB gets installed as part of Android SDK.
  2. Installation of IntelliJ 2019 version
  3. The latest version of Java
  4. Download sample Android automation application called Eribank using link https://experitest.s3.amazonaws.com/eribank.apk

 

Android device recognition

Before we move forward with Appium and Mobile automation, it is important that your PC detects the Android device. To accomplish this, the device needs to be connected to PC and developer mode/USB debugging needs to be enabled.

  1. Connect the device to PC using a USB cable.
  2. In the android device navigate toSettings > About Phone and click ‘Build number’ until you become a developer.
  3. Once you are in developer mode you need to enable USB Debugging by navigating to Settings > System >  Developer Options > USB debugging. See the screenshot below.

Note: The USB debugging option may change depending on the Android version. The steps above are for Android 10.

Once these steps are completed the ADB utility needs to be used to list the connected android device.

  1. Open the command prompt
  2. Navigate to the Android SDK path \platform-tools\
  3. Execute the command ADB devices. You should get the device name.

Note: The device name (a3ae1c63) got from this command, needs to be used later in the Mobile Automation Session (in manual or automation test).

Starting Appium Server

In this section, we will use Appium Desktop for running the Appium Server. Appium Desktop is an excellent utility using which you can run an Appium Server and as well as Start Inspector session to load the application manually and find out the location elements of the Application under automation.

To start Appium Server,

  1. Launch the installed Appium Server in your machine (Example: In Windows from Start Menu Programs)
  2. Click on the Start Server button.
  3. This will start Appium Server (localhost:4723) as shown in the screenshot below. Notice Start Inspector Session to start the inspector session.

Using Inspector Session

Inspector utility of Appium can be used to create a manual session and then find the element location identifiers

Launch the Inspector Utility from the Appium menu as shown above in the Start Appium Server section.

Specify the mandatory Desired Capabilities as shown above and click Start Session.

Once you click the Start Session, a session is established to Appium Server, which mediates to the specified device in ‘deviceName’ capability. Appium Server installs the application specified in ‘app’ capability and Inspector utility will display the Application.

Inspector utility allows you to click any element in the application and displays all the properties related to the element which can be used to locate it at the runtime. See the screenshot below, the Username element is selected and all the attributes of this element are displayed in the right pane.

 Please go through Finding Elements in Appium to know more about locating elements in Appium.

For example in this case element ‘Username’ can be located using xpath syntax:

//*[@text='Username']

 

Basic Automation Test Creation

Create a basic Gradle Java Project

Launch IntelliJ and click Create New Project

Select Gradle and Java in next screen and Click Next

Fill in Project details (GroupId and ArtifactId) and Click Next

Click Next in this screen (keeping the default selection intact)

Click Finish in this screen (keeping the default selection intact)

With these steps, a basic Gradle project is created in IntelliJ.

Gradle Dependency

For successful compilation, the project needs the Appium Client bits and TestNG (Widely used Java Testing framework).

To accomplish this, open build.gradle and update the ‘dependencies’ field with the following and save the file.

dependencies {
compile group: 'org.testng', name: 'testng', version: '+'
compile group: 'io.appium', name: 'java-client', version: '+'
}

This will download all the dependencies (Appium Java Client and TestNG testing framework libraries) into the project.

Test Authoring

Create a Java package name “test”

Create a Java class named EribankTest.java under the ‘test’ package.

Paste the following setup function in the EribankTest.java

@BeforeTest
public void setUp() throws MalformedURLException {
// Desired Capabilities as previously explaining in Appium Inspector section
dc.setCapability(MobileCapabilityType.APP, "C:\\eribank.apk");
dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.experitest.ExperiBank");
dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".LoginActivity");
dc.setCapability("platformName","Android");
dc.setCapability("deviceName", "a3ae1c63");
// Driver initialization to Appium Server
driver = new AndroidDriver&lt;&gt;(new URL("http://localhost:4723/wd/hub"), dc);
}

The function above sets up the Android Driver with the desired capabilities for the session initialization. This function basically installs/launches the Application eribank.apk in the device a3ae1c63.

We now will create a test which will login to the Eribank application.

@Test
public void testEribankLogin() {
// Location of elements got from Appium Inspector
By userElemBy = By.xpath("//*[@text='Username']")
// Send text to the username field
driver.findElement(userElemBy).sendKeys("company");
// Send text to the password field
driver.findElement(By.xpath("//*[@text='Password']")).sendKeys("company");
// Send click command to the login button
driver.findElement(By.xpath("//*[@text='Login']")).click();
}

Copy the created test function above and paste in the Eribank.apk. We get our final java file as shown below.

Executing the Test

To run the test, we need to create Run configuration by navigating to IntelliJ menu Run > Edit Configuration. This will popup the screen below. Click on ‘+’ and TestNG to create a run configuration.

Note: You may need to add the TestNG plugin to IntelliJ.

Select the Run Configuration and fill the Name, Click ‘…’ to choose the Test class (EribankTest.class) and click OK, as shown below.

Now execute the test by navigating to Menu > Run > EribankTest

The example project associated with this blog is located at Android Automation Sample project.

Guy ArieliCTO