关闭

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

标签: uiautomator
10393人阅读 评论(18) 收藏 举报
分类:

在之前的第一篇文章:


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


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




今天我学习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>

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

0
0
查看评论

uiautomator获取不到动态界面的缘由

这几天查看了下源码发现,uiautomatorviewer在获取界面布局信息的时候用的是启动一个脚本,该脚本在/system/bin/uiautomator。这个命令也可以在命令行下启动。 默认情况下,获取的控件信息保存在/storage...
  • g19920917
  • g19920917
  • 2014-10-02 09:53
  • 13911

[Android测试] Appium的一些坑问题错误解决 与 技巧集锦

问题1. error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress 之前的会话没有关闭,然后你又运行了测试实例,也没有设置覆盖. 解决: 1...
  • niubitianping
  • niubitianping
  • 2016-09-22 16:42
  • 22075

使用uiautomatorviewer做UI测试

uiautomatorviewer.bat在android/sdk/tools/bin目录下,双击运行。 点击1号按钮,报错: adb not installed' error when trying to take a snapshot with uiautomatorviewe...
  • zh_ang_lei
  • zh_ang_lei
  • 2017-03-14 14:37
  • 3801

点击uiautomatorviewer.bat获取screenshot报错的解决方法

之前获取元素坐标一直使用的是uiautomatorviewer,今天测试时突然联不上我的手机,点击获取screenshot时报如下错误: 点击提示框内的Details如下: Error while obtaining UI hierarchy...
  • Near1314you
  • Near1314you
  • 2017-11-28 14:14
  • 265

monkeyrunner内置uiautomator出错的原因

06-01 17:06:47.897: E/AndroidRuntime(3007): *** FATAL EXCEPTION IN SYSTEM PROCESS: main 06-01 17:06:47.897: E/AndroidRuntime(3007): java.lang.IllegalS...
  • qhshiniba
  • qhshiniba
  • 2014-06-02 15:25
  • 5261

uiautomatorviewer出错记录

第一次接触uiautomator测试,想着先简单熟悉一下工具流程,有个大概印象,谁知道上来uiaotumatorviewer打开截屏就出错: Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException:...
  • Sasoritattoo
  • Sasoritattoo
  • 2013-12-27 10:45
  • 23271

uiautomatorviewer.bat的使用

1、找到uiautomatorviewer.bat文件,我的是在:C:\Users\Administrator\AppData\Local\Android\sdk\tools\bin文件下,双击打开即可,如下图。 2、有可能出现Unable to connect to adb....
  • csc48656
  • csc48656
  • 2017-06-09 10:04
  • 2624

uiautomatorviewer出现Unable to connect to adb

启动测试工具uiautomatorviewer,点击第二个按钮Device Screenshot时,出现Unable to connect to adb. Check if adb is installed correctly错误:  解决办法如下: 打开uiautom...
  • WHACKW
  • WHACKW
  • 2017-08-03 14:46
  • 716

xiaocong的uiautomator有时候找不到界面元素的问题和解决

问题最近一直在用xiaocong的uiautomator做android的自动化测试,这是一个python对android uiautomator的包装,非常好用。不过最近经常出现一个问题,界面上明明有这个元素,但是xiaocong的uiautomator却无法发现,无法点击。解决经过一番折腾,我猜...
  • windyoyo
  • windyoyo
  • 2016-05-13 00:29
  • 3125

使用uiautomatorviewer获取android apk的元素

网上很多介绍使用uiautomatorviewer的方法,但都未说明怎么打开uiautomatorviewer界面,只怪我知识太浅了。下面简单介绍下: 1,打开uiautomatorviewer界面 首先确保Android_home已配置,可以打印看下:echo $ANDROID_HOME 配置好后...
  • xiaoyida11
  • xiaoyida11
  • 2016-04-14 17:21
  • 1379
    个人资料
    • 访问:1760155次
    • 积分:22215
    • 等级:
    • 排名:第388名
    • 原创:570篇
    • 转载:1篇
    • 译文:9篇
    • 评论:577条
    博客专栏