1. STest软件测试社区首页
  2. WEB测试

Macaca微信小程序实践

本帖已被设为精华帖!,

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

输出结果:

Macaca微信小程序实践

微信公众号示例代码:

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

原创文章,作者:众测,如若转载,请注明出处:https://www.stest.com

发表评论

邮箱地址不会被公开。 必填项已用*标注

QR code