package com.softwinner.performance.benchmark;

/**
* Created by Administrator on 2017/7/21.
*/ 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.performance.benchmark;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log; import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Before;
import org.junit.Test; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
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;
import static org.junit.Assert.assertTrue; /**
* Created by Administrator on 2017/7/21.
*/ public class test { private static List<XMLBean> list;
private static final String PATH = "/sdcard/window_dump.xml";
private final static int LAUNCH_TIMEOUT = 5000;
UiDevice device;
private final String mLogTag = "testliuzhipeng"; @Before
public void setup() throws InterruptedException {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
this.grantPermission();
openApplication("com.antutu.videobench");
Thread.sleep(20000);
device.waitForIdle();
Log.i(mLogTag, "sleep finished");
} @Test
public void testAA() throws UiObjectNotFoundException, IOException, InterruptedException { File file = new File(PATH);
if(file.exists()){
assertTrue(String.format("delete %s fail ",PATH), file.delete());
}
device.dumpWindowHierarchy(file);
if (!file.exists()) {
Log.i(mLogTag, "dump fail");
assertTrue("dump fail", false);
}
try {
list = new LinkedList<XMLBean>();
// 1、读取xml文件
SAXReader reader = new SAXReader();
Document document = reader.read(file);
// 2、获取根节点
Element rootNode = document.getRootElement();
treeWalk(rootNode);
Log.i(mLogTag, "list::" + list.toString());
Log.i(mLogTag, "list.size::" + list.size());
if (list != null)
list.clear();
list = null;
Log.i(mLogTag, "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());
Log.i(mLogTag, elementNode.toString());
XMLBean bean = new XMLBean(elementNode);
print("-------------------------------------------");
Log.i(mLogTag, "-------------------------------------");
if (bean.text.equals("Play Music")) {
print(bean.bounds);
Log.i(mLogTag, bean.bounds);
}
print("-------------------------------------------");
list.add(bean);
}
}
public void openApplication(String packageNameStr){
// wakeScreen();
/* Start from the home screen*/
device.pressHome(); final String launcherPackage = device.getLauncherPackageName();
assertThat(launcherPackage,notNullValue());
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT); // launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(packageNameStr);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent); // Wait for the app to appear
// try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
// wakeScreen();
device.wait(Until.hasObject(By.pkg(packageNameStr).depth(0)),
LAUNCH_TIMEOUT);
}
public void grantPermission(){
openApplication("com.softwinner.performance.frameratetest");
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
Log.i(mLogTag,"after open test apk success, grant stored permission");
// wakeScreen();
UiObject permission = device.findObject(new UiSelector()
.resourceId("com.android.packageinstaller:id/permission_allow_button")
);
if(permission.waitForExists(1000)){
try {
// wakeScreen();
permission.clickAndWaitForNewWindow();
Log.i(mLogTag,"click grant store permission");
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
} public void printMessage(final InputStream input){
new Thread(new Runnable() {
@Override
public void run() {
Reader reader = new InputStreamReader(input);
BufferedReader bf = new BufferedReader(reader);
String line = null;
try{
while((line = bf.readLine()) != null ){
Log.i(mLogTag, line);
}
}
catch (IOException e){
e.printStackTrace();
}
}
}).start(); }
}

[java开发篇][dom4j模块]遍历,解析xml的更多相关文章

  1. [java开发篇][dom模块] 遍历解析xml

    http://blog.csdn.net/andie_guo/article/details/24844351 XML DOM节点树 XML DOM将XML文档作为树结构,树结构称为一个节点树.所有的 ...

  2. [java开发篇][dom4j模块] 遍历xml文件

    http://blog.csdn.net/chenleixing/article/details/44353491 在android studio 导入dom4j库(build-gradle(Moud ...

  3. 使用dom4j创建和解析xml文件

    使用dom4j创建和解析xml文件 在项目开发中,我们经常会遇到xml文件的创建和解析从别人接口得到的xml文件,而我们最常使用的组件是dom4j. 下面我就以代码来讲解一下如何使用dom4j来创建x ...

  4. DOM4J生成、解析XML实例

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterato ...

  5. dom4j生成和解析xml文件

    dom4j生成和解析xml文件 要生成和解析如下格式的xml文件: <?xml version="1.0" encoding="UTF-8"?> & ...

  6. nodejs模块xml2js解析xml的坑

    在一个项目中,用到nodejs模块xml2js解析xml,xml的数据如下: <xml> <MsgId>6197906553041859764</MsgId> &l ...

  7. java使用dom4j和XPath解析XML与.net 操作XML小结

    最近研究java的dom4j包,使用 dom4j包来操作了xml 文件 包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), ...

  8. Java高级特性 第15节 解析XML文档(3) - JDOM和DOM4J技术

    一.JDOM解析 特征: 1.仅使用具体类,而不使用接口. 2.API大量使用了Collections类. Jdom由6个包构成: Element类表示XML文档的元素 org.jdom: 解析xml ...

  9. 《阿里巴巴Java开发手册v1.2》解析(编程规约篇)

    之前在乐视天天研究各种底层高大上的东西,因为我就一个人,想怎么弄怎么弄.如今来了新美大,好好研读一下<阿里巴巴Java开发手册v1.2>.还要对这么看似简单的东西解析一番.毕竟现在带团队, ...

随机推荐

  1. uvm_reg_map——寄存器模型(八)

    所有的寄存器都需要地址,都需要加入到地址列表中 //-------------------------------------------------------------------------- ...

  2. Jquery 如何获取表单中的全部元素的值

    1.使用var formData = $(formId).serialize()获取:获取数据的格式为url参数形式的字符串.例如:id=100&name=张三   2.服务器端使用parse ...

  3. python 之网页解析器

    一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...

  4. fpga Verilog hdl 按键消抖 部分程序讲解

    module debounce(clk_in,rst_in,key_in,key_pulse,key_state); input clk_in;//system clock input rst_in; ...

  5. MovieReview—Black Panther(黑豹)

    Justice & Evil   The night before the night, i saw the latest movie in the Marvel series at JiaH ...

  6. js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

    js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

  7. 超全面Java 面试题(2.1)

    这部分主要是开源JavaEE框架方面的内容,包括hibernate.MyBatis.spring.Spring MVC等,由于Struts2已经是明日黄花,在这里就不讨论Struts2的面试题,此外, ...

  8. java基础—GUI编程(二)

    一.事件监听

  9. ps基础实例

    一:合并多个图片 1.先新件一个图片)CTRL+N),大小定成你想要的大小 2.把你要放入的照片用PS打开 3.把放入的照片用移动工具(V)拉到新件的图片里面 4.用CTRL+T调整大小(按住SHIF ...

  10. Bootstrap历练实例:带列表组的面板

    带列表组的面板 我们可以在任何面板中包含列表组,通过在 <div> 元素中添加 .panel 和 .panel-default 类来创建面板,并在面板中添加列表组.您可以从 列表组 一章中 ...