Appium+Python實現iOS自動化測試~環境搭建

Appium是一款開源的跨平臺測試自動化工具,適用於本地,混合和移動網絡和桌面應用程序。支持模擬器(iOS、Android)和真實設備(iOS,Android,Windows,Mac)。html

推薦你們先閱讀如下幾篇文章java

iOS自動化測試詳細介紹
基於KIF的iOS UI 自動化測試和持續集成node

準備工具

  • Xcode
  • PyCharm
  • Command Line Tool
  • HomeBrew 、Node 、npm ...

1.安裝Homebrew

HomebrewMACOSX的軟件管理工具,粗俗點說就是ma界的qq軟件助手之類的東西。因此經過brew,安裝什麼chrome瀏覽器啊、atom編輯器之類的可視化工具也是能夠的。android

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"複製代碼

2.安裝Node

  • 1.使用Homebrew下載:ios

    brew install node複製代碼
  • 2.或者從官方地址下載.pkg文件安裝git

3.安裝npm

npmnode.js界的程序/模塊管理工具,也就是說npm只管理那些服務於JavaScript社區的程序。並且跨平臺,只要有node環境,windows和osx,以及其餘unix like操做系統均可以使用。如不須要命令行啓動Appium可跳過此步驟。github

  • 切換淘寶鏡像安裝cnpm(因爲某種緣由,直接用npm下載安裝會有好多網絡問題,安裝淘寶的cnpm要比npm好用):chrome

    npm install -g cnpm --registry=https://registry.npm.taobao.org複製代碼

4.安裝carthage

相似cocoapods管理第三方代碼,自動將工程編譯爲動態庫,僅支持iOS8以上。shell

brew install carthage複製代碼

5.安裝其餘工具

  • libimobiledevice 使用指南macos

    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和證書,編譯WebDriverAgentLibWebDriverAgentRunner

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環境搭建就基本完成了。

相關文章
相關標籤/搜索