Running Appium Tests on Android Emulator

Appium allows us to execute our tests on both real devices and emulators/simulators. We have seen earlier on how to execute tests on real Android Devices. However, we may not have enough real devices to test always with different versions of android. In such cases we can use android emulators that emulates real device software, hardware and OS.

To run emulator, First we need to define an AVD which defines the hardware characteristics of the emulator like Device, Target API, CPU/ABI image and other options. So here Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Android Wear, or Android TV device that you want to simulate in the Android Emulator.

We can create emulators in two different ways:
1. Creating an emulator using AVD Manager
2. Creating an emulator using Command Line

The AVD Manager is an interface you can launch from Android Studio that helps you create and manage AVDs.

Creating an emulator using AVD Manager (Android Studio)

Step 1: - Open the AVD Manager by clicking Tools > Android > AVD Manager.

Step 2: - Click on Create Virtual Device, at the bottom of the AVD Manager dialog.

Select Hardware page appears with list of devices.

Android Appium with Google Play

Notice that some system images are labeled with Google APIs, which means it includes access to Google Play services. Other system images are labeled with Google Play, which means it includes the Google Play Store app and access to Google Play services.

System images with Google Play currently only work with AVDs targeting Nexus 5 and 5X. In Android Studio's 2.3.2+ AVD Manager displays with Play Store Column and Icons.

Step 3: - Select any device, and then click Next.

Step 4: - Select the system image for a particular API level, and then click Next.

The Recommended tab lists recommended system images. The other tabs include a more complete list. The right pane describes the selected system image. x86 images run the fastest in the emulator.

If you see Download link next to the system image, click on it to download the system image. Make sure you are connected to the internet to download it.

Step 5:- Give any name to the device and click on Finish.

Now the devices will be displayed under the Android Virtual Devices with play and edit options. Now Select the AVD name and click on Start/Play icon uder actions section.

The default value is 5554 for the first android virtual device instance running on the your machine. If we launch an other emulator, it will be assigned with an even numbered port after 5554 i.e second emulator will be assigned with port 5556. In the same way we can initialize more than one AVD at the same time and a maximum of 16 AVDs.

Check out official Android documentation on Create and Manage Virtual Devices

Creating an emulator using Command Line :

Android avdmanager is a command line tool that allows to create and manage Android Virtual Devices (AVDs) from the command line. An AVD lets you define the characteristics of an Android handset, Android Wear, or Android TV device that you want to simulate in the Android Emulator.

The avdmanager tool is provided in the Android SDK Tools package (25.3.0 and higher) and is located in android_sdk/tools/bin/.

AVDManager location Android studio

Create AVD using below command, the following command creates an AVD named test using the x86 system image for API level 25.

avdmanager create avd -n test -k "system-images;android-25;google_apis;x86"

android-25 - API Level
google_apis - Variant name
x86 - CPU architecture

Based on your requirements, you can change API level / ABI etc.

Below are few commands to view available targets, device definitions, or AVDs. If you do not specify target, device, or avd, avdmanager lists all three.

- list : Lists existing targets or virtual devices.
- list avd : Lists existing Android Virtual Devices.
- list target : Lists existing targets.
- list device : Lists existing devices.

Here's the basic command-line syntax for starting a virtual device from a terminal prompt. (Navigate to tools folder where emulator )

emulator -avd avd_name [ {-option [value]} … ]
emulator @avd_name [ {-option [value]} … ]

Let's view the avd that we have created, enter following command

 emulator -avd test

Create AVD from Command line

To print a list of all emulator options, including a short description, enter this command emulator -help

The Android SDK includes an Android device emulator — a virtual device that runs on your computer. The Android Emulator lets you develop and test Android apps without using a physical device.

Once you have the Android Emulator booted or an Android Device connected that is running an AVD with API Level, then run Appium on the command line (via the appium command)

Your test script should ensure that the platformVersion capability corresponds to the emulator or device version you are testing, and that the app capability is an absolute path to the .apk file of the Android app.

Appium Tutorials: 

Add new comment