看了@seveniruby发布的使用 appium 进行微信小程序的自动化测试,最近一直在使用 macaca,于是开始在 macaca 上实践。

开始实践

一实践就发现 macaca 不支持自定义 androidProcess,于是提了 issue 等待 macaca 团队解决。

星期天晚上更新了 macaca-android1.1.16 支持自定义 androidProcess,终于可以愉快的进行微信 H5 相关的测试了。

关于微信小程序结构请参考使用 appium 进行微信小程序的自动化测试

微信小程序示例代码:

Java 代码,需要 macacaclient2.0,fastjson

static MacacaClient driver = new MacacaClient();

public static void setup() throws Exception {
    JSONObject porps = new JSONObject();
    porps.put("autoAcceptAlerts", true);
    porps.put("platformVersion", "5.1");
    porps.put("deviceName", "OPPO");
    porps.put("platformName", "android");
    porps.put("udid", "JB6HPJAMVGHIWOTG");
    porps.put("reuse", 3);
    porps.put("package", "com.tencent.mm");
    porps.put("activity", ".ui.LauncherUI");
    // 关键是加上这段
    porps.put("androidProcess", "com.tencent.mm:appbrand0");//被测小程序所在第几个-1
    JSONObject desiredCapabilities = new JSONObject();
    desiredCapabilities.put("desiredCapabilities", porps);
    driver.initDriver(desiredCapabilities);
}

public static void main(String[] args) throws Exception {
    setup();
    driver.sleep(2000);
    driver.elementByXPath("//*[@text='发现']").click();
    driver.elementByXPath("//*[@text='小程序']").click();
    driver.sleep(2000);
    driver.elementByXPath("//*[@text='美团外卖+']").click();
    Thread.sleep(5000);
    driver.context(driver.contexts().getString(2));//进入小程序后会有3个web context,其中第二个是真正的小程序webview
    Thread.sleep(5000);
    System.out.println(driver.elementByClassName("poi-list-header").getText());//输出美团外卖+小程序首页的“附近商家”
    Thread.sleep(2000);
    System.out.println(driver.currentContext());
    driver.quit();
}

输出结果:

微信公众号示例代码:

static MacacaClient driver = new MacacaClient();

public static void setup() throws Exception {
    JSONObject porps = new JSONObject();
    porps.put("autoAcceptAlerts", true);
    porps.put("platformVersion", "5.1");
    porps.put("deviceName", "OPPO");
    porps.put("platformName", "android");
    porps.put("udid", "JB6HPJAMVGHIWOTG");
    porps.put("reuse", 3);
    porps.put("package", "com.tencent.mm");
    porps.put("activity", ".ui.LauncherUI");
    // 关键是加上这段
    porps.put("androidProcess", "com.tencent.mm:tools");
    JSONObject desiredCapabilities = new JSONObject();
    desiredCapabilities.put("desiredCapabilities", porps);
    driver.initDriver(desiredCapabilities);
}

public static void main(String[] args) throws Exception {
    setup();
    driver.sleep(2000);
    driver.elementByXPath("//*[@text='中国航信']").click();
    driver.elementByXPath("//*[@text='超实用的春运赶飞机交通指南!']").click();
    Thread.sleep(5000);
    switchToWebView(driver);
    Thread.sleep(2000);
    System.out.println(driver.elementById("activity-name").getText());
    Thread.sleep(2000);
    System.out.println(driver.currentContext());
    driver.quit();
}

public static MacacaClient switchToWebView(MacacaClient driver) throws Exception {
    JSONArray contexts = driver.contexts();
    return driver.context(contexts.get(contexts.size() - 1).toString());
}


↙↙↙阅读原文可查看相关链接,并与作者交流