MAC下搭建 appium+ios+python 自動化測試環境(一)

Mac 下 appium +python 自動化測試系列:
Mac 下搭建 appium+ios+python 自動化測試環境(一)
Mac 下 appium 自動化測試iOS 測試配置和腳本編寫(二)
Mac 下 搭建appium +android+python 自動化測試環境(三)
Mac 下 appium 自動化測試 Android 測試配置和腳本編寫(四)

環境搭建要求,MAC 機器一臺,要求 Xcode 8.0以上,有 Apple ID 賬號,懂一點點終端命令,最重要的是能夠聯網,如果都 OK 了,那就可以開搞了!

  • 1.安裝 Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 2.安裝 libimobiledevice
brew install libimobiledevice
  • 3.安裝 ideviceinstaller(真機安裝相關)
brew install  ideviceinstaller
  • 4.安裝 carthage
brew install carthage

安裝 carthage容易出現以下錯誤

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

原因是carthage已經安裝,但是沒有 link 到 brew,還有就是文件夾權限問題,在終端輸入:

sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks
brew link carthage
This package has installed:
    ?   Node.js v8.11.2 to /usr/local/bin/node
    ?   npm v5.6.0 to /usr/local/bin/npm
Make sure that /usr/local/bin is in your $PATH.
  • 6.安裝 cnpm(主要是因為墻和網絡的問題,換成淘寶的 cnpm)
npm install -g cnpm --registry=https://registry.npm.taobao.org

安裝這個坑比較多,一般會出現以下提示:

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

這個時候你換成

sudo npm install -g cnpm --registry=https://registry.npm.taobao.org

如果臉不好,或者網絡問題,又會出現以下問題:

npm WARN registry Unexpected warning for https://registry.npm.taobao.org/: Miscellaneous Warning ETIMEDOUT: request to https://registry.npm.taobao.org/cnpm failed, reason: connect ETIMEDOUT 114.55.80.225:443
npm WARN registry Using stale package data from https://registry.npm.taobao.org/ due to a request error during revalidation.

網上找一了一圈,還是沒解決,最后還是根據權限提示,自己摸索出來的,先打開目錄權限,然后再安裝鏡像

sudo chown $(whoami):admin /usr/local/lib/node_modules/

npm install -g cnpm --registry=https://registry.npm.taobao.org
  • 7.安裝 iOS-deploy
cnpm install -g ios-deploy

這個會出現超時錯誤

Get /binary-mirror-config/latest from https://registry.npm.taobao.org error: ConnectionTimeoutError: Connect timeout for 5000ms, GET https://registry.npmjs.org/binary-mirror-config/latest -2 

感覺就是淘寶的鏡像源的問題,沒有同步或者什么原因,解決方法如下:

//設置鏡像源
npm config set registry https://registry.npm.taobao.org --global
//配置鏡像列表
npm config list
//執行安裝指令
npm install -g ios-deploy
  • 8.安裝 xcpretty
gem install xcpretty

如果沒使用代理或者公司網有限制的話,一般沒問題。

  • 9.安裝 appium1.8 release版本可以終端安裝也可以下載桌面端appium 桌面端下載鏈接 選擇 dmg 或者 desktop 版本。因為我們前面配置過 npm 的鏡像源了,所以直接:
npm install -g appium

安裝完順便安裝 appium doctor

npm install -g appium-doctor

公司網絡太坑,我用的手機熱點下的,媽蛋啊,心疼我的流量!

接著安裝 python 的環境

git clone git@github.com:appium/python-client.git
cd python-client
python setup.py install
  • 10.安裝 wd
npm install -g wd
  • 11.安裝 webpack
npm i -g webpack 
  • 12.安裝 appium-xcuitest-driver依賴
#如果已經安裝,請先卸載
#npm uninstall appium-xcuitest-driver
npm install appium-xcuitest-driver
  • 13.配置 WebDriverAgent

首先,我想說這個 appium 自帶的項目,有毒啊!!這個真的有毒啊,絕對有毒啊!

cd  /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent

mkdir -p Resources/WebDriverAgent.bundle

sh ./Scripts/bootstrap.sh

運行完,就是報fatal: could not read Username for 'https://github.com': terminal prompts disabled這個錯誤啊,原因竟然是,依賴庫里面的 Cartfile里面的依賴庫不見了啊!我搞了一天多,搜索都搜索的要吐了!

正確姿勢如下,請 get??
先進到用戶目錄下或者根目錄下:

cd ~

git clone https://github.com/facebook/WebDriverAgent.git

cd WebDriverAgent

mkdir -p Resources/WebDriverAgent.bundle

sh ./Scripts/bootstrap.sh

一頓操作猛如虎之后, 進入到appium-xcuitest-driver路徑下

cd  /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/

rm –rf WebDriverAgent(移除自帶的,當然如果沒有安裝自帶的就不用執行這個指令)

ln -s ~/WebDriverAgent WebDriverAgent  (其中~/WebDriverAgent是源地址,后面的是目標地址,也就是把你 clone 的項目鏈接到 driver 里面)
  • 14.編譯 WebDriverAgent

1.進入目標文件或者你剛鏈接的源文件也行(鏈接之后源文件改變,引用文件也會改變)


WebDriverAgent項目路徑
  1. 雙擊WebDriverAgent.xcodeproj打開此項目,這個項目由facebook開源.在這里我們的目的就是更改一些配置,讓他能夠編譯成功,首先編譯之前你得有 ID賬號,并且登錄你的Xcode賬戶


    配置開發證書等

    接著編譯WebDriverAgentRunner


    配置 runner 的證書信息

    配置 setting 信息
  • 15.建立服務WebDriverAgent (手機和MAC都在同一個網段下,且都能連接外網)關閉Xcode,進入WebDriverAgent 文件夾
cd  /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent

xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=真機的udid' test

如果終端出現以下信息,表示編譯 OK

Testing failed:
    Early unexpected exit, operation never finished bootstrapping - no restart will be attempted
** TEST FAILED **

這個是時候iphone多了一個WebDriverAgentRunner的app


信任證書選項

我們在通用設置的描述文件里面信任我們的開發證書
之后再次運行上面的指令:

xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=真機的udid' test

如果出現以下信息,表示運行成功了:

2018-04-26 16:25:33.445429+0800 WebDriverAgentRunner-Runner[314:21633] Continuing to run tests in the background with task ID 1
Test Suite 'All tests' started at 2018-04-26 16:25:33.508
Test Suite 'WebDriverAgentRunner.xctest' started at 2018-04-26 16:25:33.509
Test Suite 'UITestingUITests' started at 2018-04-26 16:25:33.510
Test Case '-[UITestingUITests testRunner]' started.
    t =     0.01s Start Test at 2018-04-26 16:25:33.518
    t =     0.01s Set Up
2018-04-26 16:25:33.527655+0800 WebDriverAgentRunner-Runner[314:21633] Built at May 22 2018 07:53:09
2018-04-26 16:25:33.552834+0800 WebDriverAgentRunner-Runner[314:21633] ServerURLHere->http://172.20.10.7:8100<-ServerURLHere

這個時候我們來一遍驗證吧。

appium-doctor --ios

出現如下信息:

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.11.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/xxxxxx
info AppiumDoctor ### Diagnostic completed, no fix needed. ###
info AppiumDoctor 
info AppiumDoctor Everything looks good, bye!
info AppiumDoctor 

至此,說明我們環境配置成功了!

整個環境配置踩得的坑不要不要的,主要是公司是內網,上外網用的代理,也沒法掛 VPN,希望以上的環境搭建真實有效的減少你們踩坑的幾率,愉快的開始自動化測試路程!下一篇,我會寫一下如何進行腳本測試和 appium desktop 的使用!

參考:

在 MAC 上搭建 appium1.6.3 過程

Xcode9.0+appium1.6.5真機環境運行app及遇坑知多少

appium mac 下 安裝及踩坑

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容