uiautomator中怎样统计触发控件的总次数
展开全部
今天我学习uiautomator的时候,用dumpWindowHierarchy()获得控件信息的时候,发现这个方法可以得到动态的界面信息。这就弥补了uiautomatorviewer的缺陷。下面把过程给大家贴出来。
如何编写java工程文件的信息,前辈们写了很多,我把我学习的连接放给大家。
我捡主要的将。java代码:
package com.ibm.test;
import java.io.File;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class MyUIauto extends UiAutomatorTestCase {
private static final String TAG = "MyUIauto";
public void testPressHome() throws UiObjectNotFoundException {
File file = new File("/data/local/tmp/local/tmp/qian.xml");
UiDevice uiDevice = getUiDevice();
//uiDevice.pressHome();
String realPath = "qian.xml";
uiDevice.dumpWindowHierarchy(realPath);
}
}
我看了一下dumpWindowHierarchy的方法源码:
public void [More ...] dumpWindowHierarchy(String fileName) {
AccessibilityNodeInfo root = getAutomatorBridge().getQueryController().getAccessibilityRootNode();
if(root != null) {
AccessibilityNodeInfoDumper.dumpWindowToFile(
root, new File(new File(Environment.getDataDirectory(),
"local/tmp"), fileName));
}
}
官网上的说明默认目录是/data/local/tmp目录,但从源码上可以看见它又增加了local/tmp目录。合并起来就是/data/local/tmp/local/tmp目录。这也是我起先随便建立个目录造成的错误的原因,老是报/data/local/tmp/local/tmp/下无文件的错误。后来找到源码才知道。源码网站给大家:
grepcode
编写完java工程 以后,用ant命令生成jar包,然后把jar包push到设备上,将秒表打开。然后运行脚本,得到xml文件,抓到本地。查看xml文件如下:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
<node index="0" text="" resource-id="" class="android.widget.FrameLayout"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false" password="false"
selected="false" bounds="[0,0][480,854]">
<node index="0" text="" resource-id="android:id/action_bar_overlay_layout"
class="android.view.View" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,0][480,854]">
<node index="0" text="" resource-id="android:id/action_bar_container"
class="android.widget.FrameLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,38][480,110]">
<node index="0" text="" resource-id="android:id/action_bar"
class="android.view.View" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,38][480,110]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="true"
enabled="false" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[24,38][90,110]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[24,55][90,92]">
<node index="0" text="秒表" resource-id="android:id/action_bar_title"
class="android.widget.TextView" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[24,55][78,92]" />
</node>
</node>
</node>
</node>
<node index="1" text="" resource-id="android:id/content" class="android.widget.FrameLayout"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false" password="false"
selected="false" bounds="[0,110][480,764]">
<node index="0" text=""
resource-id="com.android.deskclock:id/desk_clock_pager" class="android.support.v4.view.ViewPager"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="true"
focused="false" scrollable="true" long-clickable="false" password="false"
selected="false" bounds="[0,110][480,764]">
<node index="3" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[0,110][480,764]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[36,110][444,373]">
<node index="0" text="" resource-id=""
class="android.widget.RelativeLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[36,140][444,331]">
<node index="0" text="00:06:24.9"
resource-id="com.android.deskclock:id/interval_time" class="android.widget.TextView"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false"
password="false" selected="false" bounds="[261,140][428,189]" />
<node index="1" text="00:06:24.9" resource-id="com.android.deskclock:id/total_time"
class="android.widget.TextView" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[43,219][436,331]" /
如何编写java工程文件的信息,前辈们写了很多,我把我学习的连接放给大家。
我捡主要的将。java代码:
package com.ibm.test;
import java.io.File;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class MyUIauto extends UiAutomatorTestCase {
private static final String TAG = "MyUIauto";
public void testPressHome() throws UiObjectNotFoundException {
File file = new File("/data/local/tmp/local/tmp/qian.xml");
UiDevice uiDevice = getUiDevice();
//uiDevice.pressHome();
String realPath = "qian.xml";
uiDevice.dumpWindowHierarchy(realPath);
}
}
我看了一下dumpWindowHierarchy的方法源码:
public void [More ...] dumpWindowHierarchy(String fileName) {
AccessibilityNodeInfo root = getAutomatorBridge().getQueryController().getAccessibilityRootNode();
if(root != null) {
AccessibilityNodeInfoDumper.dumpWindowToFile(
root, new File(new File(Environment.getDataDirectory(),
"local/tmp"), fileName));
}
}
官网上的说明默认目录是/data/local/tmp目录,但从源码上可以看见它又增加了local/tmp目录。合并起来就是/data/local/tmp/local/tmp目录。这也是我起先随便建立个目录造成的错误的原因,老是报/data/local/tmp/local/tmp/下无文件的错误。后来找到源码才知道。源码网站给大家:
grepcode
编写完java工程 以后,用ant命令生成jar包,然后把jar包push到设备上,将秒表打开。然后运行脚本,得到xml文件,抓到本地。查看xml文件如下:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
<node index="0" text="" resource-id="" class="android.widget.FrameLayout"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false" password="false"
selected="false" bounds="[0,0][480,854]">
<node index="0" text="" resource-id="android:id/action_bar_overlay_layout"
class="android.view.View" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,0][480,854]">
<node index="0" text="" resource-id="android:id/action_bar_container"
class="android.widget.FrameLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,38][480,110]">
<node index="0" text="" resource-id="android:id/action_bar"
class="android.view.View" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[0,38][480,110]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="true"
enabled="false" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[24,38][90,110]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false" bounds="[24,55][90,92]">
<node index="0" text="秒表" resource-id="android:id/action_bar_title"
class="android.widget.TextView" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[24,55][78,92]" />
</node>
</node>
</node>
</node>
<node index="1" text="" resource-id="android:id/content" class="android.widget.FrameLayout"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false" password="false"
selected="false" bounds="[0,110][480,764]">
<node index="0" text=""
resource-id="com.android.deskclock:id/desk_clock_pager" class="android.support.v4.view.ViewPager"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="true"
focused="false" scrollable="true" long-clickable="false" password="false"
selected="false" bounds="[0,110][480,764]">
<node index="3" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[0,110][480,764]">
<node index="0" text="" resource-id=""
class="android.widget.LinearLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[36,110][444,373]">
<node index="0" text="" resource-id=""
class="android.widget.RelativeLayout" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[36,140][444,331]">
<node index="0" text="00:06:24.9"
resource-id="com.android.deskclock:id/interval_time" class="android.widget.TextView"
package="com.android.deskclock" content-desc="" checkable="false"
checked="false" clickable="false" enabled="true" focusable="false"
focused="false" scrollable="false" long-clickable="false"
password="false" selected="false" bounds="[261,140][428,189]" />
<node index="1" text="00:06:24.9" resource-id="com.android.deskclock:id/total_time"
class="android.widget.TextView" package="com.android.deskclock"
content-desc="" checkable="false" checked="false" clickable="false"
enabled="true" focusable="false" focused="false" scrollable="false"
long-clickable="false" password="false" selected="false"
bounds="[43,219][436,331]" /
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询