Appium是一款開源的跨平臺測試自動化工具,適用於本地,混合和行動網路和桌面應用程式。支援模擬器(iOS、Android)和真實裝置(iOS,Android,Windows,Mac)。
推薦大家先閱讀以下幾篇文章
iOS自動化測試詳細介紹
基於KIF的iOS UI 自動化測試和持續整合
準備工具
- Xcode
- PyCharm
- Command Line Tool
- HomeBrew 、Node 、npm ...
1.安裝Homebrew
Homebrew 是MACOSX
的軟體管理工具,粗俗點說就是ma界的qq軟體助手之類的東西。所以通過brew,安裝什麼chrome瀏覽器啊、atom編輯器之類的視覺化工具也是可以的。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"複製程式碼
2.安裝Node
-
1.使用Homebrew下載:
brew install node複製程式碼
-
2.或者從官方地址下載.pkg檔案安裝
3.安裝npm
npm 是node.js
界的程式/模組管理工具,也就是說npm只管理那些服務於JavaScript社群的程式。而且跨平臺,只要有node環境,windows和osx,以及其他unix like作業系統都可以使用。如不需要命令列啟動Appium可跳過此步驟。
-
切換淘寶映象安裝cnpm(由於某種原因,直接用npm下載安裝會有好多網路問題,安裝淘寶的cnpm要比npm好用):
npm install -g cnpm --registry=https://registry.npm.taobao.org複製程式碼
4.安裝carthage
類似cocoapods
管理第三方程式碼,自動將工程編譯為動態庫,僅支援iOS8以上。
brew install carthage複製程式碼
5.安裝其他工具
-
brew install libimobiledevice --HEAD複製程式碼
-
ideviceinstaller 負責給iOS裝置安裝解除安裝應用或者備份應用。該工具是基於libmobiledevice的,因此首先要完成libmobiledevice的編譯安裝
brew install ideviceinstaller複製程式碼
ideviceinstaller不支援iOS10。所以我們還需要安裝ios-deploy,ios-deploy是一個使用命令列安裝iosapp到連線的裝置的工具,原理是根據osx命令列工程呼叫系統底層函式,獲取連線的裝置、查詢/安裝/解除安裝app。
cnpm install -g ios-deploy複製程式碼
-
xcpretty 用於對xcodebuild的輸出進行格式化,可以不安裝
sudo gem install xcpretty複製程式碼
-
App Inspector 是執行在瀏覽器端的移動裝置 UI 檢視器,使用樹狀態結構檢視 UI 佈局,並且能自動生成 XPath,方便指令碼的編寫和生成。檢視AppInspector用法
cnpm install app-inspector -g複製程式碼
6.下載Appium-Python-Client
-
先安裝 pip
sudo easy_install pip複製程式碼
-
再安裝 Appium-Python-Client
sudo pip install Appium-Python-Client複製程式碼
7.下載appium和appium-doctor
這裡有兩種安裝appium的方式,一種是利用cnpm下載安裝:
cnpm install -g appium複製程式碼
cnpm install -g appium-doctor複製程式碼
推薦! 還可以選擇下載Appium-desktop安裝包,開啟Appium-desktop的dmg包,複製Appium.app到Applications資料夾中,即完成安裝。
8.配置appium-xcuitest-driver
appium-xcuitest-driver其中引用Facebook提供的WDA方案來驅動iOS的測試
// !如果WebDriverAgent 所在路徑和此不同,請自行查詢
cd /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
// 如果是安裝appium-desktop的話,路徑為
cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
複製程式碼
mkdir -p Resources/WebDriverAgent.bundle複製程式碼
sh ./Scripts/bootstrap.sh複製程式碼
該指令碼會使用Carthage下載所有的依賴,使用npm打包響應的js檔案。
常見報錯:Cannot find module ‘eslint-config-appium
加上引數-d即可:
sh ./Scripts/bootstrap.sh -d複製程式碼
執行成功截圖如下:
重點來了!!!
如果怎麼樣都無法成功,請從github上拉取一份最新的wda程式碼!
git clone https://github.com/facebook/WebDriverAgent.git
cd 到WebDriverAgent
sh ./Scripts/bootstrap.sh複製程式碼
執行成功後,把這一份程式碼替換到/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent下就OK了。一定要先執行成功,再替換!
參考連結:
Appium 1.6.5 and ios 10.3.3 真機測試
WebDriverAgent 天坑記
iOS 真機如何安裝 WebDriverAgent
9.執行appium-doctor,檢視結果
appium-doctor
info AppiumDoctor Appium Doctor v.1.4.3
info AppiumDoctor ### Diagnostic starting ###
info AppiumDoctor ✔ The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor ✔ Node version is 8.9.4
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/limaolin
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複製程式碼
10.配置Android_HOME、JAVA_HOME
下載Android SDK Manager推薦連結, 下載成功後,開啟終端,輸入cd到Android SDK的tools目錄所在路徑(從下載可見tools目錄,將tools目錄直接拖入到cd後面即可自動補全路徑)
再輸入命令./android sdk,彈出SDK Manager,如下圖:
vi ~/.bash_profile複製程式碼
新增HOME路徑:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
export CLASS_PATH=$JAVA_HOME/lib
export ANDROID_HOME=/usr/local/android-sdk-macosx複製程式碼
執行sources命令:
source ~/.bash_profile複製程式碼
再次執行appium-doctor
檢視結果,此時:
info AppiumDoctor Appium Doctor v.1.4.3
info AppiumDoctor ### Diagnostic starting ###
info AppiumDoctor ✔ The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor ✔ Node version is 8.9.4
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/limaolin
info AppiumDoctor ✔ ANDROID_HOME is set to: /usr/local/android-sdk-macosx
info AppiumDoctor ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
info AppiumDoctor ✔ adb exists at: /usr/local/android-sdk-macosx/platform-tools/adb
info AppiumDoctor ✔ android exists at: /usr/local/android-sdk-macosx/tools/android
info AppiumDoctor ✔ emulator exists at: /usr/local/android-sdk-macosx/tools/emulator
info AppiumDoctor ✔ Bin directory of $JAVA_HOME is set
info AppiumDoctor ### Diagnostic completed, no fix needed. ###
info AppiumDoctor
info AppiumDoctor Everything looks good, bye!
info AppiumDoctor複製程式碼
如果還沒有生效的話,進入終端的偏好設定:
修改shell的開啟方式為 /bin/bash 安裝zsh的童鞋自行修改為zshell的路徑~11.編譯WDA
WDA是appium自動化測試的核心。
配置正確的BundleId和證照,編譯WebDriverAgentLib
、WebDriverAgentRunner
cmd+U執行測試,或者
cd /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=真機的udid' test複製程式碼
開始測試
常見問題1:
解決方法: 在通用-描述檔案與裝置管理中信任剛才配置的開發者!重複剛才的命令就OK了~常見問題2:
Connection peer refused channel request for "dtxproxy:XCTestDriverInterface:XCTestManager_IDEInterface";
channel canceled Failed to run tests: The operation couldn’t be completed. (DTXProxyChannel error 1.)複製程式碼
解決方法: 重新插拔一下USB線
當出現如下:
在瀏覽器訪問ServerURL/status
地址,如果沒有出現listening on USB 如下圖:
開啟終端視窗,輸入iproxy 8100 8100
,此時在MAC Safari中訪問http://localhost:8100/status
確認WDA是否執行成功。如圖:
inspector的地址是http://localhost:8100/inspector
, inspector是用來檢視UI的圖層,方便寫測試指令碼用的。
這裡的inspector是wda自帶的,Appium-desktop桌面版的inspector功能更加豐富,還可以錄製指令碼,搭建環境更省時間,建議大家使用桌面版。
至此,Appium環境搭建就基本完成了。