Test a Native Mobile Application using Appium with Python

The tagline of the very popular open source test automation tool for mobile applications, Appium is Mobile App automation made awesome. And indeed there is no doubt about it. Appium architecture is based on similar lines as that of selenium. And this provides us with a leverage to use any programming language for coding our tests for Appium. Quoting as it is from the documentation of the Appium, the rules on which Appium is based is as follows –

  1. You shouldn't have to recompile your app or modify it in any way in order to automate it.
  2. You shouldn't be locked into a specific language or framework to write and run your tests.
  3. A mobile automation framework shouldn't reinvent the wheel when it comes to automation APIs.
  4. A mobile automation framework should be open source, in spirit and practice as well as in name!

In this article, we will see how we can use Appium to automate testing of a native app calculator on the android phone, using the language python.

Native App and Object information from UI Automator

An application which is written using the operating system software development kit is called a Native application. These are designed for specific operating system, so an android native app cannot run on ios and vice versa. The native app in here which we will explore is calculator built for the android operating system.

Lets us use the UI Automator, to fetch the locator information for some digits we will use. The scenario being – fetch two digits and perform the operation of addition on them, verify the result. 

 

Object

Locator

Digit5

Resource id - com.android.calculator2:id/digit_5

Digit6

Resource id- com.android.calculator2:id/digit_6

plusop

Resource id- com.android.calculator2:id/op_add

eqop

Resource id - com.android.calculator2:id/eq

anwtx

Resource id - com.android.calculator2:id/result

 

Screenshot- 

 

 

Setting the system-

To write the script in python, to test the above application, I will be using the IDE eclipse, python 3.6 version. So to set the system we perform the following steps

a.       Have Java available on system

b.      Install eclipse

c.       Install python

d.      Go to help -> market place and install PyDev in eclipse

e.      Install using pip, Appium-python-client

 

Desired capabilities

Desired capabilities are information sent to the Appium server to let know about the type of environment required to run the test. This information is sent using the key value pair. In our script the desired capabilities are set as follows – 

desired_caps = {}

desired_caps['platformName'] = 'Android'

desired_caps['platformVersion'] = '6.0'

desired_caps['deviceName'] = 'Android Emulator'

desired_caps['avd']="NexusEmulator"

desired_caps['avdLaunchTimeout']=150000

desired_caps['appPackage']="com.android.calculator2"

desired_caps['appActivity']="com.android.calculator2.Calculator"

We set the platform os, the version, device in here we are using is Andoird Emulator, created using AVD manager. We set the timeout, and the name of the package we will be using to automate.

 

Script in Python

Once we have provided the information of the desired capabilities, we start putting the steps to perform actions on the different objects in the application. The entire script looks as follows – 

import unittest

from appium import webdriver

 

desired_caps = {}

desired_caps['platformName'] = 'Android'

desired_caps['platformVersion'] = '6.0'

desired_caps['deviceName'] = 'Android Emulator'

desired_caps['avd']="NexusEmulator"

desired_caps['avdLaunchTimeout']=150000

desired_caps['appPackage']="com.android.calculator2"

desired_caps['appActivity']="com.android.calculator2.Calculator"

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.find_element_by_id("com.android.calculator2:id/digit_5").click()

driver.find_element_by_id("com.android.calculator2:id/op_add").click()

driver.find_element_by_id("com.android.calculator2:id/digit_6").click()

driver.find_element_by_id("com.android.calculator2:id/eq").click()

assert(driver.find_element_by_id("com.android.calculator2:id/result").text.contains("11"))

 

We launch the Appium server, and execute the test to get the desired output.

Hope you found this article helpful to run your very first test of automating a native app using python language for Appium. Please do let us know your thoughts!