首页 > 其他分享 >通过adb命令获取页面activity所有元素

通过adb命令获取页面activity所有元素

时间:2023-06-06 16:47:06浏览次数:36  
标签:elements String get attr adb activity put new 页面

    /**
     * 获取设备当前页面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;
    }

  部分程序执行有错误,后期需要调整

标签:elements,String,get,attr,adb,activity,put,new,页面
From: https://www.cnblogs.com/Philip-Lee/p/17460961.html

相关文章

  • WPF 中的 经典的ModelView 通知页面更新 UI
    viewmodel------------------------------------------------------------------------------usingHPControls.Helper;usingSystem;usingSystem.ComponentModel;usingSystem.Threading;usingXiaowei.Models;usingXiaowei.Services;usingXiaowei.Settings;nam......
  • MariaDB Galera Cluster 部署
    MariaDBGaleraCluster部署介绍MariaDB作为MySQL的一个分支,在开源项目中已经广泛使用,例如openstack,所以,为了保证服务的高可用性,同时提高系统的负载能力,集群部署是必不可少的。1、MariaDBGaleraCluster介绍GaleraCluster是由第三方公司Codership所研发的一套免费开源的集......
  • Playwright微软自动化框架-对浏览器的操作-页面前进/后退/刷新操作
    昨天使用微软自动化框架Playwright对浏览器进行全屏显示,多个浏览器调用等功能,今天我们来对浏览器进行前进/后退/刷新等操作。  直接上代码:  package org.example;import com.microsoft.playwright.*;publicclass BrowserFresh{  publicstaticvoid main(String[]......
  • CentOS下安装MariaDB
    yuanwen:https://www.cnblogs.com/michael999/p/16877964.html1.安装服务端安装MySQL软件,可以在windows和linux下安装,此处只提供centos下安装过程。MariaDb是最近推出一款企业级MYSQLServerPerconaServer和MariaDb是当前最为流行的两大企业级MYSQLSERVER解决方案sud......
  • 【HarmonyOS】一文教你如何在低代码项目中跳转H5页面
     【关键字】元服务、低代码、H5页面跳转、WebView【1、写在前面】今天我们来实现一个在低代码项目中通过按钮跳转到H5页面的功能,本项目是基于API6的JS工程,我们的实现思路是在页面B中通过Java加载WebView控件,在低码页面中为按钮绑定点击事件,事件中实现通过JS调用Java能力,OK,下面一起......
  • 【HarmonyOS】一文教你如何在低代码项目中跳转H5页面
    ​ 【关键字】元服务、低代码、H5页面跳转、WebView 【1、写在前面】今天我们来实现一个在低代码项目中通过按钮跳转到H5页面的功能,本项目是基于API6的JS工程,我们的实现思路是在页面B中通过Java加载WebView控件,在低码页面中为按钮绑定点击事件,事件中实现通过JS调用Java能力,O......
  • 【GiraKoo】adb.exe频繁崩溃,与Android设备连接不稳定
    【解决方案】adb.exe频繁崩溃,与Android设备连接不稳定在使用AndroidStudio时,发现adb.exe连接非常不稳定。通过EveryThing工具搜索关键字adb.exe。发现了大量的AppCrash_adb文件夹,adb.exe.xxx.dmp文件等情况。初步判断,应当是adb在运行时,程序频繁崩溃。遇到此类问题,尝试了以下几......
  • XtraDB与InnoDB的区别
    XtraDB是Percona开发的一种InnoDB存储引擎的衍生版本,它在InnoDB的基础上进行了优化和改进。下面是XtraDB和InnoDB之间的几个不同点:XtraDB对InnoDB的缓存管理进行了优化,增加了自适应哈希索引、在线实时扩展缓存等功能,可以更好地利用系统内存资源。XtraDB支持更多的线程并发,采用了更......
  • 关于荣耀X40打开USB调试之后仍然无法连接adb
    解决方案:1-打开开发者选项关于手机-多次点击版本号-提示您正处于开发者选项模式2-进入系统和更新-点击开发人员选项-开启USB调试备注:此时输出adbdevices可能无法连接,需要进一步进行设置3-选择USB配置,勾选RNDIS(USB以太网),会弹出......
  • 浏览器中跑定时器跑着跑着就慢了(离开当前页面,再次进来后时间会慢一点)
    最近再写一个考试系统,到点交卷,在手机上跑定时器是没问题的,在浏览器上跑的时候,一离开页面(指切换标签页),再次回来时间就会慢很多关于浏览器变慢的原因是,当离开当前页面时,cpu检测到你不在当前页面,为了节省资源,会放慢浏览器的时间。导致进入页面,时间变慢。有没有解决办法呢,当然是有的......