All Products
Document Center

Write an iOS test script

Last Updated: Mar 04, 2022

Mobile Testing supports automated testing code that is written based on Appium. This topic describes how to use Appium to make functional test on iOS 10 devices.


Mobile Testing supports third-party libraries, including pytest, unittest, and Robot Framework. If you want to use more frameworks, contact technical support.


  • You are familiar with how to use Appium for automated testing. For more information, visit Appium documentation.

  • You have basic Python programming skills.

Configure environments

iOS 10 is used in this topic to show the required environments:

  • macOS 10.11.5 and later

  • Xcode 8.0 and later

  • Appium 1.6 and later

Create a Capabilities file

Write a Capabilities file in the Python language to specify the test environment required to execute the test script. The file is named

The file contains the get_uri() and get_desired_capabilities() functions.




Queries the parameters of the current session.

You can specify the parameters based on your actual requirements. For more information, visit Appium Desired Capabilities.


Returns the URL of the Appium server.

Sample code

#! /usr/bin/env python

import sys

def get_desired_capabilities():
    desired_caps = {
        'platformName': 'iOS',
        'platformVersion': '10.0',
        'deviceName': 'iPhone 6s',
        'udid': '36317c0f81086d7f4f99a9771179720b7962a2ad',
        'app': '/Users/adam/',
        'newCommandTimeout': 60,    
        'automationName': 'Appium',
        'noReset': True

    return desired_caps

def get_uri():
    return "http://localhost:56000/wd/hub"

def flushio():


1. 10 parameters are set in the get_desired_capabilities() function.




The system type of the test device.


The system version of the test device.


The name of the test device.


The ID of the test device.


The storage path of the deviceconsole tool to query the logs of the test device.


The storage path of the app installer.

By default, if this parameter is not specified, bundle ID runs the path of an installed app on the phone.


The bundle ID of the app.


The timeout period to wait between two commands.

If the time interval between two commands exceeds the specified value, the Appium server ends the current session.


The automation engine used in the current session.

  • If the system version of the device is earlier than Android 4.2, set the value to Selendroid.

  • If the system version of the device is IOS 4.2 or Android 4.2 and later, set the value to Appium.


Indicates whether to reset the app before this session.

2. By default, the listening port is set to 50000 for Appium Servers that are started. The return value is http://localhost:50000/wd/hub.

Note is used only to execute and verify the test script. After the test script is packaged and uploaded, the Mobile Testing platform generates a file to replace the file.

Create an entry script file

Write a test script in the Python language as an entry to perform automated testing. The script file is named

Sample code and description:

# -*- coding: utf-8 -*-

from appium import webdriver

# Import the file to the same created directory.
import desired_capabilities

# Use unittest of Python as the unit test tool.
from unittest import TestCase

# Use unittest of Python as the unit test tool.
import unittest

# Use the time.sleep(xx) function to wait.
import time

class MqcTest(TestCase):

    def setUp(self):
        # Query the specified capabilities and notify the Appium server to create the corresponding session.
        desired_caps = desired_capabilities.get_desired_capabilities()
        # Query the address of server.
        uri = desired_capabilities.get_uri()
        # Create a session and query the driver object that encapsulates all device operations.
        self.driver = webdriver.Remote(uri, desired_caps)
        # Wait for app to fully load.

    # If the first use case is detected and the dialog box appears, close the dialog box.
    def test_case_a_dismiss_alert(self):
        while True:
            alertEle = self.driver.find_elements_by_class_name("XCUIElementTypeAlert")
            if alertEle:
                print 'find an alert'
                notAllowBtn = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@label='Not allowed']")

    # Use the second use case to log on to the app.
    def test_case_b_login(self):
        # Query the left-side button in the navigation pane.
        leftBtn = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@label='navigationbar sidebar']")

        # Click the profile picture to log on.
        potraitEle = self.driver.find_element_by_xpath("//XCUIElementTypeStaticText[@label='Click the profile picture to log on']")

        # Log on and enter the account password.
        tfEle = self.driver.find_element_by_class_name("XCUIElementTypeTextField")
        stfEle = self.driver.find_element_by_class_name("XCUIElementTypeSecureTextField")

        # Click "Log On".
        loginBtn = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@label='Log On']")

        # Wait for logon.

    def tearDown(self):
        # The test ends. Exit the session.

if __name__ == '__main__':
    try: unittest.main()
    except SystemExit: pass

The Mobile Testing platform implements functional testing by executing the test script in the file.

Start the Appium server

Run the appium command on the command line to start the Appium server. Example: appium -p 50000.





The listener port of the Appium server.


The IP address of the Appium server.


The port of Android Debug Bridge (adb) in Selendroid mode.


The serial number of the test device specified when the Appium server is attached to multiple devices.


Cleans up the device after the test case is executed.

Execute the test script

Run the python -v command on the command line to execute the test script and verify whether the features to be tested work normally.

Submit a test to Mobile Testing

Step 1: Package the test file as a ZIP file.


The file must be stored in the root directory.

Step 2: In the Mobile Testing console, create an uploaded script for subsequent Android functional testing. For more information, see Create a script.