appium自动化测试之H5页面测试

逆水行舟用力撑,一篙松劲退千寻。这篇文章主要讲述appium自动化测试之H5页面测试相关的知识,希望能为你提供帮助。
appium定位并操作app H5页面元素,以及一些报错和解决方案参考链接
https://testerhome.com/topics/7866
https://sutune.me/2018/04/30/appium-h5/
chromedriver下载地址:https://chromedriver.chromium.org/downloads
测试环境准备
1.PC端安装chrome,手机端安装谷歌浏览器(部分品牌应用商店可能会没有,可以安装应用宝,再到应用宝上安装google浏览器,或者PC端应用宝内下载。不太清楚这个有什么用途,我不按安装也是可以运行的。)
2. 打开webview调试模式
这里以微信为例,访问debugx5.qq.com开启调试模式。
或者在app中配置如下代码(在WebView类中调用静态方法setWebContentsDebuggingEnabled):

if (Build.VERSION.SDK_INT > =Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}

3.浏览器输入  chrome://inspect/devices#devices,可以看到当前webview页面,如果获取不到可以刷新下,或者调试模式未打开。
appium自动化测试之H5页面测试

文章图片
appium自动化测试之H5页面测试

文章图片

代码准备
desired_caps_a = {\'platformName\': "android", \'platFormVersion\': "10", \'deviceName\': "dd", \'udid\': "648d4f29", # \'automationName\': "UiAutomator1", \'noReset\': True, \'unicodeKeyboard\': True, \'resetKeyboard\': True, \'appPackage\': "com.tencent.mm", \'appActivity\': "com.tencent.mm.ui.LauncherUI", \'chromedriverExecutable\': r"C:\\Users\\v_yddchen\\Desktop\\chromedriver_win32 77.0\\chromedriver.exe", \'chromeOptions\': {\'androidProcess\': "com.tencent.mm:toolsmp"}# webview的进程名字,不然会报错

 
# 测试获取H5元素 my_logging.debug(device_a.contexts) device_a.find_element_by_android_uiautomator(\'new UiSelector().text("Cc")\').click() device_a.find_element_by_android_uiautomator(\'new UiSelector().textContains("警惕")\').click() WebDriverWait(device_a, 20).until(lambda x: x.find_element_by_android_uiautomator(\'new UiSelector().text("腾讯新闻")\')) my_logging.debug(device_a.contexts) device_a.switch_to.context("WEBVIEW_com.tencent.mm:toolsmp") time.sleep(3) my_logging.debug(device_a.current_context)

 
错误
运行上述代码报错了
unknown error: Failed to get PID for the following process: com.tencent.mm
appium自动化测试之H5页面测试

文章图片

 
 
解决方法:
1.有说是appiumd的chromedriver版本不对,虽然不太认同,但是我尝试了一下
设备:oppo reno android 9 webdriver 70.0.3538.110
PC端chrome版本81.0.4044.117      手机端chrome同样版本 81.0.4044.117   
appium webview的版本78.0.3904.62
  ...\\AppData\\Local\\Programs\\appium-desktop\\resources\\app\\node_modules\\appium\\node_modules\\appium-chromedriver\\chromedriver\\win的路径下,更换chromedriver的版本,或者初始化时
chromedriverExecutable指定chromedriver的路径
更换chromedriver版本 81、78、70都是一样的报错(有点怀疑源码有啥问题,github上搜到了一些蛛丝马迹,等我再翻翻)。
2.更换了一台设备 oppo findx2 Android 10  webview  77.0.3865.92
【appium自动化测试之H5页面测试】以上代码运行成功。
奇怪的是,不同手机输出的contexts是不同的。
oppo reno android 9 webdriver 70.0.3538.110
appium自动化测试之H5页面测试

文章图片

 
    oppo findx2 Android 10  webview 77.0.3865.92
appium自动化测试之H5页面测试

文章图片

 
  3.确定appium的chromedriver版本的决定性因素
  oppo findx2 Android 10  webview  77.0.3865.92
appium自动化测试之H5页面测试

文章图片

 
 
更换appium chromedriver的版本为73,低于系统webdriver和app的webview版本,运行提示出错
appium自动化测试之H5页面测试

文章图片

 
  如果chromedriver的版本为77,运行成功。
设置为81或者78,运行失败。明确报错chrome version问题是很好解决的。
appium自动化测试之H5页面测试

文章图片

 

    推荐阅读