zoukankan      html  css  js  c++  java
  • Appium常用操作之X5内核应用(如微信小程序)上下文切换

    1、打开微信在任意窗口输入一下内容

    针对微信版本在7.0以下,可以只需要在任意聊天窗口输入debugx5.qq.com即可打开。

    针对微信版本在7.0+,微信有对H5开关做了调整,需要在聊天窗口输入如下:

    2、手机通过usb连接到电脑,打开USB调试模式,通过adb devices命令检测到设备

    3、进入到小程序中

    这点相信大家都知道如何进入小程序。

    4、使用uc - devtools检测webview页面

    显示的webview版本是57.xxx,这里就是微信X5内核的版本,不是android System webview版本,同样也可以点击inspect查看页面元素

    可以看到当前就是一个html页面,我们可以采取常规web元素定位方式来定位元素

    注意事项:

      (1)页面空白加载不出来,这是因为Google的inspect工具需要访问到墙外的网站,所以需要有FQ工具或者VPN方式;

      (2)微信在新版本中对小程序调试入口加上了限制:在微信主窗口下来打开小程序,在chrome中通过inspect工具是检测不到小程序对应的url入口的;

        解决方案:

        在微信->发现->搜一搜搜索小程序,即可发现在inspect工具中可以将对应url显示出来。

      (3)还有一点需要注意,如果点击右上角关闭了小程序之后,一定要记得从后台清理下对应的小程序进程(关闭之后小程序还在后台运行),再次点击重启小程序。

    5、默认appium-desktop安装之后里面自带的chromedriver不是2.26的,需要手动去官网下载对应版本的chromedriver:

      http://chromedriver.storage.googleapis.com/index.html

      将其放到appium的chromedriver对应目录中,我的是:

      C:UsersAdministratorAppDataLocalappium-desktopapp-1.5.0 esourcesapp ode_modulesappium ode_modulesappium-chromedriver

    6、微信/QQ有很多进程,我们要确定当前web页面是位于哪个进程中,使用adb命令:

    • adb shell dumpsys activity top | findstr ACTIVITY
    • adb shell ps 进程号

    确定我们当前微信的页面运行在com.tencent.mm:toolsmp中

    7、在desiredCapabilities中指定:

    desired_caps={}
    // 支持X5内核应用自动化配置
    desired_caps["recreateChromeDriverSessions"]= True

    desired_caps["platformName"]="Android" desired_caps["platformversion"]="5.1" desired_caps["deviceName"]="Android Emulator" desired_caps["appPackage"]="com.tencent.mm" desired_caps["appActivity"]="com.tencent.mm.ui.LauncherUI" desired_caps["noReset"]=True // ChromeOptions使用来定制启动选项,因为在appium中切换context识别webview的时候, // 把com.tencent.mm:toolsmp的webview识别成com.tencent.mm的webview. // 所以为了避免这个问题,加上androidProcess: com.tencent.mm:toolsmp
    desired_caps["chromeOptions"] = {"androidProcess":"com.tencent.mm:tools"}
    // 初始化会默认将chrome浏览器打开,需要将Browser置为空 
    desired_caps["browserName"]=""
    driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)

      

    8、根据chrome的inspect工具得到web元素的信息

    测试流程如下:

      选择测试小程序->点击老师->滑动老师列表找到“歪歪”

      "老师"对应的xpath://*[@id="js-tab-bar"]/li[3]

      "歪歪"对应的xpath://em[text()='歪歪']

    测试代码对应如下:

    time.sleep(5)
    #点击微信中底部“发现”按钮
    driver.find_element_by_android_uiautomator('new UiSelector().text("发现")').click()
    #点击发现里面搜一搜
    driver.find_element_by_android_uiautomator("new UiSelector().text("搜一搜")").click()
     
    time.sleep(5)
     
    #点击搜索框
    driver.find_element_by_id("com.tencent.mm:id/jd").click()
    driver.find_element_by_id("com.tencent.mm:id/jd").sendKeys("柠檬班软件测试")
     
    time.sleep(5)
     
    // 点击搜索结果中的柠檬班软件测试(采用adb命令坐标点击的方式)
    os.system("adb shell input tap 281 205")
     
    time.sleep(5)
     
    // 点击柠檬班软件测试小程序
    os.system("adb shell input tap 364 470")
     
    // 等待小程序加载完成
    time.sleep(10)
     
    // 获取到所有的contexts
    print("所有的contexts:" + driver.contexts);
     
    // 切换到小程序webview对应的context中
    driver.switch_to.context("WEBVIEW_com.tencent.mm:toolsmp")
    time.sleep(5)
     
    // 获取到所有的handles
    windowHandles = driver.window_handles
    print("所有的windowsHandles" + windowHandles)
     
    // 遍历所有的handles,找到当前页面所在的handle:如果pageSource有包含你想要的元素,就是所要找的handle
    // 小程序的页面来回切换也需要:遍历所有的handles,切换到元素所在的handle
    for handle in windowHandles:
        print("切换到对应的windowHandle:" + windowHandle)
        driver.switch_to.window(handle)
        time.sleep(3)
        if driver.page_source.find("柠檬班")!=-1:
            break
        
    
    // 点击老师
    WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.xpath("//*[@id="js-tab-bar"]/li[3]")))).click()
    WebDriverWait(driver,20).until(EC.presence_of_element_located((By.xpath("//em[text()='歪歪']"))))   
    // 通过js滚动到指定的元素 (这个元素已经在文档中间已经存在,但是还是不可见的)
    ele=driver.find_element_by_xpath("//em[text()='歪歪']")
    time.sleep(3)
    
    // 滑动到上面定位到的元素的位置
    driver.execute_script("arguments[0].scrollIntoViewIfNeeded(true);", ele);
     
    time.sleep(3)
    

    到目前为止,微信小程序自动化代码实现就完成了,后续按照自己的需求编写代码完成。

    参考文章:https://blog.csdn.net/qq_38741986/article/details/99702537

  • 相关阅读:
    intel instruction 指令速查
    WinDbg双机调试配置
    MSDN上关于WinDbg的手册
    build temu error about SDL
    taintCheck的实现
    Vim使用taglist功能
    Windows编写driver
    cabal替代脚本
    怎样理解Functor与Monad
    haskell基本语法
  • 原文地址:https://www.cnblogs.com/123blog/p/12624007.html
Copyright © 2011-2022 走看看