Python - Hello world with Appium on Android

By xngo on July 6, 2019

Appium is an automation tool to test native, hybrid and mobile web applications. It uses the webdriver to control the applications. It is designed as a client and server application. The client will send the commands to the server and then the server executes those commands on the mobile devices.

In this tutorial, I will show you how to run your first test with Appium on Android using Python.

Setup environment variables

Make sure that your Android environment variables are set correctly. ANDROID_HOME is the path to the Android's SDK which also contains both tools and platform-tools folders. Here is an example for my case.

export ANDROID_HOME=/root/tmp/android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

After your environment variables are set, you should able to run adb command. Otherwise, your environment variables are not set properly.

Install server

  1. Download the Appium-Desktop from https://github.com/appium/appium-desktop/releases/tag/v1.13.0
  2. Run it.

Install client

pip install Appium-Python-Client

Get Android device and application informations

In order for Appium to control of your mobile device, it needs the device name, application package name and the application main activity name.

To get the device name, plug your device to your computer and then run the following.

adb devices

Output

List of devices attached
VOPVZL9LLRDEFEOV    device

The application package name and the application main activity name can be found in your AndroidManifest.xml. Here is an example for my case. AndroidManifest: Package & Activity

Appium in action

For my case, here is a code example where Appium will launch my application.

from appium import webdriver
 
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'VOPVZL9LLRDEFEOV'
desired_caps['appPackage'] = 'net.openwritings.xmtl'
desired_caps['appActivity'] = '.MainActivity'
desired_caps['app'] = './app-debug.apk'
 
# This will launch your Android application.
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

Reference

  • List of client available http://appium.io/docs/en/about-appium/appium-clients/index.html
  • https://github.com/appium/python-client
  • https://github.com/appium/appium/blob/master/docs/en/drivers/android-uiautomator2.md

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.