51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2363|回复: 2
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-6-27 14:01:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
背景前几天接到H5开发人员那边的业务开发需求单,说想将H5接入到自动化系列中,特此记录分享一下。
环境前置准备
  • 手机与电脑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.com61.91.161.217    chrometophone.appspot.com2、使用翻墙软件,如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
  1. __author__ = 'mikezhou'
  2. #coding=utf-8

  3. #appium 微信h5自动化示例
  4. from appium import webdriver
  5. import time

  6. packageName='com.tencent.mm'
  7. appActivity='.ui.LauncherUI'

  8. desired_caps = {}
  9. desired_caps['platformName'] = 'Android'
  10. desired_caps['platformVersion'] = '5.1.1'
  11. desired_caps['deviceName'] = 'K31GLMA660800338'
  12. desired_caps['appPackage'] = packageName
  13. desired_caps['appActivity'] = appActivity
  14. desired_caps['fullReset'] = 'false'
  15. desired_caps['unicodeKeyboard'] = 'True'
  16. desired_caps['resetKeyboard'] = 'True'
  17. desired_caps['fastReset'] = 'false'

  18. desired_caps['chromeOptions']={'androidProcess': 'com.tencent.mm:tools'}   #驱动H5自动化关键之一

  19. driver = webdriver.Remote('http://127.0.1.1:4723/wd/hub', desired_caps)

  20. driver.implicitly_wait(30)
  21. driver.find_element_by_name('我').click()
  22. print driver.contexts
  23. driver.find_element_by_name('相册').click()
  24. driver.find_element_by_xpath("//*[contains(@text,'正在繁星直播')]").click()
  25. print driver.current_context
  26. driver.find_element_by_xpath("//*[contains(@text,'正在繁星直播')]").click()
  27. print driver.current_context
  28. driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
  29. print driver.current_context
  30. print driver.page_source
  31. driver.find_element_by_xpath('//*[@id="btnRecommend"]/div[1]').click()
  32. driver.switch_to_default_content()
  33. time.sleep(2)
  34. driver.quit()
  35. Python+AppiumLibrary+RobotFramework
  36. 注:RF关键字与用例部分,只提供关键代码,一些前置需要导入的library,自行添加:
  37. App启动关键字
  38. Open MobileFx Android App_H5
  39.     [Arguments]    ${remote_url}    ${deviceName}    ${appActivity}    ${appPackage}    ${platformVersion}    ${Process}
  40.     ...    ${app}=${Empty}
  41.     [Documentation]    *启动手机繁星app_H5[Android]*
  42.     ...
  43.     ...    入参顺序:
  44.     ...
  45.     ...    Arguments: 远程服务地址|设备名称|待测应用appActivity| 待测应用package包名|平台版本号|webveiw进程名
  46.     ...
  47.     ...    Examples:
  48.     ...
  49.     ...    | Open MobileFx Android App H5 | http://localhost:4723/wd/hub | Android Emulator | .ui.LauncherUI | 'com.tencent.mm | 4.4.2 | com.tencent.mm:tools |


  50.     ${androidProcess}=    Create Dictionary    androidProcess=${Process}
  51.     Open Application    ${remote_url}    alias=fanxingappForAndroid    platformName=Android    deviceName=${deviceName}    automationName=appium    appActivity=${appActivity}
  52.     ...    appPackage=${appPackage}    platformVersion=${platformVersion}    unicodeKeyboard=True    resetKeyboard=True    app=${app}    chromeOptions=${androidProcess}
  53. 套件用例区

  54. *** Settings ***
  55. Suite Setup       启动app
  56. Suite Teardown    Close All Applications
  57. Library           AppiumLibrary

  58. *** Variables ***
  59. ${appActivity}    .ui.LauncherUI
  60. ${appPackage}     com.tencent.mm
  61. ${deviceName}     ${get_deviceName}
  62. ${platformVersion}    ${get_platform_version}
  63. ${remote_url}     ${android_remote_url}
  64. ${androidProcess}    com.tencent.mm:tools

  65. *** Test Cases ***
复制代码
微信分享验证
  1. [Documentation]    检查首页搜索
  2.     [Tags]    mikezhou    main    online
  3.     [Setup]
  4.     [Timeout]
  5.     Click Element Wait    name=我
  6.     ${contexts}    get_contexts
  7.     ${current_context}    get_current_context
  8.     log    ${contexts}
  9.     log    ${current_context}
  10.     Click Element Wait    name=相册
  11.     ${current_context}    get_current_context
  12.     log    ${current_context}
  13.     Click Element Wait    xpath=//*[contains(@text,'助力鹿晗')]
  14.     ${current_context}    get_current_context
  15.     log    ${current_context}
  16.     Click Element Wait    xpath=//*[contains(@text,'助力鹿晗')]
  17.     ${current_context}    get_current_context
  18.     log    ${current_context}
  19.     Switch To Context    WEBVIEW_com.tencent.mm:tools
  20.     ${current_context}    get_current_context
  21.     log    ${current_context}
  22.     ${page}    Log Source
  23.     log    ${page}
  24.     Page Should Contain Text    鹿晗    timeout=15
  25.     Page Should Contain Text    概念诠释冒险精神
  26.     Click Element Wait    xpath=/html/body/div[1]/div
  27.     sleep    3
  28.     [Teardown]
  29. *** Keywords ***
  30. 启动app
  31.     Open MobileFx Android App_H5    ${remote_url}    ${deviceName}    ${appActivity}    ${appPackage}    ${platformVersion}    ${androidProcess}<span style="background-color: rgb(255, 255, 255); font-size: 14px; font-weight: normal;"></span>
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2017-6-27 14:13:50 | 只看该作者
Click Element Wait,这个是自己封装的吗?
回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2017-6-27 14:14:35 | 只看该作者
草帽路飞UU 发表于 2017-6-27 14:13
Click Element Wait,这个是自己封装的吗?

嗯,是的!
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-21 04:36 , Processed in 0.064076 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表