Android自动化测试中uiautomator修改uiautomatorviewer获取不到动态界面的缺陷

在之前的第一篇文章:


android自动化测试中hierarchyviewer和uiautomatorviewer获取控件信息的方式比对(1)


我说uiautomatorviewer获取动态的界面会报错。错误如下:


Android自动化测试中uiautomator修改uiautomatorviewer获取不到动态界面的缺陷_第1张图片


今天我学习uiautomator的时候,用dumpWindowHierarchy()获得控件信息的时候,发现这个方法可以得到动态的界面信息。这就弥补了uiautomatorviewer的缺陷。下面把过程给大家贴出来。


如何编写java工程文件的信息,前辈们写了很多,我把我学习的连接放给大家。


http://blog.csdn.net/robinzhou/article/details/8447058


我捡主要的将。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]" />
							</node>
						</node>
						<node index="1" 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="[18,405][462,477]">
							<node index="0" text="暂停" resource-id="com.android.deskclock:id/pause"
								class="android.widget.Button" package="com.android.deskclock"
								content-desc="" checkable="false" checked="false" clickable="true"
								enabled="true" focusable="true" focused="false" scrollable="false"
								long-clickable="false" password="false" selected="false"
								bounds="[18,405][160,477]" />
							<node index="1" text="停止" resource-id="com.android.deskclock:id/stop"
								class="android.widget.Button" package="com.android.deskclock"
								content-desc="" checkable="false" checked="false" clickable="true"
								enabled="true" focusable="true" focused="false" scrollable="false"
								long-clickable="false" password="false" selected="false"
								bounds="[169,405][311,477]" />
							<node index="2" text="计次" resource-id="com.android.deskclock:id/mark"
								class="android.widget.Button" package="com.android.deskclock"
								content-desc="" checkable="false" checked="false" clickable="true"
								enabled="true" focusable="true" focused="false" scrollable="false"
								long-clickable="false" password="false" selected="false"
								bounds="[320,405][462,477]" />
						</node>
						<node index="2" 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="[18,510][462,716]">
							<node index="0" text="" resource-id="com.android.deskclock:id/list"
								class="android.widget.ListView" package="com.android.deskclock"
								content-desc="" checkable="false" checked="false" clickable="true"
								enabled="true" focusable="false" focused="false" scrollable="false"
								long-clickable="false" password="false" selected="false"
								bounds="[18,526][462,700]" />
						</node>
					</node>
				</node>
			</node>
			<node index="2" text="" resource-id="android:id/split_action_bar"
				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,764][480,854]">
				<node index="0" text="" resource-id=""
					class="android.widget.HorizontalScrollView" package="com.android.deskclock"
					content-desc="" checkable="false" checked="false" clickable="false"
					enabled="true" focusable="true" focused="false" scrollable="false"
					long-clickable="false" password="false" selected="false" bounds="[0,764][480,854]">
					<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="[0,764][480,854]">
						<node index="0" text="" resource-id=""
							class="android.app.ActionBar$Tab" package="com.android.deskclock"
							content-desc="" checkable="false" checked="false" clickable="true"
							enabled="true" focusable="true" focused="false" scrollable="false"
							long-clickable="false" password="false" selected="false"
							bounds="[0,764][120,854]">
							<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,770][96,850]">
								<node index="0" text="" resource-id="com.android.deskclock:id/tab_icon"
									class="android.widget.ImageView" package="com.android.deskclock"
									content-desc="" checkable="false" checked="false" clickable="false"
									enabled="true" focusable="true" focused="false" scrollable="false"
									long-clickable="false" password="false" selected="false"
									bounds="[36,770][84,818]" />
								<node index="1" text="闹钟" resource-id="com.android.deskclock:id/tab_text"
									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,818][96,850]" />
							</node>
						</node>
						<node index="1" text="" resource-id=""
							class="android.app.ActionBar$Tab" package="com.android.deskclock"
							content-desc="" checkable="false" checked="false" clickable="true"
							enabled="true" focusable="true" focused="false" scrollable="false"
							long-clickable="false" password="false" selected="false"
							bounds="[120,764][240,854]">
							<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="[144,770][216,850]">
								<node index="0" text="" resource-id="com.android.deskclock:id/tab_icon"
									class="android.widget.ImageView" package="com.android.deskclock"
									content-desc="" checkable="false" checked="false" clickable="false"
									enabled="true" focusable="true" focused="false" scrollable="false"
									long-clickable="false" password="false" selected="false"
									bounds="[156,770][204,818]" />
								<node index="1" text="世界时间" resource-id="com.android.deskclock:id/tab_text"
									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="[144,818][216,850]" />
							</node>
						</node>
						<node index="2" text="" resource-id=""
							class="android.app.ActionBar$Tab" package="com.android.deskclock"
							content-desc="" checkable="false" checked="false" clickable="true"
							enabled="true" focusable="true" focused="false" scrollable="false"
							long-clickable="false" password="false" selected="false"
							bounds="[240,764][360,854]">
							<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="[264,770][336,850]">
								<node index="0" text="" resource-id="com.android.deskclock:id/tab_icon"
									class="android.widget.ImageView" package="com.android.deskclock"
									content-desc="" checkable="false" checked="false" clickable="false"
									enabled="true" focusable="true" focused="false" scrollable="false"
									long-clickable="false" password="false" selected="false"
									bounds="[276,770][324,818]" />
								<node index="1" text="倒计时" resource-id="com.android.deskclock:id/tab_text"
									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="[264,818][336,850]" />
							</node>
						</node>
						<node index="3" text="" resource-id=""
							class="android.app.ActionBar$Tab" package="com.android.deskclock"
							content-desc="" checkable="false" checked="false" clickable="true"
							enabled="true" focusable="true" focused="false" scrollable="false"
							long-clickable="false" password="false" selected="true"
							bounds="[360,764][480,854]">
							<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="true"
								bounds="[384,770][456,850]">
								<node index="0" text="" resource-id="com.android.deskclock:id/tab_icon"
									class="android.widget.ImageView" package="com.android.deskclock"
									content-desc="" checkable="false" checked="false" clickable="false"
									enabled="true" focusable="true" focused="false" scrollable="false"
									long-clickable="false" password="false" selected="true"
									bounds="[396,770][444,818]" />
								<node index="1" text="秒表" resource-id="com.android.deskclock:id/tab_text"
									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="true"
									bounds="[384,818][456,850]" />
							</node>
						</node>
					</node>
				</node>
			</node>
		</node>
	</node>
</hierarchy>

所以,这个方式才是获得控件的最佳方法。

你可能感兴趣的:(UiAutomator)