zoukankan      html  css  js  c++  java
  • 微信小程序自动化

    由于腾讯系QQ、微信是基于腾讯自研X5内核,不是谷歌原生webview,所以调试会有些许差异(有很多app厂商也开始采用X5内核)
    微信小程序只能够支持手机,模拟器是不行的
    • step1:打开微信小程序webview开关(微信小程序页面的元素)
    聊天窗口输入如下URL:
    打开 :http://debugmm.qq.com/?forcex5=true
    关闭:http://debugmm.qq.com/?forcex5=false
    http://debugx5.qq.com
    • step2:UC开发者工具识别小程序的web元素信息
    • step3:确认微信小程序对应的进程名
    微信有很多的进程,我们要确定当前小程序是位于哪个进程中:
     
    // 支持X5内核应用自动化配置
    desiredCapabilities.setCapability("recreateChromeDriverSessions", true);
    // ChromeOptions使用来定制启动选项,因为在appium中切换context识别webview的时候,
    // 把com.tencent.mm:toolsmp的webview识别成com.tencent.mm的webview.
    // 所以为了避免这个问题,加上androidProcess: com.tencent.mm:toolsmp
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("androidProcess", "com.tencent.mm:toolsmp");
    desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
    // 初始化会默认将chrome浏览器打开,需要将Browser置为空
    desiredCapabilities.setBrowserName("");
    • step4:X5内核启动参数配置
    • step5:编写脚本
    注意:小程序X5内核(webview)版本和chromeDriver版本匹配的时候不能按照表格对应版本去匹配,要前后版本试一下
    V2.40 chromeDriver
    实例:从微信进入小程序,操作小程序
    //打开测试App
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("platformName", "Android");
    desiredCapabilities.setCapability("appPackage", "com.tencent.mm");
    desiredCapabilities.setCapability("appActivity", "com.tencent.mm.ui.LauncherUI");
    // 支持X5内核应用自动化配置
    desiredCapabilities.setCapability("recreateChromeDriverSessions", true);
    // ChromeOptions使用来定制启动选项,因为在appium中切换context识别webview的时候,
    // 把com.tencent.mm:toolsmp的webview识别成com.tencent.mm的webview.
    // 所以为了避免这个问题,加上androidProcess: com.tencent.mm:toolsmp
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("androidProcess", "com.tencent.mm:appbrand0");
    desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
    // 初始化会默认将chrome浏览器打开,需要将Browser置为空
    desiredCapabilities.setBrowserName("");
    //添加noReset 不清除数据 值取布尔值
    //默认不加的值为false 一定要注意:不加的话后果很严重,把微信的数据全部的清掉
    desiredCapabilities.setCapability("noReset", true);
    URL remoteUrl = new URL("http://localhost:4723/wd/hub");
    driver = new AndroidDriver(remoteUrl, desiredCapabilities);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    Thread.sleep(10000);
    //下拉==向下滑动
    swipeDown(600);
    //选择小程序
    driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text("apple2软件…")")).click();
    Thread.sleep(6000);
    //切换context
    System.out.println(driver.getContextHandles());
    //选择context的时候:context名字是包含之前查询到的小程序进程名
    driver.context("WEBVIEW_com.tencent.mm:appbrand0");
    //因为小程序开启之后会连带打开三个web窗口,所以我们需要切换到对应正确的窗口中去
    //每一个窗口会有对应的句柄(标识) 三个句柄
    Set<String> handles = driver.getWindowHandles();
    for(String handle:handles){
    if(driver.getTitle().equals("apple2购物商城")){
    break;
    }
    //切换句柄
    driver.switchTo().window(handle);
    }
    //定位小程序页面元素
    driver.findElement(By.xpath("//a[text()='商品(103)']")).click();
  • 相关阅读:
    操作系统基本原理
    String.StartsWith 方法
    桥接模式
    there is no default constructor available in ... | interface extends interface
    JAVA,获取手机屏幕大小
    JAVA,读写properties文件
    JAVA,执行cmd命令控制台输出内容乱码问题解决
    JAVA自动化,使用UIAutomator2加快Appium运行速度
    JAVA自动化,真机打开APP,弹出权限弹窗问题解决
    揭秘TDSQL-A分布式执行框架:解放OLAP关联分析查询性能瓶颈
  • 原文地址:https://www.cnblogs.com/zhiyu07/p/14187198.html
Copyright © 2011-2022 走看看