Skip to content

Mobile Appium (Android and iOS) Automation Quick Start Guide

rohandudam edited this page Jul 30, 2024 · 19 revisions

Table of Contents

Sample Android Application

The sample application used for this guide is a Bitcoin Info app. This app gives you detailed information about Bitcoin, it's current price and answers some of the frequently asked questions about Bitcoin.

There is a main page which allows a user to link to subpages like Bitcoin Info, Real Time Price of Bitcoin and some more helpful subpages.

Usage Example

The Qxf2 POM framework uses appium to run the mobile tests. Appium is an open source test automation framework for use with native, hybrid and mobile web apps. It drives iOS, Android, and Windows apps using the WebDriver protocol. To get setup with appium and run your tests you need to do the following things

Get setup with appium

You can refer to the below links to get setup with appium on an Android and iOS device

You can also refer to the above blog to learn how to set up emulators on an Android and iOS device.

Write your first mobile appium test

Here is the sample test script for writing a test for the Bitcoin app we discussed earlier.

"""
Automated test will do the following:
    # Open Bitcoin Info application in emulator.
    # Click on the bitcoin real time price page button.
    # Compare expected bitcoin real time price page heading with current page heading.
    # Verify that the bitcoin real time price is displayed on the page.
    # Display the results.
"""

import os, sys, time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from page_objects.PageFactory import PageFactory
from utils.Option_Parser import Option_Parser
import conf.mobile_bitcoin_conf as conf
import conf.testrail_caseid_conf as testrail_file


def test_mobile_bitcoin_price(mobile_os_name, mobile_os_version, device_name, app_package, app_activity, remote_flag, device_flag, testrail_flag, test_run_id,app_name):
    "Run the test."
    try:
        # Initalize flags for tests summary.
        expected_pass = 0
        actual_pass = -1

        #1. Create a test object.
        test_obj = PageFactory.get_page_object("bitcoin main page")
        
        #2. Setup and register a driver
        start_time = int(time.time())
        test_obj.register_driver(mobile_os_name,mobile_os_version,device_name,app_package,app_activity,remote_flag,device_flag,app_name)

        #3. Get expected bitcoin price page header name
        expected_bitcoin_price_page_heading = conf.expected_bitcoin_price_page_heading
        
        #4. Click on real time price page button and verify the price page header name.
        result_flag = test_obj.click_on_real_time_price_button(expected_bitcoin_price_page_heading)
        test_obj.log_result(result_flag,
                    positive="Successfully visited the bitcoin real time price page.",
                    negative="Failed to visit the bitcoin real time price page.")
       
        test_obj.write('Script duration: %d seconds\n'%(int(time.time()-start_time)))
        
        #5. Verify bitcoin real time price is displayed.
        if result_flag is True:
            result_flag = test_obj.get_bitcoin_real_time_price()
        test_obj.log_result(result_flag,
                            positive="Successfully got the bitcoin real time price in usd.",
                            negative="Failed to get the bitcoin real time price in usd.")
       
        test_obj.write('Script duration: %d seconds\n'%(int(time.time()-start_time)))

        #7. Print out the results.
        test_obj.write_test_summary()

        #8. Teardown and Assertion.
        test_obj.wait(3)
        expected_pass = test_obj.result_counter
        actual_pass = test_obj.pass_counter
        test_obj.teardown()

    except Exception, e:
        print "Exception when trying to run test:%s" % __file__
        print "Python says:%s" % str(e)

    assert expected_pass == actual_pass

How to locate elements

We use XPath selector for locating the web elements and store the locator information in the locators_conf.py file. In the Page Object bitcoin_main_page.py, we are fetching the locators and setting the corresponding values in the click_on_price_button method.

bitcoin_main_page.py

import conf.locators_conf as locators
class Bitcoin_Main_Page(Mobile_Base_Page):
    "Page object bitcoin main page."

    #Locators of the mobile page elements.
    bitcoin_real_time_price_button = locators.bitcoin_real_time_price_button

For a detailed understanding of how to identify elements for a mobile app, refer to our blog on Ways to identify UI elements in mobile apps

How to run tests

a) Make sure you have the Appium and Android or iOS emulator running. pytest tests/test_mobile_bitcoin_price.py (can be used to run the test with default options)

b) Make sure you have the Appium and Android or iOS emulator running. pytest tests/test_mobile_bitcoin_price.py -H 8.0 -I "Google Nexus" (to run test in Google Nexus, Android version 8 emulator)

c) Use pytest --h to check all the pytest options which can be used to run the test. Listed below are some options requied to run mobile tests.

  REMOTE_FLAG, --remote_flag=REMOTE_FLAG
                        Run the test in Browserstack/Sauce Lab: Y or N
  MOBILE_OS_NAME, --mobile_os_name=MOBILE_OS_NAME
                        Enter operating system of mobile. Ex: Android, iOS
  MOBILE_OS_VERSION, --mobile_os_version=MOBILE_OS_VERSION
                        Enter version of operating system of mobile: 8.1.0
  DEVICE_NAME, --device_name=DEVICE_NAME
                        Enter device name. Ex: Emulator, physical device name
  APP_PACKAGE, --app_package=APP_PACKAGE
                        Enter name of app package. Ex: bitcoininfo
  APP_ACTIVITY, --app_activity=APP_ACTIVITY
                        Enter name of app activity. Ex: .MainActivity
  DEVICE_FLAG, --device_flag=DEVICE_FLAG
                        Enter Y or N. 'Y' if you want to run the test on device. 'N' if you want to run the test on emulator.
  APP_NAME, --app_name=APP_NAME
                        Enter application name to be uploaded.Ex:Bitcoin