mac OS 下超詳細搭建 Appiumm 自動化測試環境
現在很多公司,尤其是研發團隊開始使用mac OS了,測試團隊也不列外。所以,特意寫下 mac OS 下自動化測試的各種環境搭建吧,計劃分期寫,感興趣的持續關注。
Appium 在 mac OS 環境搭建
Appium 一年之前相比,有了很大的改變
1、iOS 9 之前一直以 instruments 下的 UIAutomation 為驅動底層技術(弊端由於 instruments 的限制,單臺 mac 只能對應單臺裝置);
2、iOS 9.3 時代推出 XCUITest 工具,用以替代 UIAutomation;
3、iOS 10 時代蘋果直接廢棄了 UIAutomation、Facebook 推出 WebDriverAgent(實現的 server 能夠支援單臺 mac 對應多個裝置);4、Appium 在iOS 9.3 後全面採用 WebDriverAgent 的方案。
首先,我們要知道環境搭建所需工具:

其次,重中之重: 版本很重要,xcode9.3 + node10.11.0 + appium1.7.0,如果根據以下配置仍然不能成功執行,那麼99%是版本的問題。
下面開始逐個安裝
1、安裝node
brew install node
通過這種方法安裝的node是最新版本,因為我本地需要有node 8.1.1版本,所以使用nvm進行安裝。
2、下載Xcode 和 Command Line Tools:需要有個AppleID,並根據自己電腦OS版本。

下載後直接安裝即可,注意Xcode保持在application資料夾下只有一個唯一版本。
3、下載安裝JDK

下載完成後直接安裝即可。
4、安裝Android SDK
很多資料都推薦使用brew安裝,但是實踐後發現SDK檔案為空,所以使用Android studio來安裝。
如果開啟Android studio報錯,直接cancel到下一步即可;若使用default方式安裝,也可以在configuration裡重新下載相關tools。

5、安裝appium最新版本
這裡不使用npm下載方式是因為下載過程較長,且容易報錯。
6、安裝appium-doctor
npm install -g appium-doctor
7、配置Android環境變數
在 ~/.bash_profile檔案中新增path
export ANDROID_HOME=~/Library/Android/sdk/
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home #jdk安裝路徑
export PATH=~/bin:$PATH:/usr/local/bin:$ANDROID_HOME/platform-tools/:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
在appium的configurations裡設定

8、安裝其他依賴
brew install libimobiledevice --HEAD
npm install -g ios-deploy
brew install carthage
libimobiledevice / ideviceinstaller 庫:相當於 android 的 adb,是 Appium 底層用到的工具之一,用於獲取 iOS 裝置資訊
ios-deploy:檢視裝置應用
carthage:類似於 java 的 maven,專案依賴管理,主要是 WebDriverAgent 使用
9、配置WebDriverAgent
(1)首先進入WebDriverAgent所在檔案目錄下:
/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
(2)根目錄下執行:
./Scripts/bootstrap.sh
(3)開啟 WebDriverAgent.xcodeproj 工程檔案
(4)新增自己的account:Xcode -> Preferences -> account -> 左下角 +

(5)更改bundle id:這裡的bundle id就相當於一個App的唯一識別號,更改的規則隨意,只要確認不會與有重複即可;所有的targets都需要修改。
10、驗證是否環境已經安裝成功了
(1)終端輸入: appium-doctor
如果環境都已配置好,應該是文章最開始圖片中的結果。
(2)執行WebDriverAgentRunner

按照圖中,1:選擇scheme為WebDriverAgentRunner -> 2:選擇執行裝置,模擬器和真機都可以 -> 3:選擇要構建的方式 ->4:選擇Test方式的步驟驗證是否能成功執行,WebDriverAgentRunner
如果真機執行後彈出需要在設定中信任證書。
Verify the Developer App certificate for your account is trusted on your device. Open Settings on iPhone and navigate to General -> Device Management, then select your Developer App certificate to trust it.
按照步驟操作後,重新執行就可以了。

成功執行結果圖
11、遇到的問題總結
1)appium版本不支援xcode版本(需要呼叫XCUITest),重新下載可匹配的Xcode版本。
Xcode9.3 + Appium1.6.4時,執行WebDriverAgentRunner報錯。
Early unexpected exit, operation never finished bootstrapping - no restart will be attempted

解決方法:更新Appium版本到1.7.0
2)appium無法調起webdriverrunner(webdriveragent更新),檢視是否因為webdriveragent證書籤名、依賴framework、serverURL丟擲錯誤資訊。
安裝依賴framework時報錯
Falling back to building from the source
A shell task (/usr/bin/xcrun xcodebuild -project /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent/Carthage/Checkouts/CocoaAsyncSocket/CocoaAsyncSocket.xcodeproj CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES -list) failed with exit code 72:
xcrun: error: unable to find utility "xcodebuild", not a developer tool or in PATH
解決方法:選擇Command Line Tools

Xcode -> Preferences -> Locations -> Command Line Tools。
3)長時間測試,server中斷,這個無法解決,appium自身問題。
4)webdriveragent啟動失敗,status、inspector無法正常檢視,檢視版本。
5)appium指令碼啟動失敗,檢視指令碼中的配置。
如何學習?學習沒有資料?請關注本公眾號,回覆「資料」試試,獲得更多視訊資料和入群資格。