/**
* 获取设备当前页面activity控件元素信息
* @param iDevice 安卓设备信息
* @return
*/
private JSONArray getDevicePageResource(IDevice iDevice){
long startTime = System.currentTimeMillis();
String cmd = "adb -s" + iDevice.getSerialNumber() + " shell uiautomator dump --compressed /data/local/tmp/uidump.xml && adb -s " + iDevice.getSerialNumber() + " pull /data/local/tmp/uidump.xml";
System.out.println("设备id是" + iDevice.getSerialNumber());
// String cmd = "adb -s 172.25.78.12:49110 shell uiautomator dump --compressed /data/local/tmp/uidump.xml && adb -s 172.25.78.12:49110 pull /data/local/tmp/uidump.xml && type uidump.xml";
StringBuilder sb = new StringBuilder();
try {
Process p = Runtime.getRuntime().exec(cmd);
//取得命令结果的输出流
InputStream in = p.getInputStream();
//用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(in);
//用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
//直到读完为止
while ((line = br.readLine()) != null) {
// System.out.println("getViewElementsActivity--------------->" + line);
sb.append(line);
}
//killed
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = (Document) builder.parse(new InputSource(new StringReader(String.valueOf(sb))));
JSONArray elementList = new JSONArray();
Document doc = Jsoup.parse(String.valueOf(sb));
String xpath = "/hierarchy";
elementList.addAll(getChildren(document.body().children().get(0).children(), xpath));
xpathId = 1;
logger.info("使用adb的方式获取资源总耗时 " + (System.currentTimeMillis() + startTime) + " ms");
logger.error("最终获取到的资源为:" + sb.toString());
return elementList;
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
return null;
}
private int xpathId = 1;
/**
* @param xpath 父级节点xpath
* @return com.alibaba.fastjson.JSONArray
* @author ZhouYiXun
* @des 获取子节点信息
* @date 2021/8/16 23:36
*/
public JSONArray getChildren(org.jsoup.select.Elements elements, String xpath) {
JSONArray elementList = new JSONArray();
for (int i = 0; i < elements.size(); i++) {
JSONObject ele = new JSONObject();
int tagCount = 0;
int siblingIndex = 0;
String indexXpath;
for (int j = 0; j < elements.size(); j++) {
if (elements.get(j).attr("class").equals(elements.get(i).attr("class"))) {
tagCount++;
}
if (i == j) {
siblingIndex = tagCount;
}
}
if (tagCount == 1) {
indexXpath = xpath + "/" + elements.get(i).attr("class");
} else {
indexXpath = xpath + "/" + elements.get(i).attr("class") + "[" + siblingIndex + "]";
}
ele.put("id", xpathId);
xpathId++;
ele.put("label", "<" + elements.get(i).attr("class") + ">");
JSONObject detail = new JSONObject();
detail.put("xpath", indexXpath);
for (Attribute attr : elements.get(i).attributes()) {
if (attr.getKey().equals("bounds")) {
String bounds = attr.getValue().replace("][", ":");
String pointStart = bounds.substring(1, bounds.indexOf(":"));
String pointEnd = bounds.substring(bounds.indexOf(":") + 1, bounds.indexOf("]"));
detail.put("bStart", pointStart);
detail.put("bEnd", pointEnd);
}
detail.put(attr.getKey(), attr.getValue());
}
ele.put("detail", detail);
if (elements.get(i).children().size() > 0) {
ele.put("children", getChildren(elements.get(i).children(), indexXpath));
}
elementList.add(ele);
}
return elementList;
}
部分程序执行有错误,后期需要调整