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. 如何在cmd查看文件内容的md5值

    在cmd下进入 要查看的文件目录 默认目录是c,切换到其他盘符例如: C:\D: 就会切换到D盘 D:\ 然后输入命令 certutil -hashfile  文件名称.文件类型 MD5 如 cert ...

  2. 使用poi或jxl,通过java读写xls、xlsx文档

    package nicetime.com.baseutil; import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffExcepti ...

  3. [神经网络]一步一步使用Mobile-Net完成视觉识别(一)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第一篇,环境配置篇. 先打开tensorflow object detection api 看看需要什 ...

  4. Fruit Ninja(取随机数)

    链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...

  5. 2018.2.10 使用SSH连接远程滴滴云服务器Ubuntu (Windows下) 及 putty工具永久设置字体、颜色

    一开始会有人问云服务器是什么? 云服务器是一种类似VPS服务器的虚拟化技术, VPS是采用虚拟软件,VZ或VM在一台服务器上虚拟出多个类似独立服务器的部分,每个部分都可以做单独的操作系统,管理方法同服 ...

  6. CRF条件随机场简介<转>

    转自http://hi.baidu.com/hehehehello/item/3b0d1f8ba1c2e5c698255f89 CRF(Conditional Random Field) 条件随机场是 ...

  7. vector 下标操作

    比如:vector<int> ivec(3).. 当采用下标操作ivec[10]的时候,该操作是未定义的,在自己的机器上输出的值是零.建议使用迭代器进行操作.

  8. C#访问数组元素

    在C#中,使用索引来访问数组元素.索引必须是一个整型值. 在数组中,每一个维度的索引从0开始. 一.访问一维数组元素 int[] array = {1,2,3,4,5,6,7,8,9,10}; // ...

  9. Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用

    ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...

  10. Unity基础-发布

    PC BuildSetting File->BuildSetting Switch Platform Development Build是在调试模式下使用版本 Script Debugging调 ...