一、技術原理
安卓H5頁面自動化測試其本質是通過appium驅動手機瀏覽器執行測試腳本。如圖所示:
二、常用方法
1、檢測否開啟webview
判斷真機或者模擬器是否開啟webview,可以用下面的命令:
$ adb shell cat /proc/net/unix | grep webview
結果:
00000000: 00000002 00000000 00010000 0001 01 101240 @webview_devtools_remote_20149
2、查看手機webview版本
- 直接在shell腳本中顯示版本
adb shell dumpsys package com.google.android.webview | find "versionName"
結果:
versionName=75.0.3770.143
- 直接打開手機瀏覽器,並顯示版本
adb shell am start -a android.intent.action.VIEW -d https://liulanmi.com/labs/core.html
手機或模擬器種會打開瀏覽器,並顯示版本號,如圖:
- 通過PC端chrome瀏覽器查看
直接通過PC端chrome瀏覽器,也可以查看到瀏覽器版本號,只需在瀏覽器地址欄輸入[chrome://inspect/#devices](chrome://inspect/#devices)
,但需要注意用到inspect檢查元素,可能需要正確的上網,也就是能訪問google。
3、定位元素
保證瀏覽器正確上網后,在PC段瀏覽器地址欄輸入chrome://inspect/#devices
,然后點擊inspect檢查元素。
點擊inspect后,顯示元素信息。
4、下載chromedriver
查看到當前瀏覽器版本是75,然后訪問chromedriver下載頁,下載對應版本的driver。

三、編寫測試用例
import time
from appium import webdriver
class TestH5:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7'
desired_caps['deviceName'] = 'Android Emulator'
#瀏覽器名稱
desired_caps['browserName'] = 'Browser'
#chromedriver位置
desired_caps['chromedriverExecutableDir'] = r"F:\software\webdriver\75"
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(5)
def app_quit(self):
self.driver.quit()
def test_search(self):
self.driver.get('http://www.baidu.com')
self.driver.find_element_by_id('index-kw').send_keys('test')
self.driver.find_element_by_id('index-bn').click()
time.sleep(5)
結果:
四、下載
1、chromedriver
地址:http://chromedriver.storage.googleapis.com/index.html
淘寶鏡像:https://npm.taobao.org/mirrors/chromedriver
2、chromium62
地址:https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win_x64/620167/
3、appium
地址:http://appium.io/docs/en/writing-running-appium/caps/