• appium Android下webview混合APP使用教程


    背景

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

    也可参考testerhome发表的文章链接:https://testerhome.com/topics/7866

    环境前置准备

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

    尝试解决方法:

    1、在windows host文件中增加:

    61.91.161.217  chrome-devtools-frontend.appspot.com
    61.91.161.217    chrometophone.appspot.com

    2、使用FQ软件,如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()
    复制代码
  • 相关阅读:
    You need to use a Theme.AppCompat theme
    Objective-C中nil与release的区别与用法
    objective-c中自己创建的对象为什么不能调用release
    NSString类的相关用法
    [Bug-IOS]
    中国有什么旅游社交网站吗?
    om.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException
    【剑指Offer学习】【面试题22:栈的压入、弹出序列】
    2-08. 用扑克牌计算24点(25) (ZJU_PAT 数学 枚举)
    在北京工作了两年,如今跳槽到了广州,社保公积金该怎样办理?
  • 原文地址:https://www.cnblogs.com/dreamhighqiu/p/10995986.html
Copyright © 2020-2023  润新知