Mobile Testing支援執行遵循Appium測試架構編寫的自動化測試代碼。本文介紹如何使用Appium對iOS 10裝置進行功能測試的方法。
Mobile Testing平台目前可支援pytest、unittest和robotframework等三方庫。如需其他支援,請聯絡支援人員。
前提條件
瞭解使用Appium進行自動化測試的方法。具體說明請參見:Appium官方文檔。
具備Python編程基礎。
配置環境
本文以iOS 10為例進行說明,相應環境要求如下:
macOS 10.11.5及以上
Xcode 8.0及以上
Appium 1.6.0及以上
建立Capabilities檔案
使用Python語言編寫Capabilities檔案,指定執行測試指令碼所需測試環境,檔案命名為desired_capabilities.py。
其中包括get_uri()和get_desired_capabilities()2個函數。
函數 | 說明 |
| 擷取本次會話的參數。 參數可根據實際需求進行設定。具體說明請參見:Appium Desired Capabilities。 |
| 返回Appium Server的地址。 |
範例程式碼
#!/usr/bin/env python
import sys
def get_desired_capabilities():
desired_caps = {
'platformName': 'iOS',
'platformVersion': '10.0',
'deviceName': 'iPhone 6s',
'udid': '36317c0f81086d7f4f99a9771179720b7962****',
'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()樣本說明
1:在get_desired_capabilities()函數中設定了10個參數:
參數 | 說明 |
platformName | 測試裝置的系統類別型。 |
platformVersion | 測試裝置的系統版本。 |
deviceName | 測試裝置的名稱。 |
udid | 測試裝置的ID。 |
realDeviceLogger | deviceconsole工具儲存路徑,用於擷取測試裝置的日誌。 |
app | 應用安裝程式的儲存路徑。 如不填寫,則預設為bundleId運行手機上已安裝應用的路徑。 |
bundleId | 應用的bundleId。 |
newCommandTimeout | 兩條指令之間允許的最長時間間隔。 如兩條指令之間的時間間隔超過預設取值,則Appium Server將終止本次會話。 |
automationName | 本次會話所使用的自動化引擎。
|
noReset | 本次會話之前是否重設應用。 |
2:本地啟動的Appium Server,監聽連接埠預設設定為50000,則傳回值為http://localhost:50000/wd/hub。
desired_capabilities.py僅用於本地執行/驗證測試指令碼,待測試指令碼打包上傳後,Mobile Testing平台自動產生並替換該檔案。
建立入口指令檔
使用Python語言編寫測試指令碼,作為執行自動化測試的入口,檔案命名為main.py。
範例程式碼及說明如下:
# -*- coding: utf-8 -*-
from appium import webdriver
# 引入剛剛建立的同目錄下的desired_capabilities.py
import desired_capabilities
# 我們使用python的unittest作為單元測試工具
from unittest import TestCase
# 我們使用python的unittest作為單元測試工具
import unittest
# 使用time.sleep(xx)函數進行等待
import time
class MqcTest(TestCase):
def setUp(self):
# 擷取我們設定的capabilities,通知Appium Server建立相應的會話。
desired_caps = desired_capabilities.get_desired_capabilities()
# 擷取server的地址
uri = desired_capabilities.get_uri()
# 建立會話,得到driver對象,driver對象封裝了所有的裝置操作。
self.driver = webdriver.Remote(uri, desired_caps)
# 等待app完全載入
time.sleep(3)
# 第1個用例,如果檢測到彈框,就點掉
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='不允許']")
notAllowBtn.click()
else:
break
# 第2個用例,登入
def test_case_b_login(self):
# 取得導覽列的左側按鈕
leftBtn = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@label='navigationbar sidebar']")
leftBtn.click()
time.sleep(3)
# 點擊“點擊頭像登入”按鈕
potraitEle = self.driver.find_element_by_xpath("//XCUIElementTypeStaticText[@label='點擊頭像登入']")
potraitEle.click()
time.sleep(3)
# 開始登入,輸入帳號密碼
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)
# 點擊“登入”按鈕
loginBtn = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@label='登入']")
loginBtn.click()
# 等待登入成功
time.sleep(5)
def tearDown(self):
# 測試結束,退出會話
self.driver.quit()
if __name__ == '__main__':
try: unittest.main()
except SystemExit: passMobile Testing平台通過執行main.py檔案的測試指令碼實施功能測試。
啟動Appium Server
在本地命令列,執行appium命令,啟動Appium Server。例如:appium -p 50000
參數釋義
參數 | 說明 |
-p | 指定Appium Server監聽的連接埠號碼。 |
-a | 指定Appium Server的IP地址。 |
-selendroid-port | 在Selendroid模式下,指定adb forward的連接埠。 |
-U | 當Appium Server掛載多個裝置時,指定用於測試的裝置的串號。 |
--full-reset | 設定測試案例執行完成後,對裝置進行完全清理。 |
執行測試指令碼
本地添加已開啟開發人員模式的手機。
在本地命令列,執行
python main.py -v命令,執行並驗證測試指令碼功能正常。
提交至Mobile Testing
1:將測試檔案打包為zip檔案。
main.py檔案必須放在根目錄下。
2:在Mobile Testing控制台,建立上傳指令碼,用於後續的Android功能測試。具體操作請參見:建立指令碼。