Skip to main content

Appium and Android setup

Back to Appium Tutorial Index

Android Requirements
  • Android SDK API >= 17 (Additional features require 18/19)
  • Appium supports Android on OS X, Linux and Windows. Follow these directions for setting up your environment properly for testing on different OSes:
Android setup does not differ greatly on different operating system. Let’s look at detailed setup instructions for Ubuntu  -

Installing Appium app on MAC or Windows 
 

Installing Appium on Ubuntu  -

Install node.js (v0.10 or greater) -

Since appium server is written in node.js, first node.js needs to be installed. Following this appium can be installed from npm -

Setup with Ubuntu: curl -sL https://deb.nodesource.com/setup | sudo bash -
Then install with Ubuntu: sudo apt-get install -y nodejs


DON’T install node js using above method, it would result in following error when starting appium -
error: Appium will not work if used or installed with sudo. Please rerun/install as a non-root user. If you had to install Appium using `sudo npm install -g appium`, the solution is to reinstall Node using a method (Homebrew, for example) that doesn't require sudo to install global npm packages.

To fix it -

  • Download latest nodejs linux binaries (or windows, mac if you are not on linux) form http://nodejs.org/download/
  • Extract into a folder that doesn't need sudo rights to access, for example your home folder.
tar -xvf <downloaded_binary_tar.gz>
{extracted file looks as following in my home directory -
/home/tarun/node-v0.12.2-linux-x64}
  • Add the following line to your ~/.bashrc file.
export PATH=$PATH:<full_path_of_the_extracted_node_folder>/bin

appium is installed in bin folder and updating path would let you execute appium from command line as described later -
refresh bashrc so you don’t have to logout and login -
source ~/.bashrc
  • Open a new terminal and execute
npm install -g appium
    {appium installation looks as following on my ubuntu -
/home/tarun/node-v0.12.2-linux-x64/bin}
  • Now start appium as -
cd /home/tarun/node-v0.12.2-linux-x64/bin
./appium
If everything goes right then you would have appium server up and running as following  
info: Welcome to Appium v1.3.7 (REV 72fbfaa116d3d9f6a862600ee99cf02f6d0e2182)
info: Appium REST http interface listener started on 0.0.0.0:4723
info: Console LogLevel: debug

Install Android SDK


  • If you are using Android Studio then you can set up Android Virtual Device from AVD under Tools > Android > AVD Manager as following - 




  • Install android sdk from -

Unpack the .zip file you've downloaded. The SDK files are downloaded separately to a user-specified directory

By default, Android SDK does not include everything required to start development. The SDK separates tools, platforms, and other components into packages that can be downloaded as needed using the Android SDK Manager. Hence we must first download a few packages and add to Android SDK.
  • Downloading additional packages and latest SDK tools. launch the Android SDK Manager.
Open a terminal and navigate to the tools/ directory in the location where the Android SDK was installed, then execute android sdk.
For ex it looks as following on my ubuntu -
cd /Documents/mine/lib/android-sdk-linux/tools$
./android &

  • As a minimum when setting up the Android SDK, you should download the latest tools and Android platform:
    1. Open the Tools directory and select:
      • Android SDK Tools
      • Android SDK Platform-tools
      • Android SDK Build-tools (highest version)
AndroidTools.png
    1. Open the first Android X.X folder (the latest version) and select:
      • SDK Platform
      • A system image for the emulator, such as ARM EABI v7a System Image
  1. AndroidTools.png
3. Get the support library for additional APIs
The Android Support Library provides an extended set of APIs that are compatible with most versions of Android.
Open the Extras directory and select:
    • Android Support Repository
    • Android Support Library
  • AndroidTools.png
  • Once you've selected all the desired packages, continue to install:

  1. Click Install X packages.
  2. In the next window, double-click each package name on the left to accept the license agreement for each.
  3. Click Install.

The download progress is shown at the bottom of the SDK Manager window. Do not exit the SDK Manager or it will cancel the download.

Creating new emulator
Android emulator can be created from command line or using AVD (Android Virtual Device manager). AVD can be launched from Android SDK Manager as following -
Android SDK Manager > Tools > Manage AVDs  - launches Android Virtual Device (AVD) manager
You can create a new device or select any of the existing Device Definitions, for example select Nexus 5 by Google and click Create AVD button

AndroidTools.png
Select Target, CPU/ABI, Skin and click ok to set up new virtual device. When you click Ok then device would be listed under “Android Virtual Devices” tab -
AndroidTools.png

Select device > click Start button and Launch the device. This would launch Android emulator -
AndroidTools.png

NOTE: if Android emulator opens a blank screen then select Emulation Options > Use Host GPU and start emulator again. This would also increase the emulator performance drastically.

Setup ANDROID_HOME variable

Finally, set $ANDROID_HOME to be your Android SDK path. This looks as following on my ubuntu -
  • export ANDROID_HOME=/home/tarun/Documents/mine/lib/android-sdk-linux
  • export PATH=$PATH:/home/tarun/Documents/mine/lib/android-sdk-linux/tools

  • and then execute source ~/.bashrc
Install Android Debug Bridge (adb) -
Execute following command to install adb on Ubuntu -

sudo apt-get install android-tools-adb

and following for mac - 


  1. brew install android-platform-tools

Android Debug Bridge (adb) is a command line tool that lets you communicate with an emulator or android device. It is a client-server program and includes three components:
  • A client, which runs on development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients. For example if you want to see list of emulator/devices running then execute following command -
adb devices
  • A server, which runs as a background process on development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.
Refer ADB for more on this topic.

You can find the adb tool in <sdk>/platform-tools/ directory. It is available on following directory on my linux - /home/tarun/Documents/mine/lib/android-sdk-linux/platform-tools

Appium Inspector


If you installed appium on OSX or Windows using appium binary then you can use appium inspector to inspect to inspect app. Appium inspector has two functions - 
  • Inspect elements in app
  • Enable record and playback

Appium inspector does not work very well with android. Appium encourages to use uiautomatorviewer. An inspector tool from Google which is same as Appium inspector. You can find information on this here


Before using UIAutomatorViewer you should shut down appium as appium uses UIAutomatorViewer internally and this will prevent UIAutomatorViewer from working.
Once you have android SDK installed then you can start UIAutomatorViewer from command line - 

uiautomatorviewer

This would start UiAutomatorViewer as - 



Now open the app (for example calculator) in emulator or device and click on Device Screenshot (uiautomator dump) in the UIAutomatorViewer menu.
Following this you would see the snapshot of application in UIAutomatorViewer as - 



Now you can analyze the app element by hovering mouse on them. Mousehover would highlight the element in red circle in UIAutomatorViewer and would detail down the hierarchy in RHS. 

Popular posts from this blog

Selenium Tutorial: Pattern Mathing using Selenium

I must confess I have never been admirer of Regular Expression but then there are times you can not escape from it, especially while working on a website which has dynamic contents appeared in static text and you want to validate it. like - "Validate that this text appears and there is 123 here and 456 here" And the test condition is 123 and 456 could be any three digits but number if digits should not be more than three. In a crude way we can at least test this - Assert.assertTrue(selenium.getText("elementLocator").contains("Validate that this text appears and there is")); but what if text goes wrong after "and there is"... what if more than 3 digits appear in text. This is where pattern matching/regular expression comes for our rescue and we can use matches method of String class to achieve same. So the assertion would be - String text = selenium.getText("elementLocator"); Assert.assertTrue(text.matches("Validate

Using chrome console to test xPath and css selectors

Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application - Hit escape key and console would open another pane to write element locators - And now you can start writing xPath or css selectors in chrome console and test them - The syntax for writing css id - $$(“ ”) And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed - If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application - If you want to clean console of previously written element selectors then just hit ctrl+L keys and chrome console would be empty again. Pro

Return only first or last element from webelements collection

We often come across situation when there are multiple elements on a page and we probably like to exercise only a few of them using selenium webdriver. May be just first and last element. For example on a search result page we may like to click on only first and last link and not all. This is when Iterables API comes handy. (By the way I am assuming that you have already completed watching selenium training videos :)). Once we have collection of web element then we can use Iterables to get only first or last element as following - Consider that we fetch collection of element as - List< WebElement > webElements = getDriver().findElements(By. id ( "htmlID" ));   Now we can get the first web element from this collection as -  WebElement firstElement = Iterables. getFirst (webElements,  getDriver().findElement(By. id ( "defaultElement" )));   Herein second argument -   (getDriver().findElement(By. id ( "defaultElement" )))    in the me