Macaca Macaca 微信公众号、小程序实践

adfghzhang · 2017年01月17日 · 最后由 zz_hui 回复于 2017年11月24日 · 2471 次阅读
本帖已被设为精华帖!

看了@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());
}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 8 条回复 时间 点赞

运行了脚本,获取的元素无法点击啊,提示 An unknown server-side error occurred while processing the command. 请问有遇到么

UIAutomatorWD http server ready
proxy.js:55:14 [master] pid:63976 Proxy: /wd/hub/session/:sessionId/accept_alert:POST to http://127.0.0.1:9007/wd/hub/session/:sessionId/accept_alert:POST with body: {}
proxy.js:85:20 [master] pid:63976 Got response with status 200: {"status":0,"sessionId":":sessionId"}
restart UIAutomatorWD server
uiautomator-client.js:61:14 [master] pid:63976 INSTRUMENTATION_RESULT: shortMsg=Process crashed.
uiautomator-client.js:61:14 [master] pid:63976
INSTRUMENTATION_CODE: 0

uiautomator-client.js:61:14 [master] pid:63976 INSTRUMENTATION_STATUS: numtests=1
uiautomator-client.js:61:14 [master] pid:63976
INSTRUMENTATION_STATUS: stream=
com.macaca.android.testing.UIAutomatorWD:
INSTRUMENTATION_STATUS: id=AndroidJUnitRunner

uiautomator-client.js:61:14 [master] pid:63976 INSTRUMENTATION_STATUS: test=MacacaTestRunner
INSTRUMENTATION_STATUS: class=com.macaca.android.testing.UIAutomatorWD
uiautomator-client.js:61:14 [master] pid:63976
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1

uiautomator-client.js:61:14 [master] pid:63976 INSTRUMENTATION_STATUS: stream=
uiautomator-client.js:61:14 [master] pid:63976
UIAutomatorWD->http://localhost:9007<-UIAutomatorWD
INSTRUMENTATION_STATUS_CODE: 0

UIAutomatorWD http server ready
proxy.js:55:14 [master] pid:63976 Proxy: /wd/hub/session/329e1239-a666-4340-94a7-6cccc1d9a645/element/1/click:POST to http://127.0.0.1:9007/wd/hub/session/:sessionId/element/1/click:POST with body: {}
proxy.js:85:20 [master] pid:63976 Got response with status 200: {"status":13,"value":"An unknown server-side error occurred while processing the command.","sessionId":"329e1239-a666-4340-94a7-6cccc1d9a645"}
session.js:109:14 [master] pid:63976 Send HTTP Respone to Client[2017-11-24 10:29:51]: {"status":13,"value":"\"An unknown server-side error occurred while processing the command.\"","sessionId":"329e1239-a666-4340-94a7-6cccc1d9a645"}

达峰的夏天 macaca 控制微信 webview 失败 中提及了此贴 03月27日 10:03
刘小仙 回复

把详细的日志和代码贴上来看看

切换 context 时,提示 chrome not reachable,请问@adfghzhang 遇到过吗?如何解决呢~

#7 楼 @utopia 太仔细了,我做这个 demo 的时候没有尝试进入其他 web 页面,所以只得到了这个结果。果然都是做测试的,情况考虑很周全。

@adfghzhang
这个 不准确:
driver.context(driver.contexts().getString(2));//进入小程序后会有 3 个 web context,其中第二个是真正的小程序 webview

首次进入返回四个上下文,类似下面的

["NATIVE_APP","CDwindow-7ede937a-9a55-4371-b53b-a2ace8f9d27b","CDwindow-a111b072-844d-4573-b49b-eadfad61e68d","CDwindow-24736235-ed5b-4bf4-b889-9485002091b0"]}

我把每个上下文对应的 source 保存到文本发现,发现重启微信和不重启微信情况,小程序 webview 的顺序是不一样的,重启微信是第 3 个,上面是对的
进入小程序之后推出,进入微信其他 page,再进入小程序的时候,小程序的 webview 是 contexts 第 2 个,上面要写 1 才行。

莫离 macaca 打开微信小程序 (python 版本) 中提及了此贴 01月19日 13:50

#3 楼 @utopia 是的就用的 devtool

@adfghzhang 请问如何定位小程序 webview 里面控件的

chromedevtool inspect 出的能直接用么

继 Appium 之后,macaca 也加入了。

恒温 将本帖设为了精华贴 01月17日 11:52
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册