微信小程序自动化测试实践小结

参考《微信小程序自动化测试实践》的小结:

  1. 基于腾讯自研X5内核
  2. 配置:打开微信,在任意窗口输入:debugx5.qq.com>勾选是否打开TBS内核Inspector调试功能
  3. 代码
// 支持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("");

// 休眠一下
Thread.sleep(5000);
// 找到微信的发现并点击
androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"发现\")").click();
// 点击发现里面搜一搜
androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"搜一搜\")").click();

Thread.sleep(2000);

// 点击搜索框
androidDriver.findElement(By.id("com.tencent.mm:id/jd")).click();
        androidDriver.findElement(By.id("com.tencent.mm:id/jd")).sendKeys("柠檬班软件测试");

Thread.sleep(2000);

// 点击搜索结果中的柠檬班软件测试(采用adb命令坐标点击的方式)
execAdb("adb shell input tap 300 200");

Thread.sleep(4000);

// 点击柠檬班软件测试小程序
execAdb("adb shell input tap 300 500");

// 等待小程序加载完成
Thread.sleep(10000);

// 获取到所有的contexts
System.out.println("所有的contexts:" + androidDriver.getContextHandles());

// 切换到小程序webview对应的context中
androidDriver.context("WEBVIEW_com.tencent.mm:toolsmp");
Thread.sleep(2000);

// 获取到所有的handles
Set<String> windowHandles = androidDriver.getWindowHandles();
System.out.println("所有的windowsHandles" + windowHandles);

// 遍历所有的handles,找到当前页面所在的handle:如果pageSource有包含你想要的元素,就是所要找的handle
// 小程序的页面来回切换也需要:遍历所有的handles,切换到元素所在的handle
for (String windowHandle : windowHandles) {
    System.out.println("切换到对应的windowHandle:" + windowHandle);
    androidDriver.switchTo().window(windowHandle);
    Thread.sleep(2000);
    if (androidDriver.getPageSource().contains("柠檬班")) {
        break;
    }
}
// 点击老师
androidDriver.findElement(By.xpath("//*[@id=\"js-tab-bar\"]/li[3]")).click();
        
// 通过js滚动到指定的元素 (这个元素已经在文档中间已经存在,但是还是不可见的)
WebElement ele=androidDriver.findElement(By.xpath("//em[text()='歪歪']"));
Thread.sleep(2000);
// 将Driver实例化为js对象
JavascriptExecutor jExecutor=(JavascriptExecutor)androidDriver;
// 滑动到上面定位到的元素的位置
jExecutor.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", ele);

Thread.sleep(2000);

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值