Appium APP H5 混合自动化使用说明 [基于 Appium+Python 系列]

狂师 · March 09, 2017 · Last by kasijia replied at July 31, 2019 · 6690 hits
本帖已被设为精华帖!

背景

前几天接到 H5 开发人员那边的业务开发需求单,说想将 H5 接入到自动化系列中,特此记录分享一下。

环境前置准备

  • 手机与电脑 USB 连接,开启 USB 调试模式,通过 adb devices 可查看到此设备。
  • 电脑端、移动端安装 chrome 浏览器。(尽量保证移动端 chrome 版本低于电脑端)
  • App webview 开启 debug 模式
  • 在电脑端 Chrome 浏览器地址栏输入chrome://inspect/#devices,进入调试模式:
    • 此时页面显示了手机型号、驱动名称、APP 要调试的 WebView 名称
    • 点击 inspect,若成功加载与 APP 端相同界面的调试页面,则配置成功
    • 若获取不到 WebView 或者调试页面预览框显示空白,则需要进行破解–安装软件(由于默认的 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}

结束语

感慨最近事事不利,感昌近十几天了都未好转。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 23 条回复 时间 点赞

来给你点个赞

Click Element Wait,这个是自己封装的吗?

狂师 #3 · March 17, 2017 Author

#2 楼 @whoiszjy 是的

—— 来自 TesterHome 官方 安卓客户端

${contexts} get_contexts
${current_context} get_current_context
log ${contexts}
sleep 2
Switch To Context WEBVIEW_gc.meidui
sleep 2
click element xpath=//*[@id='addcart']

我这个切换到 H5 页面进行点击,一直报错,能指导下那里有问题吗?

狂师 回复

${contexts} get_contexts
${current_context} get_current_context
log ${contexts}
sleep 2
Switch To Context WEBVIEW_gc.meidui
sleep 2
click element xpath=//*[@id='addcart']

我这个切换到 H5 页面进行点击,一直报错,能指导下那里有问题吗?

思寒_seveniruby 将本帖设为了精华贴 19 Mar 19:48

记得更新打赏二维码

狂师 #8 · March 20, 2017 Author

已更新

狂师 #9 · March 20, 2017 Author
红发 回复

看你的报错信息,是说没有匹配到对应元素,你切到 webview 视图后,再打印一下当前视图 contents,看是否有切换成功,另外确认清楚,你的定位方式是否书写正确。

赞~

保重身体呀。

赞~ 不过我这边 inspect 之后, 显示的内容老是一闪一闪的,有碰到过没?

@mikezhou 你好 ! 请问一下 com.tencent.mm:tools 这个进程名是怎么获取的 ? 我自己的 app 这个进程名应该怎么查找呢?

${remote_url} ${android_remote_url}中的 android_remote_url 是什么

@mikezhou hi,我想问下,我按照你 Python+AppiumLibrary+RobotFramework 的方法,最先是我的 chrome 和 chromedriver 版本不一致导致切换 context 有问题,现在我已经把手机的 chrome 和 chromedriver 版本弄一致。
get context 得到 ["WEBVIEW_com.tencent.mm:appbrand0","WEBVIEW_com.tencent.mm:tools","WEBVIEW_com.tencent.mm:support","WEBVIEW_com.tencent.mm:appbrand1","WEBVIEW_com.android.quicksearchbox"]
日志打印的 chromeOptions
{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"ZPRWJREMM4NBL7"}}}
最后切换的时候总是报错 Device ZPRWJREMM4NBL7 is not online\n
info: JSONWP Proxy: Got response with status 200: {"sessionId":"bd50308685a46e4c369e386f881e9ab0","status":13,"value":{"message":"unknown error: Device ZPRWJREMM4NBL7 is not online\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3...
请问有没有什么好的解决方法,一直卡在着!

[Chromedriver] Chromedriver exited unexpectedly with code null, signal SIGTERM
[Chromedriver] Changed state to 'stopped'
[Chromedriver] Error: session not created exception: please close '' and try again
这个问题一直卡着,找了很多文章,还不知道怎么解决

61.91.161.217 

这个 ip 直接 ping 不通。。

向大神膜拜😄

炽大哥 回复

你可以用 chromedriver2.0 的试试放在 appium 里替换下,有些人就解决这个问题,但我没解决,报错你可以参考下

info: JSONWP Proxy: Got response with status 200: {"sessionId":"14c1e22e74ff1d7bd20c2c1567d06ad1","status":13,"value":{"message":"unknown error: unable to discover open pages\n (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081...
info: Chromedriver: Changed state to 'stopped'
error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM
warn: Chromedriver for context WEBVIEW_com.tencent.mm:tools stopped unexpectedly
warn: Chromedriver quit unexpectedly, but it wasn't the active context, ignoring
error: Chromedriver: Error: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64))
at JWProxy.command$ (lib/proxy.js:133:15)
at tryCatch (D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as next
at GeneratorFunctionPrototype.invoke (D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at bound (domain.js:284:14)
at GeneratorFunctionPrototype.runBound (domain.js:297:12)
at run (D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:89:39)
at D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js💯28
at flush (D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\$.microtask.js:17:13)
at process._tickDomainCallback (node.js:381:11)
{ [Error: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64))]
status: 13,
value: { message: 'unknown error: unable to discover open pages\n (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64)' },
httpCode: 200 }
info: <-- POST /wd/hub/session/e90cc0ac-d704-4d57-8467-d9f09b73b128/context - - ms - -
undefined

开启方式
在 app 中配置如下代码(在 WebView 类中调用静态方法 setWebContentsDebuggingEnabled):

if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) {

WebView.setWebContentsDebuggingEnabled(true);
}
注:此步骤,一般需要 App 前端开发人员协助增加

请问微信小程序的测试,这段代码是加在小程序前端代码里么?具体加在哪能指点下么,开发说找不到 WebView 类

请问 webView 开关的问题怎么解决的呢,我开了 debugx5.qq.com 的调试功能,getContextHandles 时只获取到 com.tencent.mm 和 com.tencent.mm:tools 进程,没有 appbrand0。楼主给的配置,小程序的开发说找不到 webView 类

我切换到 h5 页面,print driver.current_context 就报错,appium 版本:1.13,chromedriver 版本:2.28,android system webview 55.0.2883
[Chromedriver] Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. Original error: unknown error: unable to discover open pages
Chromedriver
[Chromedriver] at Object.wrappedLogger.errorAndThrow (C:\Users\Chenyang Yue\AppData\Roaming\npm\node_modules\appium\node_modules\appium-support\lib\logging.js:78:13)
[Chromedriver] at errorAndThrow (C:\Users\Chenyang Yue\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:493:13)

这个是在手机浏览器中进行 H5 的自动化测试吗,如果是 H5 apk 呢 可以用吗

labixiaoxi 回复

请问解决了吗?我也遇到同样的问题,Chrome 和 Chromedriver 版本是匹配的

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up