Selendroid Tutorial: Android Mobile Test Automation Framework (Part 1)

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 8, 2024

Mobile Automation using Selendroid (Part-I):

Until now, millions of apps have already been developed using Android as a platform and the count still keeps on increasing day by day.

The catch is, how do we validate each and every app? As we will be requiring dedicated resources such as manpower and real devices (say e.g. mobile), etc., again it is a time-consuming task.

On the other hand, the android version, applications (.apk) version, device dimension, etc., are not consistent.

Hence, it becomes very difficult to define test exit criteria as we cannot define the complete testing scope, thereby exact testing effort estimation cannot be done.

Automate Mobile Applications Using Selendroid Test Framework Tool

Selendroid Test Framework Tool

In order to overcome the device dependency, emulators are being developed, which can provide a real-time environment, where we can test the application without worrying about the android version, devices, and their version. Still testing applications (.apk) over an emulator is a manual activity and a time-consuming task even. Hence the idea of automating mobile testing is being evolved.

To perform mobile automation, a test automation framework is developed, which enables the automation of Android-based applications by using a scripting language.

There are many automation frameworks developed such as Selendroid, Appium, Robotium, and Espresso.

This tutorial is divided into two parts:

Tutorial #1: Selendroid Introduction: Android Mobile Test Automation Framework
Tutorial #2: How to use the Selendroid framework to automate user interactions over a mobile application (coming soon)

What is Selendroid?

Selendroid is an amazing Android mobile application test automation framework tool with several important features. Selendroid is a very powerful automation tool which can interact with multiple devices simultaneously and an application can be tested without any modification or change using Selendroid.

It is also called “Selenium for Android” for testing native and hybrid mobile apps and mobile web.

Why Selendroid?

  • It is open-source (free to use)
  • Supports Selenium as a scripting language
  • Supports web driver compatible languages such as Java, C#, Perl
  • Supports all Android Versions
  • Works on both an emulator as well as a real device
  • Works on native, hybrid, and web-based applications
  • Effective while executing native applications as well as cloud-based applications. as it supports selenium grid
  • It supports object recognition using object properties
  • Easy to implement
  • Hardware devices can be plugged in and unplugged from the PC during test execution without restarting or stopping the test. Selendroid will recognize the new devices automatically. This feature is known as “Hotplugging”

Simple Architecture of Selendroids

Architecture of Selendroid

Selendroid Components

Selendroid – Client

Basically, it is a java client library. From this component, The HTTP & WebDriver request is being sent using JSON to the Selendroid Standalone server.

Selendroid – Standalone

This component manages different devices as well as the .apk’s by installing the Selendroid-server and the app under test. Http requests (to be executed over a real device/simulator) are being sent from the Selendroid standalone server to the device and the apk is under test.

Selendroid – Server

This server runs over the device along with the application under test. There is a two-way communication between the server as well as the application under test.

AndroidDriverApp

It is a built-in Android driver as well as a Web View app to test the mobile web.

Another test framework called APPIUM works similar to a Selendroid.

Here is a small comparison between them both.

FeatureSelendroidAppium
Support for Lower android version(<4.1)YesNo
Hot pluggingYesNo
User Agent TestingNoYes
Support for iOS based applicationNoYes
Environmental RequirementFor Android - Mac, Linux, Windows(any version)For iOS – Mac OSX 107.
For Android – Windows 7+, Linux, Mac OSX 107.

Pre-requisites:

To start with, the Intended audience should be very well aware of the use of selenium, any IDE tools such as Eclipse etc. Basic concepts of programming say Java, C etc…  and must be aware of using Maven and other TESTING tools.

  • Install Intel x86 Emulator Accelerator (not mandatory) to view the simulator from here
  • Java SDK and JRE are already installed in a test environment. Alternately you can download the same from here

Note down the SDK Path while installing.

Set the environmental variables present over My Computer -> right click it -> Properties -> Advance System Setting

Android Path

Java Home

  • Eclipse is already installed on your machine. If not, you can also download the same from here
  • Selenium jar files are to be downloaded. You can download the same from here as well
  • TestNG jars are already downloaded
  • Android SDK to be downloaded and installed. You can also download the same from here

Note down Android SDK Path while installing

Once downloaded the ANDROID_HOME should be set to the environmental variables present over:

My Computer -> right click it -> Properties -> Advance System Setting

Android Path

  • Selendroid jar files are to be downloaded. You can also download the same from here

Remember to download the Selendroid standalone jar file from the above location. Usually, the name is like selendroid-standalone-0.9.0-with-dependencies.jar. Also, download one sample APK file say selendroid-test-app-0.8.0.apk from the above location

Note: 

  • We can also use maven repository to give reference to all the jars or else you have to download them manually and attach them to the build path (Over Eclipse Project)
  • Devices/Emulators – In Android all the installed applications will have the .apk extension. There are other ways you can download apps such as APK Info which will inform you about the list of applications installed on your android device and its actual name, size etc…

Environmental Setup

#1) Keep the APK which needs to be installed over the proper location

In my case, I kept it over G:\\Jars\\selendroid-test-app-0.8.0.apk

#2) Next, keep the application over in the project folder

In my case, it is D:\AppiumAutomation\SelendroidFirst

Local APK

#3) Connect the real device to the PC

Make sure the USB Debugging mode is enabled and you are also allowing external apps to get installed through the USB connection.

Tips:

  • Please refer to the USB debugging settings required for mobile, say Redmi Note 3 from here
  • Allow external applications installed using USB say Redmi note 3 from here

#4) Open the command prompt over Windows

Navigate to the folder where the Selendroid standalone jar, as well as the downloaded apk file, is present.

Now specify a similar command and press enter in the command prompt

E.g. java –jar selendroid-standalone-0.15.0-with-dependencies.jar -aut selendroid-test-app-0.8.0.apk

Or java –jar selendroid-standalone-0.15.0-with-dependencies.jar

APK File: selendroid-test-app-0.8.0.apk

Note: When the above command is executed, make sure that the APK is signed and also the real device is connected to the PC and debugging is enabled over the device.

(Note: Click on the image for an enlarged view)

Command Console

Check the following points over the command prompt to make sure the environment is ready for automation:

  • Verify for message “Device Specified with the valid number”
  • Verify for message “server is started in default port say (4444)…”
  • Verify for message “Session being created…”

Again, we can always reconfirm whether a server is started or not using the following URL over any of your web browsers.

e.g. http://localhost:4444/wd/hub/status

Confirmation of Server

#5) Inspecting the APK’s

  • You can use Real Apps by connecting real mobile devices
  • You can use any offline APK files downloaded

Selendroid Inspector is useful in debugging/inspecting the web elements of APK.

Once the device is identified and started we can view the APK (in virtual mode) and find the references, which is useful for writing the scripts, using the following URL over any of your web browsers.

E.g: http://localhost:4444/inspector

Here we have the screenshot of the application under test and we have the facility to inspect the element as well.

Or you can always use uiautomatorviewer to find the objects from the application under test.

The default path for this is mentioned below:

C:\Users\admin\android-sdks\tools

C:\Users\admin – This is the location where I have installed Android SDK’s but it can be different as per the location specified during its installation.

Selendroid Device

Writing First Script Using APK Files Over Real Devices

Objective:

  • Start the server (on default port say 4444)
  • Create a session
  • Install the app (.apk file) over the connected real device
  • Automate the text field with some data
  • Automatically click on the button
package SelendroidFirst;
import io.selendroid.client.SelendroidDriver;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.common.device.DeviceTargetPlatform;
import io.selendroid.standalone.SelendroidConfiguration;
import io.selendroid.standalone.SelendroidLauncher;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.*;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.VERSION;
public class RealDevices{

public SelendroidLauncher selendroidServer = null;
public WebDriver driver = null;
public void conf() throws Exception
{
//**Configuration**//
SelendroidConfiguration config = new SelendroidConfiguration();
config.addSupportedApp("G:\\Jars\\selendroid-test-app-0.8.0.apk");
SelendroidLauncher selendroidServer = new SelendroidLauncher(config);
selendroidServer.launchSelendroid();

//**Creating capabilities**//
SelendroidCapabilities sc = new SelendroidCapabilities(); sc.setAut("io.selendroid.testapp:0.8.0");	
sc.setEmulator(false);

//**Instantiating new Selendroid driver**//
WebDriver driver = new SelendroidDriver(sc);

//**Sending data to the text field**//
driver.findElement(By.id("my_text_field")).sendKeys("Selendroid Test");

//**Clicking on the button**//
driver.findElement(By.id("visibleButtonTest")).click();
Thread.sleep(10000);
}

Conclusion

Selendroid is a very powerful automation test framework tool which can be used to test native, hybrid as well as a web app on any Android device as well as a simulator.

It supports Hotplugging, which means a user can have multiple devices tested at a time, hence parallel test execution is possible across multiple devices. It supports various kinds of interactions like long-press, touch actions etc…

The only complexity with it is the environmental setup, which is also there in other frameworks. Once it is set up correctly then we can have our scripts run without any difficulties.

In part 2 of this Selendroid tutorial, we will cover – How to use the Selendroid framework to automate user interactions over a mobile application.

In our next article, we will discuss more on pCloudy Hands-on Review Tutorial.

Was this helpful?

Thanks for your feedback!

Recommended Reading

2 thoughts on “Selendroid Tutorial: Android Mobile Test Automation Framework (Part 1)”

Leave a Comment