轉載自:https://www.jianshu.com/p/a9aaf03c39cc
5、更新Appium中的WebDriverAgent
1)下載webdriveragent https://github.com/appium/WebDriverAgent.git
git clone https://github.com/appium/WebDriverAgent.git
2)cd 進入下載後的WebDriverAgent文件
3)執行 ./Scripts/bootstrap.sh
4)用Xcode打開WebDriverAgent.xcodepro文件
配置WebDriverAgentLib和WebDriverAgentRunner的證書
如圖所示配置,
修改bundleid ,隨便修改,只要不跟已經註冊過的衝突就行了,要求唯一
team選擇真實的開發者賬號
手機連上電腦
選擇自己的手機
然後點擊test運行
如果出現如下圖錯誤,在ios設備-設置-通用-描述文件和設備管理,信任一下開發者,再次test運行就不會出現錯誤了
運行成功後,在xcode控制檯可以打印出ip地址和端口號
進入到Appium中的WebDriverAgent目錄,目錄路徑如
/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/
將自己下載並編譯後的WebDriverAgent替換Appium原有的WebDriverAgent
(六)利用Appium-Python-Client進行iOS的自動化測試
這一步是將python與appium關聯起來
在終端輸入:pip install Appium-Python-Client,安裝即可
(七)Appium中開啓iOS webview調試進行h5自動化測試
在做ios的h5頁面測試時,測試包必須要使用develop證書打包,app要是debug包,纔可以進行真機測試
實踐:如果在ios真機的webview頁面執行appium自動化,需要做以下準備:
1)安裝 ios_webkit_debug_proxy,在執行webview自動化時必須啓動該服務
http://appium.io/docs/en/writing-running-appium/web/ios-webkit-debug-proxy/index.html
a.brew install ios-webkit-debug-proxy
2)運行ios-webkit-debug-proxy
a.官方提供的命令:ios_webkit_debug_proxy -c UUID:27753 -d
b.使用chrome dev-tools調試
i.運行命令
ios_webkit_debug_proxy -f chrome-devtools://devtools/bundled/inspector.html
ii.根據日誌提示,獲得端口並打開頁面對應端口頁面,如lcoalhost:9221
iii.點擊設備提示拷貝鏈接到地址欄即可訪問;
c.使用safari調試
i.打開mac上的safari瀏覽器開發者模式
ii.打開設備上的webview頁面
iii.點擊safari瀏覽器菜單中的開發-device,選擇進程;
在appium中使用ios-webkit-debug-proxy :
第一種方式:set the desired capability,加上"startIWDP": true,
// example desired capabilities
{
"browserName": "Safari",
"platformName": "iOS",
"deviceName": "iPhone 7",
"automationName": "XCUITest",
"startIWDP": true,
"udid": "auto"
}
第二種方式:直接手動啓動ios-webkit-debug-proxy,然後再執行自動化,注意此時需要制定端口27753(appium默認iwdp端口爲這個)以及設備ID,即
ios_webkit_debug_proxy -c 17d5c2ae122f79a23fbd29168e0948dce22e8ce5:27753 -d
二、元素定位方法篇
(一)通過weidtor定位元素
1)安裝
終端中輸入,安裝weditor
pip install weditor
2)啓動
注意:先要運行webdriveragent
然後終端輸入命令:python -m weditor
點擊connect後,出現手機畫面,reload是同步手機畫面
點擊界面元素,右側出現該元素的定位信息
(二)通過appium desktop定位
1、運行Appium-Desktop ,點擊start server,開啓server
2、點擊Start Inspector Session按鈕
3、配置Desired Capabilities
如何獲取設備信息?
設備連接上電腦,然後在終端輸入idevice_id -l,就可以顯示當前設備的udid
ideviceinfo 顯示當前設備的信息,可以查看當前的deviceName, platformName
SHAPE \* MERGEFORMAT
ideviceinstaller -l 查看當前設備所有安裝的app的信息
配置好信息後,點擊start session
運行成功後,會彈出一個控制界面,在該界面中可以控制手機上正在運行的程序 ,可以定位元素
三、python+appium+ios,自動化測試實踐
(一)新建文件test.py
(二)簡單自動化腳本編寫
#encoding=utf-8
import time
import os
import unittest
from appium import webdriver
#初始化
class MyTestCase(unittest.TestCase):
def setUp(self):
desired_caps = {
'platformName': 'iOS',
'deviceName': 'iphone',
'platformVersion': '12.4',
'bundleId' : 'com.jack.TongHua',
'udid' : 'bf5ffb03be1c90e05665f41dd648b8fb60a0c2c0', 'startIWDP':'true'
}
# 初始化驅動
self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
def testRefresh(self):
#點擊刷新按鈕
self.driver.find_element_by_accessibility_id("btn update").click()
def tearDown(self):
#退出
self.driver.quit()
if __name__ == '__main__':
unittest.main()
1、對類MyTestCase添加setUp函數(這是測試用例執行前的準備動作,負責告訴appium監聽那個端口、與那個手機連接、安裝那個應用等信息)
2、對類MyTestCase添加TearDown函數(這是測試用例執行結束後的動作,可以執行保存執行結果等操作)
(三)腳本寫好後,先要運行webdriveragent
方法一:用xcode的運行webdriveragent
方法二:使用命令行運行webdriveragent
先切到webdriveragent目錄下,然後輸入 xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination id='bf5ffb03be1c90e05665f41dd648b8fb60a0c2c0' test
(四)執行自動化測試腳本
(五)pyhton+appium+iOS,遍歷真機的webview
1、運行ios_webkit_debug_proxy
ios_webkit_debug_proxy -c bf5ffb03be1c90e05665f41dd648b8fb60a0c2c0:27753 -d
2、driver.contexts獲得頁面所有context,這個方法會將context存在list裏,打印出list內容,查看你要的webview在第幾個,用switch_to.context()切換到那個webview即可,代碼如下
# 假設我的目標webview存在list裏的第三個
myWebview = self.driver.context[2]
self.driver.switch_to.context(myWebview)
這樣就切換到了目標webview,接下來就可以像web自動化一樣,操作html元素了。
3、怎麼得到元素屬性
1).啓動webkit服務,並指定谷歌瀏覽器進行監聽
ios_webkit_debug_proxy -f chrome-devtools://devtools/bundled/inspector.html
2).運行用例代碼,停留在含webview的頁面
3).谷歌瀏覽器地址輸入http://127.0.0.1:92223,就可以看到手機的webview url
4)將url複製出來,就可以像定位web元素一樣去定位h5元素