在
上一篇文章中,進了Android的坑,這次,要跳進更大更深的坑——iOS。
百度google了一輪,最大的感觸是:好多教程都不適用啊!要么是Appium版本舊,要么是iOS版本舊。想找一篇詳細的“從入門到放棄”的教程都沒有,之前搭Android環境的時候,能搜到很多十分詳實的教程,而iOS的就有點蛋疼了。
然而,坑還是要入的,所以,就從搭環境開始吧。
環境搭建
1. Xcode
必須承認,我是Mac OS新手,有Linux的基礎,用了幾天Mac OS,算是基本會用了……
本文的系統版本為Mac OS 10.12.5,由于陪測的iOS用的是10.3.2,所以Xcode必須要裝上新的8.3.2(不然沒有SDK),Xcode在App Store里安裝就好了。
2. Appium
Appium向來有命令行版的和GUI版的——我選擇后者,到
官網下載安裝最新的Appium Desktop 1.0.2的dmg,里面帶了1.6.4的Appium。
3. Appium客戶端庫
Python、Ruby、Java、Javascript、PHP、C#等,任君選擇,去
官網下載。
例如我用Python,就安裝Appium-Python-Client,在終端運行
1
2
sudo easy_install pip # 系統自帶easy_install
pip install Appium-Python-Client --user # 加上--user是因為Mac下有權限的問題
4. Homebrew
Homebrew相當于Linux下的apt-get、yum,要用它來安裝node,在終端運行
1
2
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew -v # 顯示版本,如Homebrew 1.2.1
5. node
1
2
brew install node
node -v # e.g. v7.10.0
6. appium-doctor相關
用來檢測Appium相關環境有沒有裝好的工具:
1
2
3
4
5
6
7
8
npm install -g appium-doctor
# 裝好之后 檢測一下iOS的環境有沒有配置好 如果不加--ios 則檢測Android和iOS
appium-doctor --ios
# 它提示我缺少Xcode Command Line Tools和Carthage,那就補上
xcode-select --install
brew install carthage
7. 還有一些庫
1
2
brew install libimobiledevice --HEAD
npm install -g ios-deploy # for iOS 10+
8. WebDriverAgent相關(大坑)
iOS 10+使用的是XCUITest,Appium使用的模塊是appium-xcuitest-driver,其中引用了Facebook提供的WDA方案來驅動iOS的測試。
裝Appium Desktop的時候,它里面帶了一個WebDriverAgent,但是這個自帶的是有問題的!會造成不能使用Inspector,卡了很久!從Facebook那里自己clone一份才是王道:
1
2
3
4
5
6
7
8
9
10
cd ~
git clone https://github.com/facebook/WebDriverAgent.git
cd WebDriverAgent
mkdir -p Resources/WebDriverAgent.bundle
./Scripts/bootstrap.sh # 開始下載并編譯 編譯不應該報錯
cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/
rm -rf WebDriverAgent # 把自帶的刪掉
ln -s ~/WebDriverAgent WebDriverAgent # 用facebook的原版替換回去
經過了baidu和google,用以上方法解決了不能Inspect的問題。
在使用Appium時,需要把WDA裝到真機上,然后又會遇到證書的問題,我也不是很明白,總之跟provisioning profile有關。
用Xcode打開目錄下的WebDriverAgent.xcodeproj,對于WebDriverAgentLib 和 WebDriverAgentRunner,勾選“Automatically manage signing”,把Team改成公司的,Bundle Identifier改成公司的證書可以接受的名字,具體可以參考
官方文檔操作,不懂的找開發同學協助。
然后就可以把WebDriverAgentLib和WebDriverAgentRunner都編譯到真機運行一下了。正常來說,會在桌面生成一個沒圖標的WebDriverAgentRunner,點開之后不會有什么反應,這就對了。
終于把環境搭好了,感動啊。
寫測試腳本
1. Appium server capabilities
要讓App跑起來,還需要了解Appium server capabilities,它告訴Appium服務器很多信息,例如開哪個App、手機的系統/版本、在哪臺設備上跑(真機還是模擬器等)等。
給出我用到的一些參數(in Python),其他capabilities請參考
官方文檔。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: utf-8 -*-
from time import sleep
from appium import webdriver
desired_caps = {}
desired_caps['automationName'] = 'XCUITest' # Xcode8.2以上無UIAutomation,需使用XCUITest
desired_caps['platformName'] = 'iOS'
desired_caps['platformVersion'] = '10.3.2'
desired_caps['deviceName'] = 'iPhone 7 Plus'
desired_caps['bundleId'] = '需要啟動的bundle id, 去問開發者'
desired_caps['udid'] = '真機的udid 可在Xcode或iTunes里查看'
desired_caps['newCommandTimeout'] =3600 # 1 hour
# 打開Appium服務器,start server后,嘗試啟動被測App
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(60)
driver.quit()
如果能跑起來,就是正常的,不然看一下報什么錯。
2. Inspector
能跑起來只是第一步,更重要的是如何定位元素。
Inspector的使用方法很簡單,之前運行driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub’, desired_caps)之后,連接就已經建立好了,只需在瀏覽器進入
http://localhost:8100/inspector即可,之后就可以使用熟悉的driver.find_element_by_xxx方法來定位元素啦。
后記
Selenium的坑
后來又遇到了一點坑,例如使用send_keys方法時,報
Message: Parameters were incorrect. We wanted {“required”:[“value”]} and you sent [“text”,”sessionId”,”id”,”value”]
錯誤,google了一下發現是selenium新版導致的問題,降級后解決:
1
2
pip uninstall selenium
pip install selenium==3.3.1
手勢操作
由于XCUI的原因,之前的一些手勢操作如swipe、pinch、TouchAction等都不能用了,可以參考
這篇官方文檔,使用driver.execute_script方法代替。如
1
2
driver.execute_script('mobile: scroll', {'direction': 'down'}) # 向下滾動
driver.execute_script('mobile: dragFromToForDuration', {'duration': 0, 'fromX': 374, 'fromY': 115, 'toX': 200, 'toY': 100}) # 從右往左拖
對于直接用坐標的,還要注意邏輯分辨率的問題,如iPhone 7 Plus的邏輯分辨率是414×736。
最后
剛接觸iOS的Appium,之后肯定還會遇到問題,會繼續更新本文。
更新,最近更新到了Appium Desktop 1.1,里面帶了1.6.5的Appium,使用起來暫時未發現明顯區別。
附上一些參考:
1. http://www.cocoachina.com/ios/20170112/18518.html
2. http://blog.sina.com.cn/s/blog_b5a76ebd0102wuce.html
3. https://github.com/appium/appium/blob/master/docs/en/appium-setup/real-devices-ios.md
4. http://blog.csdn.net/achang21/article/details/70877583
5. https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
6. https://github.com/appium/python-client
7. https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/appium-bindings.md
8. https://github.com/facebook/WebDriverAgent/issues/537
9. https://github.com/facebook/WebDriverAgent/wiki/Using-the-Inspector