Mobile automation with Appium

If you’ve ever wanted to start exploring in a new field, you’ve certainly come across a point where you have to make a choice regarding what to use. In order to achieve better results, you spend hours looking for the right tool. Well, when it comes to mobile automation, no more. Here comes Appium.

I. Appium

Appium is an open source tool developed based on the principles and the protocols that drive Selenium (a web automation tool). It is used for writing and running test cases for mobile native apps. By native apps, we refer to those running on Android, iOS, and Windows phones.

There is a large supply of tools which offer the same functionality, some are even built-in the most used IDEs, for example, Espresso for Android. So what does Appium offer more such that to stay ahead of other tools? The answer is simple: cross-platform. While other tools are platform dependent, Appium allows the same service to be used to run tests on multiple platforms. The design of a web-service has led to the Appium clients to be language-independent. This can be seen in the collection of Appium’s clients.

The philosophy behind Appium is constructed around several factors:

  1. Automate the mobile application after it was built. With no access to the source code (view the app as a regular user would).
  2. Platform and programming language independent.
  3. Use existing APIs in order to communicate with the mobile device.

II. Install Appium

In order to install Appium, you are offered with two possibilities:

  1. With the GUI installer provided from their official web page.
  2. From the command line using node (it assumes you have NodeJS already installed):
    1. npm install -g appium: install the Appium service globally
    2. npm install wd: install the Appium client
    3. appium: start the Appium service in order to allow connections to the service

III. Connection with the device

Once the web service is up and running, you can start writing the first test case. In order to do so, from the test, you have to connect with the service.This is done with the help of the client libraries.

For example, in Python, we perform the following steps to write a simple test case which opens the application:

  1. Install the Appium client package using the pip command:
    • pip install Appium-Python-Client
  2. Create a dictionary with the configurations for the web service (this include the target platform, the target mobile device, the compiled application file and other customizations for the connection):
    • the most important keys are defined here
      desired_caps = {}
      desired_caps['platformName'] = 'Android'
      desired_caps['platformVersion'] = '4.2'
      desired_caps['deviceName'] = 'Android Emulator'
      desired_caps['app'] = PATH('../../../apps/selendroid-test-app.apk')
  3.    Create a new session with the help of the driver:
    • With each instance of the Driver class, a new call to open the application on the mobile device is performed. It is advised to use this only once per test run.
    • Either place the driver instanciation in the @before method and be sure to close the driver’s connection with the @after method OR create a wrapper which makes sure the instantiation is unique (singleton pattern).
      driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

IV. Writing test cases

After you have secured an instance of the Driver class, you can now access and perform all the actions provided in the client’s implementation. We will go through the most important client API calls here:

  • Query for elements:
    • All the search for elements is conducted through the previously created Driver instance. It offers the initial entry point for retrieving elements.
      elem = driver.find_element_by_accessibility_id('MyElementID')

      The type of elem is WebElement, and we can perform various operations over it, but we will see this in a while. For now, note that each element can be again queried for elements that are under him, hierarchy speaking. So, for example, if elem is referring to a table, we can query it in order to retrieve all the rows of this specific table.

  • Validate elements:
    • The three most important verification actions that can be performed on any UI element are:
      • is_enabled: checks if the current element is enabled (for clicking let’s say) or not
        self.driver.find_element_by_id('ID').is_enabled()
      • is_displayed: checks if the current element is visible on the screen (as a user will see it or not)
        self.driver.find_element_by_id('ID').is_displayed()
      • is_selected: checks if the current element was previously selected or not (a radio button)
        self.driver.find_element_by_id('ID').is_selected()
    • These methods have a boolean return type and are mostly used as a precondition or a postcondition checks when checking the functionality of a more complex action.
  • Actions over elements:
    • each element is practically of type Web Element. This means that all instanced elements have access to the same methods and it falls to the developer to make sure that the right calls are performed. These actions are of two types:
      • Human-like actions: these include click(), long_tap(), send_keys() and other kinds of interactions which trigger changes on the device.
        self.driver.find_element_by_id('ID').send_keys('search it')
        self.driver.find_element_by_id('ID').click()
      • Informational: these show data related to the position on the screen, the size, the text value assigned to it.
        self.driver.find_element_by_id('ID').text
        self.driver.find_element_by_id('ID').size

V. Running the tests

Running the tests do not require a special action, you simply run the script with the test. When the Driver instantiation is executed, the connection to the service is automatically created and the application starts on the device. From now on, every method call on the driver will produce a call to the service and further send to the device.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.