概述
Appium是一個(gè)移動(dòng)端的自動(dòng)化框架姐赡,可用于測(cè)試原生應(yīng)用扰肌,移動(dòng)網(wǎng)頁(yè)應(yīng)用和混合型應(yīng)用厚柳,且是跨平臺(tái)的氧枣。可用于IOS和Android以及firefox的操作系統(tǒng)别垮。原生的應(yīng)用是指用android或ios的sdk編寫的應(yīng)用便监,移動(dòng)網(wǎng)頁(yè)應(yīng)用是指網(wǎng)頁(yè)應(yīng)用,類似于ios中safari應(yīng)用或者Chrome應(yīng)用或者類瀏覽器的應(yīng)用碳想∩斩混合應(yīng)用是指一種包裹webview的應(yīng)用,原生應(yīng)用于網(wǎng)頁(yè)內(nèi)容交互性的應(yīng)用。重要的是Appium是跨平臺(tái)的胧奔,何為跨平臺(tái)逊移,意思就是可以針對(duì)不同的平臺(tái)用一套api來(lái)編寫測(cè)試用例。
iOS10后基于UIAutomation的測(cè)試框架已經(jīng)蘋果更改為XCUITest測(cè)試框架了龙填,從Appium1.6.3開(kāi)始Appium也開(kāi)始支持XCUITest了胳泉。由于時(shí)間原因,Appium1.6.3以前的測(cè)試環(huán)境搭建及其工具使用就不再說(shuō)明岩遗,資料網(wǎng)上也是一大把扇商。截止目前最新的是Appium1.6.5,下面介紹下環(huán)境搭建過(guò)程。
開(kāi)發(fā)環(huán)境
系統(tǒng):macOS Sierra (10.12.4)
開(kāi)發(fā)工具:Xcode 8.3.2(iOS10.3.1)
環(huán)境搭建
開(kāi)始搭建
1宿礁、 安裝homebrew:homebrew 簡(jiǎn)稱brew案铺,是Mac OSX上的軟件包管理工具,能在Mac中方便的安裝軟件或者卸載軟件梆靖,可以說(shuō)Homebrew就是mac下的apt-get控汉、yum等神器。
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2返吻、 安裝libimobiledevice:libimobiledevice 是一個(gè)跨平臺(tái)的軟件庫(kù)暇番,支持 iPhone?, iPod Touch?, iPad? and Apple TV? 等設(shè)備的通訊協(xié)議。不依賴任何已有的私有庫(kù)思喊,不需要越獄壁酬。應(yīng)用軟件可以通過(guò)這個(gè)開(kāi)發(fā)包輕松訪問(wèn)設(shè)備的文件系統(tǒng)、獲取設(shè)備信息恨课,備份和恢復(fù)設(shè)備舆乔,管理 SpringBoard 圖標(biāo),管理已安裝應(yīng)用剂公,獲取通訊錄希俩、日程、備注和書簽等信息纲辽,使用 libgpod 同步音樂(lè)和視頻颜武。
$ brew install libimobiledevice --HEAD
3璃搜、 安裝carthage:carthage 使用于 Swift 語(yǔ)言編寫,只支持動(dòng)態(tài)框架鳞上,只支持 iOS8+的Cocoa依賴管理工具这吻。
$ brew install carthage
4、安裝node:node是安裝npm的前置條件篙议。
$ brew install node
5唾糯、安裝npm:npm是一個(gè)NodeJS包管理和分發(fā)工具,已經(jīng)成為了非官方的發(fā)布Node模塊(包)的標(biāo)準(zhǔn)鬼贱。
$ brew install npm
6移怯、安裝cnpm:國(guó)內(nèi)直接用npm下載安裝會(huì)有好多網(wǎng)絡(luò)問(wèn)題,安裝淘寶的cnpm要比npm好用这难,https://npm.taobao.org/
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
7舟误、安裝ios-deploy:ios-deploy是一個(gè)使用命令行安裝ios app到連接的設(shè)備的工具,原理是根據(jù)os x命令行工程調(diào)用系統(tǒng)底層函數(shù)姻乓,獲取連接的設(shè)備脐帝、查詢/安裝/卸載app。
$ cnpm install -g ios-deploy
8糖权、安裝xcpretty: xcpretty是用于對(duì)xcodebuild的輸出進(jìn)行格式化堵腹。并包含輸出report功能。
$ gem install xcpretty
9星澳、安裝appium疚顷,appium-doctor
$ cnpm install -g appium
$ cnpm install -g appium-doctor
10、使用appium-doctor檢查appium環(huán)境
$ appium-doctor //appium-doctor --ios 只檢測(cè)iOS環(huán)境
info AppiumDoctor Appium Doctor v.1.4.2
info AppiumDoctor ### Diagnostic starting ###
info AppiumDoctor ?? The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor ?? Node version is 6.10.2
info AppiumDoctor ?? Xcode is installed at: /Applications/Xcode.app/Contents/Developer
info AppiumDoctor ?? Xcode Command Line Tools are installed.
info AppiumDoctor ?? DevToolsSecurity is enabled.
info AppiumDoctor ?? The Authorization DB is set up properly.
info AppiumDoctor ?? Carthage was found at: /usr/local/bin/carthage
info AppiumDoctor ?? HOME is set to: /Users/zhoutq
WARN AppiumDoctor ?? ANDROID_HOME is NOT set!
WARN AppiumDoctor ?? JAVA_HOME is NOT set!
WARN AppiumDoctor ?? adb could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ?? android could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ?? emulator could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ?? Bin directory for $JAVA_HOME is not set
info AppiumDoctor ### Diagnostic completed, 6 fixes needed. ###
info AppiumDoctor
info AppiumDoctor ### Manual Fixes Needed ###
info AppiumDoctor The configuration cannot be automatically fixed, please do the following first:
WARN AppiumDoctor - Manually configure ANDROID_HOME.
WARN AppiumDoctor - Manually configure JAVA_HOME.
WARN AppiumDoctor - Manually configure ANDROID_HOME and run appium-doctor again.
WARN AppiumDoctor - Add '$JAVA_HOME/bin' to your PATH environment
info AppiumDoctor ###
info AppiumDoctor
info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied!
info AppiumDoctor
環(huán)境常見(jiàn)問(wèn)題解決:
? Xcode Command Line Tools are NOT installed!
$ xcode-select --install #按照提示安裝即可
WARN AppiumDoctor ? ANDROID_HOME is NOT set!
建議安裝AS禁偎,可以解決其他Android環(huán)境的其他問(wèn)題Android Studio
在.bash_profile/.zshrc文件中設(shè)置JAVA和Android環(huán)境變量
#android sdk
export ANDROID_HOME=~/Library/Android/sdk
export PATH="$HOME/.yarn/bin:$PATH"
#java_home
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
export CLASS_PATH=$JAVA_HOME/lib
以上都很簡(jiǎn)單腿堤,下面重點(diǎn)來(lái)了。
11如暖、appium服務(wù)端安裝:安裝appium-xcuitest-driver依賴笆檀,進(jìn)入WebDriverAgent安裝目錄,運(yùn)行bootstrap盒至。
$ cd /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent (如果WebDriverAgent 所在路徑和此不同酗洒,請(qǐng)自行查找)
$ mkdir -p Resources/WebDriverAgent.bundle
$ sh ./Scripts/bootstrap.sh
11.1、用Xcode打開(kāi)WebDriverAgent枷遂,并且編譯進(jìn)入WebDriverAgent 文件夾樱衷,可用如下方法:
若果編譯的過(guò)程中有語(yǔ)法錯(cuò)誤矩桂,重新安裝第10步。
11.4痪伦、建立服務(wù)WebDriverAgent:進(jìn)入WebDriverAgent 文件夾侄榴,建立服務(wù)雹锣。
$ cd /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
$ xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=真機(jī)的udid' test
運(yùn)行到最后會(huì)發(fā)現(xiàn)
而iphone多了一個(gè)WebDriverAgentRunner的app。手動(dòng)啟動(dòng)那個(gè)app的時(shí)候
原因是我們手機(jī)并并沒(méi)有信任這個(gè)apple id的開(kāi)發(fā)者癞蚕,到設(shè)置-通用-設(shè)備管理(描述文件) 信任你的apple id就可以了蕊爵。再次運(yùn)行上述命令,看到如下圖涣达,就成功了,
12. 安裝appium client端:appium client有ruby证薇,python度苔,java三個(gè)版本,可自行選擇自己喜歡的安裝即可浑度,這里只說(shuō)Python版安裝寇窑,具體見(jiàn)下面查看資料3。
pip安裝
$ pip install Appium-Python-Client
如果安裝過(guò)程出現(xiàn) could not create ‘/Library/Python/2.7/site-packages/appium’: Permission denied 錯(cuò)誤箩张,
使用 pip install Appium-Python-Client –user 命令可以安裝成功
源碼安裝 在Pipy上下載源碼安裝
$ tar -xvf Appium-Python-Client-X.X.tar.gz
$ cd Appium-Python-Client-X.X
$ python setup.py install
github安裝
$ git clone https://github.com/appium/python-client.git
$ cd python-client
$ python setup.py install
13甩骏、測(cè)試流程:
下載測(cè)試代碼并且測(cè)試,網(wǎng)上一個(gè)簡(jiǎn)單的測(cè)試代碼:erduoniba/appium_ios_sample_code先慷,自己寫可以參考Python + Appium+ IOS自動(dòng)化測(cè)試饮笛。
對(duì)于如何利用app-inspector獲取界面元素,請(qǐng)參考app-inspector官方文檔论熙。
13.1福青、先啟動(dòng)appium服務(wù)器:
$ appium -U xxxxxxx // xxxxx 真機(jī)設(shè)備ID
13.2、接下來(lái)連上真機(jī) 執(zhí)行測(cè)試用例代碼:
$ cd sample-code/examples/python
$ python ios_simple_device.py
test_scroll (__main__.SimpleIOSTests) ... ok
----------------------------------------------------------------------
Ran 1 test in 30.421s
OK
踩過(guò)的坑
1脓诡、1 - 10步安裝過(guò)程中的權(quán)限問(wèn)題:chmod -R 777 pathForFile无午,然后重新運(yùn)行該命令。
2祝谚、第11.4步我在安裝的時(shí)候一直沒(méi)有Listening on USB這一行信息宪迟,不過(guò)服務(wù)正常可以使用交惯。
3次泽、在測(cè)試過(guò)程中一直提示第11.4步第一個(gè)圖的錯(cuò)誤的話,拔數(shù)據(jù)線席爽,關(guān)測(cè)試機(jī)箕憾,重啟,重插拳昌。
參考資料
1袭异、Appium在 MAC 上搭建 appium1.6.3 過(guò)程:https://testerhome.com/topics/6962
2、appium1-macOS10.12下如何絲滑的使用appium?:http://www.reibang.com/p/05943804c25e
3炬藤、Appiumappium + iOS10.2 + Xcode8.2.1 + React Native 完成自動(dòng)化測(cè)試:https://testerhome.com/topics/7775