背景
前幾天接到H5開發人員那邊的業務開發需求單,說想將H5接入到自動化系列中,特此記錄分享一下。
也可參考testerhome發表的文章鏈接:https://testerhome.com/topics/7866
環境前置准備
- 手機與電腦USB連接,開啟USB調試模式,通過adb devices可查看到此設備。
- 電腦端、移動端安裝chrome瀏覽器。(盡量保證移動端chrome版本低於電腦端)
- App webview開啟debug模式
- 在電腦端Chrome瀏覽器地址欄輸入chrome://inspect/#devices,進入調試模式:
- 此時頁面顯示了手機型號、驅動名稱、APP要調試的WebView名稱
- 點擊inspect,若成功加載與APP端相同界面的調試頁面,則配置成功
- 若獲取不到WebView或者調試頁面預覽框顯示空白,則需要進行VPN破解–安裝翻牆軟件(由於默認的DevTools使用的是appspot服務器,這在國內是需要翻越GWF)
嘗試解決方法:
1、在windows host文件中增加:
61.91.161.217 chrome-devtools-frontend.appspot.com
61.91.161.217 chrometophone.appspot.com
2、使用翻牆軟件,如Lantern藍燈
環境檢查
App webview 調試模式檢查與開啟
-
基礎檢查方式
- 打開app對應的h5頁面,在chrome://inspect/#devices地址中,檢查是否顯示對應的webview,如沒有,則當前未開啟調試模式。
- 在自動化代碼中,進入到對應的H5頁面,輸出當前context,如果一直顯示為Natvie,則webview未開啟。
-
開啟方式
在app中配置如下代碼(在WebView類中調用靜態方法setWebContentsDebuggingEnabled):if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); }
注:此步驟,一般需要App前端開發人員協助增加
瀏覽效果
chrome://inspect/#devices地址效果圖類似如下:
點擊inspect,正常則顯示為如下:
代碼實現
下述演示demo,均以微信App中的H5為例:
微信默認H5調試模式處於關閉,可用微信打開聊天窗口,輸入debugx5.qq.com, 在彈出內核調試【信息】頁面中 勾選"是否打開TBS內核Inspector調試功能" 來打開調試功能。
Python+Appium+WebDriver
__author__ = 'mikezhou' #coding=utf-8 #appium 微信h5自動化示例 from appium import webdriver import time packageName='com.tencent.mm' appActivity='.ui.LauncherUI' desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '5.1.1' desired_caps['deviceName'] = 'K31GLMA660800338' desired_caps['appPackage'] = packageName desired_caps['appActivity'] = appActivity desired_caps['fullReset'] = 'false' desired_caps['unicodeKeyboard'] = 'True' desired_caps['resetKeyboard'] = 'True' desired_caps['fastReset'] = 'false' desired_caps['chromeOptions']={'androidProcess': 'com.tencent.mm:tools'} #驅動H5自動化關鍵之一 driver = webdriver.Remote('http://127.0.1.1:4723/wd/hub', desired_caps) driver.implicitly_wait(30) driver.find_element_by_name('我').click() print driver.contexts driver.find_element_by_name('相冊').click() driver.find_element_by_xpath("//*[contains(@text,'正在繁星直播')]").click() print driver.current_context driver.find_element_by_xpath("//*[contains(@text,'正在繁星直播')]").click() print driver.current_context driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') print driver.current_context print driver.page_source driver.find_element_by_xpath('//*[@id="btnRecommend"]/div[1]').click() driver.switch_to_default_content() time.sleep(2) driver.quit()
Python+AppiumLibrary+RobotFramework
注:RF關鍵字與用例部分,只提供關鍵代碼,一些前置需要導入的library,自行添加:
App啟動關鍵字
Open MobileFx Android App_H5 [Arguments] ${remote_url} ${deviceName} ${appActivity} ${appPackage} ${platformVersion} ${Process} ... ${app}=${Empty} [Documentation] *啟動手機繁星app_H5[Android]* ... ... 入參順序: ... ... Arguments: 遠程服務地址|設備名稱|待測應用appActivity| 待測應用package包名|平台版本號|webveiw進程名 ... ... Examples: ... ... | Open MobileFx Android App H5 | http://localhost:4723/wd/hub | Android Emulator | .ui.LauncherUI | 'com.tencent.mm | 4.4.2 | com.tencent.mm:tools | ${androidProcess}= Create Dictionary androidProcess=${Process} Open Application ${remote_url} alias=fanxingappForAndroid platformName=Android deviceName=${deviceName} automationName=appium appActivity=${appActivity} ... appPackage=${appPackage} platformVersion=${platformVersion} unicodeKeyboard=True resetKeyboard=True app=${app} chromeOptions=${androidProcess}
套件用例區
*** Settings *** Suite Setup 啟動app Suite Teardown Close All Applications Library AppiumLibrary *** Variables *** ${appActivity} .ui.LauncherUI ${appPackage} com.tencent.mm ${deviceName} ${get_deviceName} ${platformVersion} ${get_platform_version} ${remote_url} ${android_remote_url} ${androidProcess} com.tencent.mm:tools *** Test Cases *** 微信分享驗證 [Documentation] 檢查首頁搜索 [Tags] mikezhou main online [Setup] [Timeout] Click Element Wait name=我 ${contexts} get_contexts ${current_context} get_current_context log ${contexts} log ${current_context} Click Element Wait name=相冊 ${current_context} get_current_context log ${current_context} Click Element Wait xpath=//*[contains(@text,'助力鹿晗')] ${current_context} get_current_context log ${current_context} Click Element Wait xpath=//*[contains(@text,'助力鹿晗')] ${current_context} get_current_context log ${current_context} Switch To Context WEBVIEW_com.tencent.mm:tools ${current_context} get_current_context log ${current_context} ${page} Log Source log ${page} Page Should Contain Text 鹿晗 timeout=15 Page Should Contain Text 概念詮釋冒險精神 Click Element Wait xpath=/html/body/div[1]/div sleep 3 [Teardown] *** Keywords *** 啟動app Open MobileFx Android App_H5 ${remote_url} ${deviceName} ${appActivity} ${appPackage} ${platformVersion} ${androidProcess}
結束語
感慨最近事事不利,感昌近十幾天了都未好轉。
福利
最近TesterHome社區創始人思寒在極客時間出版了個人專欄,針對移動端自動化測試實戰,推薦給大家,希望對大家有所幫助