4.微信小程序自动化
由于腾讯系QQ、微信是基于腾讯自研X5内核,不是谷歌原生webview,所以调试会有些许差异(有很多
app厂商也开始采用X5内核)
微信小程序只能够支持手机,模拟器是不行的
1:打开微信小程序webview调试开关(微信小程序页面的元素)
聊天窗口输入如下URL:
http://debugmm.qq.com/?forcex5=true
2:UC开发者工具识别小程序的web元素信息
3:确认微信小程序对应的进程名
微信有很多的进程,我们要确定当前小程序是位于哪个进程中:
4:X5内核启动参数配置
// 支持X5内核应用自动化配置
desiredCapabilities.setCapability("recreateChromeDriverSessions", true);
// ChromeOptions使用来定制启动选项,因为在appium中切换context识别webview的时候,
// 把com.tencent.mm:appbrand0的webview识别成com.tencent.mm的webview.
// 所以为了避免这个问题,加上androidProcess: com.tencent.mm:appbrand0
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidProcess", "com.tencent.mm:appbrand0");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
// 初始化会默认将chrome浏览器打开,需要将Browser置为空
desiredCapabilities.setBrowserName("");
5:编写脚本
注意:小程序X5内核(webview)版本和chromeDriver版本匹配的时候不能按照常规的chromeDriver版本
对照
V2.40 chromeDriver
手机投屏软件ApowerMirror
public class SmallProgramTest { static AndroidDriver driver; public static void main(String[] args) throws MalformedURLException, InterruptedException { //实例化配置对象 DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); //1、测试哪一台设备 desiredCapabilities.setCapability("deviceName","69DDU16520017623"); //2、指定测试平台Android or IOS desiredCapabilities.setCapability("platformName","Android"); //3、指定测试App(包名-App唯一身份标识) desiredCapabilities.setCapability("appPackage","com.tencent.mm"); //4、App启动配置(App启动入口) desiredCapabilities.setCapability("appActivity","com.tencent.mm.ui.LauncherUI"); //重点,特别注意!!!不清除掉微信的数据 noReset desiredCapabilities.setCapability("noReset",true); // 支持X5内核应用自动化配置 desiredCapabilities.setCapability("recreateChromeDriverSessions", true); // ChromeOptions使用来定制启动选项,因为在appium中切换context识别webview的时候, // 把com.tencent.mm:appbrand0的webview识别成com.tencent.mm的webview. // 所以为了避免这个问题,加上androidProcess: com.tencent.mm:appbrand0 ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("androidProcess", "com.tencent.mm:appbrand2"); desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options); // 初始化会默认将chrome浏览器打开,需要将Browser置为空 desiredCapabilities.setBrowserName(""); URL url = new URL("http://127.0.0.1:4723/wd/hub"); //把配置发送给到Appium服务器(Driver实例化) driver = new AndroidDriver(url,desiredCapabilities); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //等待主页加载完成 Thread.sleep(12000); swipeDown(2000); driver.findElement(By.xpath("//*[contains(@text,'黑马视频')]")).click(); //等待小程序加载完成 Thread.sleep(8000); //切换context //Set<String> allContexts = driver.getContextHandles(); //微信webview和chromeDriver版本 //要切换的context名字是由WEBVIEW_小程序进程名 driver.context("WEBVIEW_com.tencent.mm:appbrand2"); //进入到web页面定位元素,操作元素 //大坑:微信小程序进程会对应有三个web窗口,我们需要切换到正确的那个 switchWindow("黑马视频库"); driver.quit(); } /** * 公共窗口切换方法 * @param title 标题 */ public static void switchWindow(String title){ Set<String> allHandles = driver.getWindowHandles(); for (String handle:allHandles){ //判断是不是对应的句柄(根据什么来判断) if(driver.getTitle().equals(title)){ //符合,跳出循环 break; }else { //切换窗口 driver.switchTo().window(handle); } } } /** * 向下滑动的通用封装 * @param swipeTime 滑动时间 */ public static void swipeDown(long swipeTime){ //约定滑动起始点与终止点的位置 //滑动起始点:1/2宽度 1/4高度 滑动终止点:1/2宽度 3/4高度 int screenWidth = driver.manage().window().getSize().getWidth(); int screenHeight = driver.manage().window().getSize().getHeight(); TouchAction touchAction = new TouchAction(driver); //起始点(424,632) PointOption pointOptionA = PointOption.point(screenWidth/2,screenHeight/4); //终止点(424,1000) PointOption pointOptionB = PointOption.point(screenWidth/2,screenHeight*3/4); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(swipeTime)); touchAction.press(pointOptionA).waitAction(waitOptions).moveTo(pointOptionB).release().perform(); } }