Test on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

LT

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

Kane
Book A Demo

How To Automate Android Apps Using Appium

Wasiq Bhamla

Posted On: February 20, 2023

In this age where new technologies are coming up rapidly, it’s very important to keep up with speed and ensure the highest application quality is delivered to the customers. If we see the user base of different operating systems, we can see that Android is currently the market leader worldwide. Let’s see this market trend:

mobile operating system market share

Many companies want a mobile version of their web applications or even directly create mobile applications for their customers. Therefore, it must be tested thoroughly to ensure customers get quality applications.

When I started with mobile automation testing in 2017, I explored many automation testing tools, like, Calabash with Ruby, Selendroid, and Appium, but out of those, I found Appium is one of the most popular automation tools, and it was relatively easy to get started because of the similarities it had with Selenium WebDriver. Also, it had support with many different programming languages like Java, JavaScript, C#, Python, Ruby, etc., which gave more flexibility to everyone who could use Appium in the language of their choice.

This mobile app testing tutorial on how to automate Android apps will help you know everything to get started with Android automation testing.

What is Appium?

Appium is an open-source mobile app testing framework and an ecosystem to help automate Android apps and iOS apps categorized across different mobile-based applications, like native, web, and hybrid. It also supports automating smart TV and Windows/macOS-based desktop applications.

Since Appium came up with the new version 2.0.0, it has introduced many new changes which further enhanced the overall functionality of Appium. To mention a few recent changes, it has now decoupled drivers from its command line installation. It also introduced a way to override, alter, extend, and add new functionality to Appium by creating Plugins. It also encourages using the W3C protocol for automation compared to earlier Mobile JSON wire protocol.

If you’re new to Appium, this tutorial can provide an easy-to-follow guide on performing mobile app testing.

Getting started with Android app automation

For Appium to run successfully and automate Android apps on your machine, you must set up your machine correctly. Let’s see what setup is required to be done to automate Android apps.

Install Node JS

You can install Node JS v16 LTS using Node Version Manager (NVM) by executing the following command on the terminal or command prompt on your machine.

This command will download the installation script and run it on your machine to install NVM.

Once NVM is installed successfully, you can install Node JS 16.x.x LTS using the following command.

Once this is installed, you can confirm the installed Node version by running the following command:

Pre-requisites

You must also download and install the following to compile and run your tests successfully:

  • JDK 11: You can download the installer from the link provided for your machine and install it, or you can also use Homebrew on your macOS machine to install the JDK using the following command:
  • Maven: You can download the distribution package from the provided link, unzip it in the location of your choice, and set the M2_HOME, or you can simply run brew install maven on your macOS machine.

Install Android Studio

To install Android Studio, download the installer from the Android download page and run it. Once the installation is done, open SDK manager and install the following selected packages:

SDK manager

Notice the package highlighted in red. This package will only get visible when you uncheck Hide Obsolete Packages.

All these packages are required to automate Android apps.

The last step is setting up the ANDROID_HOME environment variable pointing to the SDK path. Also, we need to update the PATH variable with paths to emulator, tools, platform-tools, and cmdline-tools paths.

Install Appium Server CLI

To run Appium to automate Android apps on your machine, you will need to install the Appium Command line tool to manage the server and drivers. To install the Appium CLI, run the following command on your terminal:

This will only install the latest Appium v2.0 CLI on your machine. Run appium -v to confirm that v2.0.0 was installed successfully.

Get Available Appium Drivers

With Appium 2.0, they have decoupled the Appium Server and drivers. So when you installed Appium CLI, it did not install any drivers. To see all the supported drivers by Appium, run this command:

drivers by Appium

Install Appium Driver

Now, from the list of supported drivers, you can install any of the required drivers as per your requirement. To install a specific driver, in this case, the uiautomator2 driver for Android, run the following command:

Install Appium Driver

To check if the driver is installed, you can again run the command to list all the supported drivers, and it will show a tick (✔️) next to the installed driver.

Install Appium Doctor

To check whether your machine setup is done correctly now, you would need appium-doctor to help us check it. To install appium-doctor, run the following command:

Check Machine Setup

Once Appium doctor is installed, run the following command to check machine setup to automate Android apps:

machine setup

Start Appium Server

To start the Appium Server using Appium v2, run the following command:

Once the server starts, it’s ready to connect to this server instance, communicate with the device, and execute your test using this session.

To stop the server, you must press Ctrl + C.

Setting up the Android Device

The last step of starting with Android automation using Appium is setting up the device to make it ready for Appium to connect with the device. The device can be a virtual Emulator or your real device. Let’s see how to set up both kinds of devices properly.

Create Android Emulator

Let’s create an Android Emulator using Android Studio. Firstly open Android Studio and click More Actions > Virtual Device Manager as shown below:

android studio

This will open up Device manager, which will contain a list of any existing virtual devices and provide an option to create new ones. This is what the device manager screen looks like:

device manager

Start the Emulator

To start the emulator, you can click on the (▶️) button in the device manager screen corresponding to the device you want to start. Once you start the emulator, it will take some time to load. Check out a sample emulator screen below:

start the emulator

Why use Cloud Platforms?

If you don’t want to set up a local emulator and still wish to inspect elements for the Android application, you can use cloud-based devices. Before we move ahead, let’s first understand what cloud providers are. Cloud providers are test orchestration & execution platforms like LambdaTest, which provide an online device farm of 3000+ real devices and operating systems to perform real device cloud testing at scale over an Appium grid. Using LambdaTest, you can perform app test automation on real Android and iOS devices.

You can also subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials around Selenium testing, Cypress E2E testing, CI/CD, and more.

Here are some of the features of LambdaTest Appium automation platform –

  • Test native features of your mobile apps
  • Automated device testing on blazing fast test automation cloud.
  • Support for all Languages & Frameworks
  • Comprehensive test execution logs
  • Geolocation testing of mobile apps.

Setting up the Cloud Platform

For this blog on how to automate Android apps, we use the LambdaTest cloud platform. On the LambdaTest Dashboard, navigate to Real Device > App Automation on the left-hand panel. Once on that page, click Upload App to upload the application. Check out the upload app page shown below:

upload app

Once you upload the application APK, LambdaTest will return you with a unique application URL in the format lt://, which you must copy and keep safe as it will be required later when you try to inspect element from the cloud or try running your Android test on the cloud.

Identifying Locators

The first step after you have done the setup on your machine is, identifying the element locators for the Application Under Test (AUT). Let’s look at how to inspect elements for Android applications.

Install Appium Server

To inspect elements, it’s better to use the Appium Server desktop application, which will give us plenty of server options that we can set up as per our requirements. To install the Appium Server application, you can download it from their GitHub Release page. From that page, download the application which is compatible with your machine. Since I am using a MacBook, I downloaded the .dmg installer file from this release page.

Once you have downloaded the installer compatible with your machine and installed the application, it will look like this:

install appium

To start the Appium Server with default settings, press the startServer button. You can also modify server settings from the Advanced tab. You can save the server settings you modified for later use by clicking the Save as Preset button under the Advanced tab. These saved presets will be available under the Presets tab.

Install Appium Inspector

Next important tool is the Appium Inspector application itself. This is also a desktop-based application that you can use to connect to a local Appium Server session; we started using the Appium Server application and the local device (whether Emulator or Real device) or even a cloud-based device where it will install the application under test on the device, launch it and will help you inspect the elements of the application.

To download the Appium Inspector application, visit their GitHub Release page and download the application compatible with your machine’s operating system. Once the application is downloaded and installed, it will look like:

appium server

On this screen, you can create desired capabilities for different devices and save them for later use.

With these capabilities, you can select any capability you want to run and the server instance where you want to run this capability. It can be either a local Appium Server or any of the supported cloud platforms, which you can select from the Select Cloud Provider tab, which will list all the supported providers.

Inspect the element on the device using the Inspector

To inspect the element, you will select the capability from the Appium Inspector and update the server details to connect to and then click on the Start Session button. Let’s see how this screen looks before you start the session.

source

Here one thing to notice is the base path, which is provided as /wd/hub, and we don’t have to provide Remote Host and Remote Port as it defaults to the localhost and port is 4723 which is where you started the Appium Server using the server application.

Once the session starts, the Android Emulator will start as mentioned in the capability avd where you must mention the Emulator name you created using Android Studio’s Virtual device manager. This is how the inspector screen looks when the session is started.

source

On this screen, you can click on the element on the application screen, mirrored on this inspector from the device. You will be able to see the properties of that element on the right-hand panel. You can see all the element hierarchies in the XML tree in the center panel.

Inspect Element on Cloud Device

To inspect an element on a cloud device, you must get your cloud credentials which you can get from the page when you visit Real Device > App Automation, which can be seen here below:

source

You can copy the user name and access key by clicking on the copy button next to them. Now open the inspector window, click Select Cloud Providers, and select LambdaTest from the list of providers. Check out the screen below:

source

When you select the desired cloud provider, you can see it in the main window tabs. Select that tab and add the desired capabilities for starting the session on LambdaTest. Let’s see the example in the next screenshot where I have selected LambdaTest and added all the desired capabilities along with additional LambdaTest-specific capabilities identified with the help of the LambdaTest Capabilities Generator.

source

Once everything looks good, click Start Session. The Appium Inspector application will establish a connection on LambdaTest. Once the session is started, you will see the inspector screen as follows:

source

Note: One thing to note here is, while setting the app capability in the inspector, is that I am passing the App URL provided by LambdaTest when the application was first uploaded on their servers.

You can confirm that the inspector session has started on LambdaTest by navigating to the LambdaTest dashboard and clicking on Real Devices > App Automation. You will see that the build and session you provided in the capability in the inspector will be visible running on the dashboard. The same is shown in the screenshot below:

source

You can get more details about the session when you click on the session name.

source

Writing the Appium test for Android App

Since you are now aware of how to inspect elements, the final step is to write an Appium test for an Android application on an Android device. In this blog on how to automate Android apps, IntelliJ IDEA Community Edition is used. You are also suggested to use IntelliJ, but it’s up to you which IDE is your preferred one.

In this blog, the following test scenario will be covered, which will help you in getting a basic understanding of how to automate Android apps using Appium.

  • Use the proverbial application provided by LambdaTest and test the Hybrid page, which contains WebView, where we will enter a URL, navigate to that web page, and validate if we are on that page.
  • Run the test on a local emulator.
  • Run the test on LambdaTest cloud real device.

Code examples used in this blog are available here:

github

Feel free to clone the repository and follow along as I walk you through the repository in this blog.

Creating a Maven Project

To create our project, let’s open IntelliJ IDEA and create a new project using Java 11 JDK and Maven. Make sure you have mentioned “group id” and “artifact id” in the Advance Setting section as shown below:

Creating a Maven Project

Adding Dependencies

Once the project is created, open the pom.xml file and add the following dependencies:

Let’s understand why we have used these dependencies:

  • Appium Java client: This dependency is required to automate Android apps.
  • TestNG: This dependency executes our tests on a local emulator and the cloud.
  • Google Truth: This dependency is used to assert our test to ensure validation of our test.
  • Guava: This is required for the Appium Java client to execute without any issues. Without this dependency, the Java client gives an Error while executing.

Starting Appium Server programmatically

When automating with Appium, it is essential to start the Appium Server from our test to control the test execution. To start an Appium Server programmatically, you must build the Appium service. Let’s see how to build the service:

This method builds the AppiumServiceBuilder class instance with the required attributes per your machine. The attributes we set here are:

  • host: We get the hostname from the system property. If it is not set, we will use host 127.0.0.1 by default for localhost.
  • port: We set the port number from the system property. If it is not set, we will use port 4723, the default on which the Appium Server will start.
  • log file: We are setting the log file name where the Appium Server will save all the server logs in this file.
  • Appium JS path: We are setting the Appium main.js path from its installation folder. Normally when you install Node JS using NVM, then when you install Appium, its path is under the ‘.nvm’ directory.
  • Base path: The base path for the server, which is normally /wd/hub.
  • Use Drivers: We set all the drivers we will use here. In this example, we will be using uiautomator2.
  • Allow insecure: We will automate a WebView in a hybrid application in this example. So to automate the web view, we must set the insecure flag chromedriver_autodownload to allow the Appium Server to automatically download the Chrome driver compatible with your device’s web view.

Now to start the Appium Server, you need to call the start method as shown below:

And to stop the Appium Server, you must call the stop method like:

Start the session on Local Emulator

Now your Appium Server is ready to start and accept sessions and connect your tests with the running device. To start the session, you must first create a set of capabilities describing your test device and the application under test. Let’s see how to achieve this in the following code snippet:

In Appium Java client v8.x.x, you can now use the UiAutomator2Options class instance to build the capabilities. In this example, we are setting:

  • Platform Name: This is set as Android as we automate Android apps.
  • Platform Version: This is to set the Android version passed from the system property or use 11 by default if the property is not set.
  • Device Name: To set the Android device name, which will be passed from the system property, or use Pixel_6_Pro by default. You must ensure that you pass the device name via system property as it may fail if not passed because you may not have the Emulator with the default name.
  • Avd Name: This is the same as the device name. When this capability is set with the correct Emulator name, Appium will launch the Emulator if it is not started already.
  • App Path: This is set as the application APK file path from your machine.
  • Auto Grant permission: This is set to true by default, allowing all the permissions required by the application under test.
  • Is Headless: This is set as the boolean value passed using system property to determine whether to run on the Emulator in a headless mode. If set to true, the Emulator UI will not be visible, and the emulator will run in the background. However, if the Emulator is already running and visible, this flag value will not be considered.

To start the session on the Appium Server, you must pass these capabilities to the AndroidDriver instance along with the server URL. Let’s see how to achieve this:

You can get the server URL using the service instance we created earlier and pass the capabilities we built to the AndroidDriver instance a while ago. This will establish the connection between the Appium Server and the device. If not started already, it will launch the emulator and install and start the application under test on the device.

Pro Tip: We are updating the driver settings to set IGNORE_UNIMPORTANT_VIEWS as true. What this will do is it will clean up the element hierarchy of the element displayed on the screen. You will only see those elements in the hierarchy which are visible. This will speed up the process of finding the elements later on.

To disconnect the session with the Appium Server, you must call the quit method from the driver instance as shown below:

Start the session on the Cloud Device

To connect our tests to run on LambdaTest cloud devices, you must first set a few environment variables on your machine which will be used in the test setup. Those environment variables can be set by adding the following lines in your .zshrc or .bash_profile file if you are on Mac, or an equivalent file on Linux or by manually adding to the Windows environment variable window:

Once these is set, now let’s see how to set up the cloud capabilities below:

Let’s break down this code snippet. First, we are building the cloud platform URL by concatenating our cloud user name and access key in the URL itself. Secondly, we have created the capabilities specific to the LambdaTest cloud and saved it as a Map, which is later passed on to the UiAutomator2Options instance class.

To know what capabilities are supported by LambdaTest, refer to the LambdaTest Capabilities Generator and play around to see what capabilities you want. The best thing about this is that it also generates a Java code snippet which you can refer to when writing your tests.

To connect your test to run on the cloud device, you need to pass the cloud URL and the capabilities we built in the previous step to the AndroidDriver instance as shown below:

To stop the cloud session, you must call the quit method as demonstrated before for running on a local emulator.

Creating Page Objects

Next step is creating a page object. Since we are looking into a hybrid screen in the Proverbial application used in this blog on how to automate Android apps using Appium, we will have this minimal page object just to understand the basic interaction Appium allows us to do:

So, on the test screen, we click on the Browser tab to navigate to the hybrid screen of the application. It contains a text box where the user will enter the website address, which you want to navigate using the sendKeys method. Then click the Find button using the click method, which will load that website in the web view component below on that same screen.

In this page object, we have a method for all the elements we will interact with in the complete flow of the tests, which we will cover in the next section. We also have the navigateTo method, which will be called from the test with the intended URL passed as the parameter.

We also have a method called webTitle, which is nothing but the h1 tag of the website we will be visiting in our test, where we will assert the text of this title element by getting the text of the element using the getText method.

Writing Test Classes

Let’s put everything together in our test class. Following is the test class content:

First, in this test class, we have a setup method, where we initialize the driverManager class and specify where we intend to run the test on. If we want to run on a local emulator, we pass false in the parameter of the createDriver method, and when we want to run on the cloud instance, we pass true.

Next, we have a teardown method where we close the driver session, which internally quits the driver and closes the Appium Server on a local emulator.

Next, in our test method, we initialize the HybridPage instance, where we navigate to the browser tab, enter the URL https://www.lambdatest.com and click on the Find button. Later we verify the web page title to verify that the page is loaded correctly.

One thing to note is the context method, which tells Appium to switch its context from the native to the web view so we can find the elements from the web page visible in the web view on that screen.

You can get all the available contexts on the screen using the getContextHandles method, which will return the context names. This name you can use to switch the driver context according to your requirements.

https://accounts.lambdatest.com/register

Running Appium tests to automate Android Apps

Before you run the test, you must create the testng.xml file. In our case, we will create two files, one for local execution called testng-local.xml and another one called testng-cloud.xml for executing on the cloud platform.

Let’s check out one of the testng.xml files to understand how to set up TestNG to run our test:

This file is for running on a local emulator. In the other file, we will have the value of the isCloud parameter to true.

Running tests on CLI using mvn

To run our tests using the Maven command line, we must first tell Maven what TestNG file needs to be executed and all the required system properties. For this, you must update your pom.xml file with the following build block as shown below:

Here we are using the maven-surefire-plugin plugin, and we specify the testng.xml file using the suiteXmlFile property. We are using the target property to differentiate which TestNG file to run, and we have declared the target property in the properties block with the default value as local. This means that if you don’t pass the system property of target, the local target will be used by default, and the local test will be executed.

Once this is setup, you can run the following command to run on the local emulator:

And run the following command to run on the cloud device:

Check out the execution of tests using the command mentioned above:

execution of tests

Running tests on IDE

Now to run our test on the IDE, you can simply right-click on the testng.xml file and click on the Run option as shown below:

Running tests on IDE

When the test runs for the first time, it will fail because you have not yet set the system properties and specify the deviceName and deviceVersion. Let’s see how to update the system properties in the IDE.

You can edit the run configuration created when you first ran the test by clicking the “Edit Configurations…” option as shown below:

Edit Configurations

This will open the configuration window, where you can mention the system properties required by your test. You can specify the system properties in the field as shown in the below screenshot:

specify the system properties

Once everything looks good, you can click on Apply and OK to save the configurations and then click on the (▶️) button next to the run configuration you saved, This will execute the tests in your IDE, and you will be able to see the run results in the corresponding panel in the IDE as shown below:

panel in the IDE

Reporting of the test result on LambdaTest

When your run is started on the cloud device, you will be able to see the real-time test execution video streaming and steps being executed on the device from your tests on the LambdaTest Dashboard. To navigate your test:

Log in to the dashboard and click Real Devices > App Automation on the left-hand side navigation panel. You will see the below screen:

Real Devices App Automation

On this screen, you will see all the test executions you have ever run. To reduce the clutter, you can filter the records based on the “Project name” you mentioned in the cloud capabilities while executing the tests. Once the filter is applied, you will see only those tests which are a part of that project.

When a test is running, you will see it running in the right section of this screen with the status RUNNING. When you click it, you will be able to see all the real-time interactions of the test.

Once the test execution is completed, you can see loads of details about the test when you click and open the test from the right section. This is what you will see first when you open the test session:

test execution is completed

On this screen, you will see the video of the tests, all the test steps that were executed, and the time it took to run that step, along with the screenshots at the time of executing that step.

You can also see the device logs of the executed test when you click on the “Device Logs” tab as shown below:

device logs

Similarly, you can see the network logs of the tests by clicking on “Network Logs” as shown below:

Network Logs

And similarly, you can see the Appium Server logs from the cloud server by clicking on “Appium Logs” tab as shown below:

Appium Server logs

Conclusion

To conclude and recap this Appium tutorial on how to automate Android apps using Appium, we learned about running Appium tests on an Android device on your local machine as well as on the LambdaTest cloud platform. We also learned how to use the new Appium 2.0 for automation, setting up our machine, setting up the required drivers, automating a hybrid app, and interacting with different elements like a button, text field, and web view. This example is enough for you to get started with Android automation and automate any type of application you may work on in the future.

Don’t forget to check out the GitHub repository used for this blog to automate Android apps.

If you liked this blog on how to automate Android apps, don’t forget to share it with your network, who may also benefit from it and help them learn about Appium.

Frequently Asked Questions (FAQs)

How do you automate apps on Android?

There are several ways to automate apps on Android:

  • Using Appium
  • Using Espresso
  • Using UI Automator

Each of these methods has its advantages and challenges, and the choice of method depends on the specific requirements of your testing scenario.

How to automate Android apps using Java?

You can use the Appium framework to automate Android apps using Java. Follow the below-mentioned steps to do so:

  1. Set up the environment:
    • Install JDK
    • Install Android Studio and Android SDK
    • Connect your Android device to your computer
  2. Create a new Java project in your preferred IDE
  3. Add the Appium Java Client dependency to your project
  4. Launch the Appium server
  5. Create an AppiumDriver instance to connect your test script and the app
  6. Interact with the app’s elements using the AppiumDriver methods
  7. Write and run your test cases

How to automate Android apps using Python?

To automate an Android app using Python, you can use Appium, an open-source tool that allows you to automate Android and iOS apps using various programming languages, including Python. Here are the steps to automate an Android app using Python and Appium:

  1. Install Python
  2. Install Appium
  3. Install Appium-Python-Client
  4. Set up the environment
  5. Write the test script
  6. Run the test script

Author

Wasiq Bhamla

Wasiq is a Test Automation specialist having more than 15 years of experience in Manual and Automation testing. He has vast experience in automating Desktop, Web, API, Android, and iOS based applications. He is also an active open source contributor on GitHub, a freelancer, a mentor, and a blogger.

Blogs: 3

linkedintwitter