1、安裝brew 軟件包管理工具:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2、安裝nodejs :brew install node 安裝完node 后需要設置通過npm安裝的軟件包放置位置設置為可以讀寫權限,chmod -R 777 /usr/local/lib/node_modules/ (注:在操作時時測試機器,賦給777權限沒有問題,如果是其他機器,需要考慮是否給了不該給的權限) 其中 /usr/local/ 可以通過npm config get prefix 獲得,這樣會省去一些權限的問題。
3、安裝macaca 參考官網:https://macacajs.github.io/environment-setup
4、安裝macaca inspect :npm i app-inspector -g
安裝完后進入目錄:/usr/local/lib/node_modules/app-inspector/node_modules/webdriveragent/WebDriverAgent,
在這個目錄下面新建一個文件夾並執行下命令
mkdir -p Resources/WebDriverAgent.bundle
sh ./Scripts/bootstrap.sh -d
用xcode 打開WebDriverAgent.xcodeproj文件,對項目文件進行重新編譯,
編譯:WebDriverAgentLib編譯選項
修改Bundle id,Team選為登錄xcode的appid,
編譯:WebDriverAgentRunner
每個編譯一定要出現succeeded才行
編譯完成,就可以啟動macaca inspector 命令:app-inspector --port 56789 -u f9b87274e2a24b310399ddxxxxxx
啟動后再瀏覽器中輸入localhost:56789 訪問,(實際端口以執行命令顯示為准),手機會安裝一個程序,需要手動點擊這個名為:WebDriverAgentRunner的app, 會提示要在設置中信任改開發者開發的軟件,進入手機>設置>通用>設備管理 >開發者應用 >信任WebDriverAgentRunner
再次安裝執行命令:
app-inspector --port 56789 -u f9b87274e2a24b310399dd56dd80f8561d99fdac --verbose
提示出錯誤:xctest client exit with code: 65
>> xctest-client.js:181:14 [master] pid:33863 xctest client exit with code: 65, signal: null
>> xctest-client.js:145:16 [master] pid:33863 deviceconsole exit with code: null, signal: SIGKILL
>> xctest-client.js:255:14 [master] pid:33863 Fail to start xctest: undefined
>> app-inspector:66:44 [master] pid:33863 undefined
解決辦法:在xcode 中編譯下:WebDriverAgentUSBClient
再刪除手機上的WDA,重復啟動下
app-inspector --port 56789 -u f9b87274e2a24b310399dd56dd80f8561d99fdac --verbose
在再手機中信任開發者。就可以了,這個問題是簽名問題
又由於macaca inspector 無法查看所有的元素,只有通過代碼獲取運行時xml頁面元素布局 獲取xml內容方法為:System.out.println(driver.getPageSource());
獲取如下,有了這個xml文件內容,就能根據實際內容得到xpath name 登錄內容,完美解決元素查找問題
<?xml version="1.0" encoding="UTF-8"?><AppiumAUT><XCUIElementTypeApplication type="XCUIElementTypeApplication" name="xxxx" label="xxxx" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeWindow type="XCUIElementTypeWindow" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeNavigationBar type="XCUIElementTypeNavigationBar" name="我的xxxx" visible="true" enabled="true" x="0" y="20" width="414" height="44"> <XCUIElementTypeButton type="XCUIElementTypeButton" visible="true" enabled="true" x="12" y="26" width="41" height="30"/> <XCUIElementTypeButton type="XCUIElementTypeButton" name="返回" label="返回" visible="true" enabled="true" x="12" y="31" width="21" height="22"/> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="我的xxxx" name="我的xxxx" label="我的xxxx" visible="true" enabled="true" x="170" y="28" width="74" height="27"/> <XCUIElementTypeButton type="XCUIElementTypeButton" name="MyReceiveSearch" label="MyReceiveSearch" visible="true" enabled="true" x="367" y="29" width="27" height="25"/> </XCUIElementTypeNavigationBar> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="64" width="414" height="672"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="64" width="414" height="57"> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="xxxx(0)" name="xxxx(0)" label="xxxx" visible="true" enabled="true" x="0" y="64" width="138" height="57"/> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="xxxx(14)" name="xxxx14)" label="xxxx" visible="true" enabled="true" x="138" y="64" width="138" height="57"/> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="xxxx(10)" name="xxxx(10)" label="xxxx" visible="true" enabled="true" x="276" y="64" width="138" height="57"/> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="117" width="138" height="4"/> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="120" width="414" height="1"/> </XCUIElementTypeOther> <XCUIElementTypeScrollView type="XCUIElementTypeScrollView" visible="true" enabled="true" x="0" y="120" width="414" height="616"> <XCUIElementTypeTable type="XCUIElementTypeTable" name="無更多記錄" label="無更多記錄" visible="true" enabled="true" x="0" y="120" width="414" height="616"/> </XCUIElementTypeScrollView> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeOther> </XCUIElementTypeWindow> <XCUIElementTypeWindow type="XCUIElementTypeWindow" visible="false" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" enabled="true" x="0" y="0" width="414" height="736"/> </XCUIElementTypeWindow> <XCUIElementTypeWindow type="XCUIElementTypeWindow" visible="true" enabled="true" x="0" y="0" width="414" height="736"> <XCUIElementTypeStatusBar type="XCUIElementTypeStatusBar" visible="true" enabled="true" x="0" y="0" width="414" height="20"> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" enabled="true" x="0" y="0" width="414" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" enabled="true" x="0" y="0" width="414" height="20"> <XCUIElementTypeOther type="XCUIElementTypeOther" name="信號強度:5 格(共 5 格)" label="信號強度:5 格(共 5 格)" visible="true" enabled="true" x="6" y="0" width="34" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="中國電信 網絡" label="中國電信 網絡" visible="true" enabled="true" x="43" y="0" width="50" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" value="SSID" name="4G" label="4G" visible="true" enabled="true" x="102" y="0" width="17" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="下午6:13" label="下午6:13" visible="true" enabled="true" x="184" y="0" width="51" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="勿擾模式" label="勿擾模式" visible="true" enabled="true" x="343" y="0" width="9" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="方向已鎖定" label="方向已鎖定" visible="true" enabled="true" x="358" y="0" width="12" height="20"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="電池電量:100%, 使用交流電源" label="電池電量:100%, 使用交流電源" visible="true" enabled="true" x="376" y="0" width="33" height="20"/> </XCUIElementTypeOther> </XCUIElementTypeStatusBar> </XCUIElementTypeWindow> </XCUIElementTypeApplication></AppiumAUT>