首页 > 代码库 > [uiautomator篇][11]下载dump,解析xml的方法
[uiautomator篇][11]下载dump,解析xml的方法
- 1 project/pad(你的项目)/build-gradle
添加:红色部分 dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) testCompile ‘junit:junit:4.12‘ androidTestCompile ‘com.android.support:support-annotations:24.0.0‘ androidTestCompile ‘com.android.support.test:runner:0.4.1‘ androidTestCompile ‘com.android.support.test:rules:0.4.1‘ compile ‘com.android.support:appcompat-v7:24.0.0‘ androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.1‘ compile ‘dom4j:dom4j:20040902.021138‘ }
- 2 sync project with Gradle Files (在工具栏倒数第四个,像眼睛和wifi)
同样是为了解决有些时候采用uiautomater不能点击某个控件的问题,新增布局文件解析接口。
主要作用:代码中无法查找到控件时,我们使用dump布局文件,此段代码主要解析dump文件,可根据任意UiObeject的任意属性,获取它其余的属性。
例如布局文件下有text为Play music的控件,想获取其位置,具体效果如下:
import org.dom4j.Element; /** * 数据实体类 * * @author *** */ public class XMLBean { public final Element element; public final int index; public final String text; public final String resourceId; public final String classText; public final String packageText; public final String contentDesc; public final boolean checkable; public final boolean checked; public final boolean clickable; public final boolean enable; public final boolean focusable; public final boolean focused; public final boolean scrollable; public final boolean long_clickable; public final boolean password; public final boolean selected; public final String bounds; public XMLBean(Element element) { this.element = element; index = Integer.parseInt(element.attributeValue("index")); text = element.attributeValue("text"); resourceId = element.attributeValue("resource-id"); classText = element.attributeValue("class"); packageText = element.attributeValue("package"); contentDesc = element.attributeValue("content-desc"); checkable = Boolean.parseBoolean(element.attributeValue("checkable")); checked = Boolean.parseBoolean(element.attributeValue("checked")); clickable = Boolean.parseBoolean(element.attributeValue("clickable")); enable = Boolean.parseBoolean(element.attributeValue("enabled")); focusable = Boolean.parseBoolean(element.attributeValue("focusable")); focused = Boolean.parseBoolean(element.attributeValue("focused")); scrollable = Boolean.parseBoolean(element.attributeValue("scrollable")); long_clickable = Boolean.parseBoolean(element.attributeValue("long-clickable")); password = Boolean.parseBoolean(element.attributeValue("password")); selected = Boolean.parseBoolean(element.attributeValue("selected")); bounds = element.attributeValue("bounds"); } @Override public String toString() { return "XMLBean [index=" + index + ", text=" + text + ", resourceId=" + resourceId + ", classText=" + classText + ", packageText=" + packageText + ", contentDesc=" + contentDesc + ", checkable=" + checkable + ", checked=" + checked + ", clickable=" + clickable + ", enable=" + enable + ", focusable=" + focusable + ", focused=" + focused + ", scrollable=" + scrollable + ", long_clickable=" + long_clickable + ", password=" + password + ", selected=" + selected + ", bounds=" + bounds + "]"; } } package com.softwinner.app; import android.content.Context; import android.content.Intent; import android.os.RemoteException; import android.support.test.InstrumentationRegistry; import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.Until; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.junit.Before; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertThat; /** * Created by *** on 2017/7/21. */ public class TestAA { private static List<XMLBean> list; private static final String PATH = "/sdcard/window_dump.xml"; private final static int LAUNCH_TIMEOUT = 5000; UiDevice device; @Before public void setup(){ device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); } @Test public void testAA() throws UiObjectNotFoundException, IOException, InterruptedException { File file = new File(PATH); device.dumpWindowHierarchy(file); if (!file.exists()) { print("No File"); return; } try { list = new LinkedList<XMLBean>(); // 1、读取xml文件 SAXReader reader = new SAXReader(); Document document = reader.read(file); // 2、获取根节点 Element rootNode = document.getRootElement(); treeWalk(rootNode); print("list::" + list.toString()); print("list.size::" + list.size()); if (list != null) list.clear(); list = null; print("End"); } catch (Exception e) { e.printStackTrace(); } } private static void print(Object obj) { System.out.println(obj); } private void treeWalk(Element element) throws UiObjectNotFoundException { String text; for (Iterator i = element.elementIterator(); i.hasNext(); ) { Element elementNode = (Element) i.next(); if (elementNode.elements().size() > 0) { treeWalk(elementNode); } print(elementNode.toString()); XMLBean bean = new XMLBean(elementNode); print("-------------------------------------------"); if(bean.text.equals("Play Music")){ print(bean.bounds); } print("-------------------------------------------"); list.add(bean); } }
[uiautomator篇][11]下载dump,解析xml的方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。