一、技术原理
安卓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。
![image-20200316213702502](https://image.shishitao.com:8440/aHR0cHM6Ly9waG90by0xMzAxMzc1NjM5LmNvcy5hcC1jaGVuZ2R1Lm15cWNsb3VkLmNvbS8yMDIwMDkxMjExMTQyMi5wbmc%3D.png?w=700&webp=1)
三、编写测试用例
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/