All Products
Search
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.

Notice

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

Prerequisites

  • 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 desired_capabilities.py.

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

Function

Description

get_desired_capabilities()

Queries the parameters of the current session.

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

get_uri()

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',
        'realDeviceLogger':'/usr/local/lib/node_modules/deviceconsole/deviceconsole',
        'app': '/Users/adam/iosapp.app',
        'bundleId':'net.oschina.iosapp',
        'newCommandTimeout': 60,    
        'automationName': 'Appium',
        'noReset': True
    }

    return desired_caps

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

def flushio():
    sys.stdout.flush()

Examples

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

Parameter

Description

platformName

The system type of the test device.

platformVersion

The system version of the test device.

deviceName

The name of the test device.

udid

The ID of the test device.

realDeviceLogger

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

app

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.

bundleId

The bundle ID of the app.

newCommandTimeout

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.

automationName

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.

noReset

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

desired_capabilities.py 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 desired_capabilities.py 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 main.py.

Sample code and description:

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

from appium import webdriver

# Import the desired_capabilities.py 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.
        time.sleep(3)

    # 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:
            time.sleep(3)
            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']")
                notAllowBtn.click()
            else:
                break

    # 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']")
        leftBtn.click()
        time.sleep(3)

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

        # 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")
        tfEle.send_keys("mqctest".decode('UTF-8'))
        time.sleep(1)
        stfEle.send_keys("123456".decode('UTF-8'))
        time.sleep(2)

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

        # Wait for logon.
        time.sleep(5)

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

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

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

Start the Appium server

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

Parameters

Parameter

Description

-p

The listener port of the Appium server.

-a

The IP address of the Appium server.

-selendroid-port

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

-U

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

--full-reset

Cleans up the device after the test case is executed.

Execute the test script

Run the python main.py -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.

Note

The main.py 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.