51Testing软件测试论坛

标题: APP H5 混合自动化使用说明 [基于 Appium+Python 系列] [打印本页]

作者: 巴黎的灯光下    时间: 2017-6-27 14:01
标题: APP H5 混合自动化使用说明 [基于 Appium+Python 系列]
背景前几天接到H5开发人员那边的业务开发需求单,说想将H5接入到自动化系列中,特此记录分享一下。
环境前置准备尝试解决方法
1、在windows host文件中增加:
61.91.161.217  chrome-devtools-frontend.appspot.com61.91.161.217    chrometophone.appspot.com2、使用翻墙软件,如Lantern蓝灯
环境检查App webview 调试模式检查与开启浏览效果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>
复制代码


作者: 草帽路飞UU    时间: 2017-6-27 14:13
Click Element Wait,这个是自己封装的吗?
作者: 巴黎的灯光下    时间: 2017-6-27 14:14
草帽路飞UU 发表于 2017-6-27 14:13
Click Element Wait,这个是自己封装的吗?

嗯,是的!




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2