UI Automator 测试工具定义以及用途


UI Automator 测试框架提供了一组 API,用于构建在用户应用和系统应用上执行交互的界面测试。通过 UI Automator API,您可以执行在测试设备中打开“设置”菜单或应用启动器等操作。UI Automator 测试框架非常适合编写黑盒式自动化测试,此类测试的测试代码不依赖于目标应用的内部实现细节。


优点:可以对所有的操作来进行自动化,操作简单(eg:点击事件 ,侧滑事件,上拉事件,以及模拟键盘输入测试用例)。可以测试所有设备的程序。

缺点:必须要Android4.0以上才能使用。


UI Automator 的执行呢,需要我们编写完毕打成jar包,直接上传到/data/local/tmp/ 目录下面,然后用adb 命令执行即可。由于是安卓原生的,比较稳定,运行的速度也比较快。

我们下载了安卓的环境变量后,然后下载任意大于4.0的安卓版本sdk即可。

都会有这个jar包,我们在写代码,创建工程的时候,直接引入即可。

如何获取定位呢,我们可以uiautomatorviewer。

UI Automator 测试框架的主要功能包括:

用于检查布局层次结构的查看器。

用于检索状态信息并在目标设备上执行操作的 API。

支持跨应用界面测试的 API。

UI Automator 测试框架提供了一个 UiDevice 类,用于在运行目标应用的设备上访问和执行操作。您可以调用其方法以访问设备属性,如当前屏幕方向或显示屏尺寸。UiDevice 类还可用于执行以下操作:

改变设备的旋转。

按硬件键,如“音量调高按钮”。

按返回、主屏幕或菜单按钮。

打开通知栏。

截取当前窗口的屏幕截图。

UI Automator API

通过 UI Automator API,您可以编写可靠的测试,而无需了解目标应用的实现细节。您可以使用这些 API 在多个应用间捕获和操纵界面组件:

UiCollection:枚举容器的界面元素,目的是为了计数,或者按可见文本或内容说明属性来定位子元素。

UiObject:表示设备上可见的界面元素。

UiScrollable:支持搜索可滚动界面容器中的项目。

UiSelector:表示对设备上的一个或多个目标界面元素的查询。

Configurator:可让您设置用于运行 UI Automator 测试的关键参数。

官方文档

https://developer.android.com/reference/androidx/test/uiautomator/package-summary

官方文档有着详细的介绍

正常的我们加入后,在后可以看到,有什么类,方式是什么,里面有什么api。

我们可以在引入后查看。

下面列举几个类的方法

UiDevice

void    clearLastTraversedText()
// 清除上次UI遍历的事件 boolean click(int x, int y)
// 根据坐标点击 boolean drag(int startX, int startY, int endX, int endY, int steps)
// 拖动 void dumpWindowHierarchy(File dest)
// dump当前的层次化结构到文件中 void dumpWindowHierarchy(OutputStream out)
// dump当前的层次化结构到流中 void dumpWindowHierarchy(String fileName)
// dump当前的层次化结构到文件中 UiObject2 findObject(BySelector selector)
// 根据BySelector查找 UiObject findObject(UiSelector selector)
// 根据UiSelector 查找 List<UiObject2> findObjects(BySelector selector)
// 根据BySelector查找 void freezeRotation()
// 冻结旋转的状态 String getCurrentActivityName()
// 获取当前Activity的名字,已经被废弃 String getCurrentPackageName()
// 获取当前package int getDisplayHeight()
// Gets the height of the display, in pixels. int getDisplayRotation()
// Returns the current rotation of the display, as defined in Surface Point getDisplaySizeDp()
// Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation. int getDisplayWidth()
// Gets the width of the display, in pixels. static UiDevice getInstance()
// 获取一个对象 static UiDevice getInstance(Instrumentation instrumentation)
// Retrieves a singleton instance of UiDevice String getLastTraversedText()
// 获取上一次遍历的文本 String getLauncherPackageName()
// 获取运行的packagename String getProductName()
// Retrieves the product name of the device. boolean hasAnyWatcherTriggered() // 检查是否有触发器触发 boolean hasObject(BySelector selector)
// 是否有符合的条件的 boolean hasWatcherTriggered(String watcherName)
// Checks if a specific registered UiWatcher has triggered. boolean isNaturalOrientation()
// Check if the device is in its natural orientation. boolean isScreenOn()
// Checks the power manager if the screen is ON. boolean openNotification()
// Opens the notification shade.
// 打开通知 boolean openQuickSettings()
// 打开设置 <R> R performActionAndWait(Runnable action, EventCondition<R> condition, long timeout)
// Performs the provided action and waits for the condition to be met. boolean pressBack()
// Simulates a short press on the BACK button. boolean pressDPadCenter()
// Simulates a short press on the CENTER button. boolean pressDPadDown()
// Simulates a short press on the DOWN button. boolean pressDPadLeft()
// Simulates a short press on the LEFT button. boolean pressDPadRight()
// Simulates a short press on the RIGHT button. boolean pressDPadUp()
// Simulates a short press on the UP button. boolean pressDelete()
// Simulates a short press on the DELETE key. boolean pressEnter()
// Simulates a short press on the ENTER key. boolean pressHome()
// Simulates a short press on the HOME button. boolean pressKeyCode(int keyCode)
// Simulates a short press using a key code. boolean pressKeyCode(int keyCode, int metaState)
// Simulates a short press using a key code. boolean pressMenu()
// Simulates a short press on the MENU button. boolean pressRecentApps()
// Simulates a short press on the Recent Apps button. boolean pressSearch()
// Simulates a short press on the SEARCH button. void registerWatcher(String name, UiWatcher watcher)
// Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. void removeWatcher(String name)
// Removes a previously registered UiWatcher. void resetWatcherTriggers()
// Resets a UiWatcher that has been triggered. void runWatchers()
// This method forces all registered watchers to run. void setCompressedLayoutHeirarchy(boolean compressed)
// Enables or disables layout hierarchy compression. void setOrientationLeft()
// 设置旋转方向 void setOrientationNatural()
// Simulates orienting the device into its natural orientation and also freezes rotation by disabling the sensors. void setOrientationRight()
// Simulates orienting the device to the right and also freezes rotation by disabling the sensors. void sleep()
// 关闭屏幕 boolean swipe(int startX, int startY, int endX, int endY, int steps)
// Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed. boolean swipe(Point[] segments, int segmentSteps)
// Performs a swipe between points in the Point array. boolean takeScreenshot(File storePath, float scale, int quality)
// 截屏
boolean takeScreenshot(File storePath)
// Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation void unfreezeRotation()
// Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation. <R> R wait(SearchCondition<R> condition, long timeout)
// Waits for given the condition to be met. void waitForIdle(long timeout)
// Waits for the current application to idle. void waitForIdle()
// Waits for the current application to idle. boolean waitForWindowUpdate(String packageName, long timeout)
// Waits for a window content update event to occur. void wakeUp()
// 点亮屏幕

  

UiObject
void    clearTextField()
// 清空输入接口 boolean click()
// 点击 boolean clickAndWaitForNewWindow()
// 点击并等待新界面 boolean clickAndWaitForNewWindow(long timeout)
// 点击并等待新界面,设置等待时间 boolean clickBottomRight()
// 点击右下边 boolean clickTopLeft()
// Clicks the top and left corner of the UI element boolean dragTo(UiObject destObj, int steps)
// 拖动 boolean dragTo(int destX, int destY, int steps)
// Drags this object to arbitrary coordinates. boolean exists()
// 判断是否存在 Rect getBounds()
// 返回边界 UiObject getChild(UiSelector selector)
// 根据条件获取子元素 int getChildCount()
// 获取子元素数量 String getClassName()
// 获取当前元素的class name String getContentDescription()
// Reads the content_desc property of the UI element UiObject getFromParent(UiSelector selector)
// Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject. String getPackageName()
// Reads the view's package property final UiSelector getSelector()
// Debugging helper. String getText()
// Reads the text property of the UI element Rect getVisibleBounds()
// 获取可见边界 boolean isCheckable()
// 是否可以点击 boolean isChecked()
// 是否已经选中 boolean isClickable()
// Checks if the UI element's clickable property is currently true. boolean isEnabled()
// Checks if the UI element's enabled property is currently true. boolean isFocusable()
// Check if the UI element's focusable property is currently true. boolean isFocused()
// Check if the UI element's focused property is currently true boolean isLongClickable()
// Check if the view's long-clickable property is currently true boolean isScrollable()
// Check if the view's scrollable property is currently true boolean isSelected()
// Checks if the UI element's selected property is currently true. boolean longClick()
// 长按 boolean longClickBottomRight()
// Long clicks bottom and right corner of the UI element boolean longClickTopLeft()
// Long clicks on the top and left corner of the UI element boolean performMultiPointerGesture(PointerCoords... touches)
// Performs a multi-touch gesture. boolean performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, int steps)
// Generates a two-pointer gesture with arbitrary starting and ending points. boolean pinchIn(int percent, int steps)
// Performs a two-pointer gesture, where each pointer moves diagonally toward the other, from the edges to the center of this UiObject . boolean pinchOut(int percent, int steps)
// Performs a two-pointer gesture, where each pointer moves diagonally opposite across the other, from the center out towards the edges of the this UiObject. boolean setText(String text)
// 设置输入内容 boolean swipeDown(int steps)
// Performs the swipe down action on the UiObject. boolean swipeLeft(int steps)
// Performs the swipe left action on the UiObject. boolean swipeRight(int steps)
// Performs the swipe right action on the UiObject. boolean swipeUp(int steps)
// Performs the swipe up action on the UiObject. boolean waitForExists(long timeout)
// Waits a specified length of time for a view to become visible. boolean waitUntilGone(long timeout)
// Waits a specified length of time for a view to become undetectable.

  

下面给大家展示一个UiAutomator的testcase
package cn.cool;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import android.os.RemoteException;

public class Testone extends UiAutomatorTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testCool() throws RemoteException, InterruptedException { }
@Override
protected void tearDown() throws Exception { } }

  

然后我们可以在实际的项目中,去实现我们真实的业务的代码。实现完毕呢,
我们就可以用 adb . shell push cool.jar /data/local/tmp/
然后使用
adb shell uiautomator runtest cool.jar -c cn.cool.Testone
就可以执行了。uiautomator 由于只能打成jar包,所以它不能直接在编辑器上进行调试。

Android测试工具 UIAutomator介绍的更多相关文章

  1. [原创]Android Monkey测试工具使用介绍

    [原创]Android Monkey测试工具使用介绍 1 Android Monkey介绍 Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件 ...

  2. 2014 非常好用的开源 Android 测试工具

    http://www.php100.com/html/it/mobile/2014/1015/7495.html 当前有很大的趋势是转向移动应用平台,Android 是最广泛使用的移动操作系统,201 ...

  3. 常见Android测试工具简介

    在进行android设备测试过程中,在进行系统测试时候,往往需要关注到很多方面,导致一个崩溃或者运行一段时间自动重启或者停止的问题很多.最简单来看,影响因素就有:底层硬件设备.OS层.上层app层.另 ...

  4. Android 测试工具集01

    Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...

  5. 测试工具Wiremock介绍

    WireMock是一个开源的测试工具,支持HTTP响应存根.请求验证.代理/拦截.记录和回放.最直接的用法: 为Web/移动应用构建Mock Service 快速创建Web API原型 模拟Web S ...

  6. monkey测试===Android测试工具Monkey用法简介(转载)

    Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey ...

  7. Android性能测试工具Emmagee介绍

    Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而行为 ...

  8. Android 测试工具集02

    User scenario testing for Android(功能性测试框架) Robotium is an Android test automation framework that has ...

  9. 【Android测试工具】Android抓包解析全过程

    需求原因 在android开发中,遇到socket编程,无法从log日志中查看到与之通讯的socket发送和返回的数据包是什么,这里介绍一个工具,tcpdump工具和wireshark工具查看抓到的内 ...

随机推荐

  1. HashTable学习

    HashTable虽然加上了线程安全,但是源码走向和思想比hashMap还是要简单直白很多,hashmap还得再看,因为很多关键点没有get,下午&明天自己去debug一遍再继续看看博客 脑子 ...

  2. 趣图:当我修复一个隐藏Bug之后

    当我修复一个隐藏Bug时 然后,陷入了死循环中....   扩展阅读 趣图:大佬如何解决bug的 趣图:快下班了,剩一个bug,修复一下再走 趣图:当我给老板展示我修复了那个 bug 时 趣图:当同事 ...

  3. 趣图:普通人讲故事 VS 程序员讲故事

      扩展阅读 趣图:我说自己菜 vs 大佬说自己菜 趣图:客户需求VS客户预算 趣图:在外行人眼中的程序员 如何处理前任程序员留下的代码 一个故事讲清楚NIO

  4. IoC基础篇(一)--- Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  5. 关于struts1与struts2

    1.Action方面 Action是整个Struts框架的核心内容,Struts1.x与Struts2的Action模型很大的区别.Struts2是一个 pull-MVC架构 ,从开发者角度看,就是说 ...

  6. VS2017 Xamarin开发Android时首次部署完成后直接闪退

    项目属性切换到Android选项,在打包属性上有一个[使用共享运行时]的选项要取消勾选,默认打钩时apk文件比较小,但程序无法运行起来. 取消后安装包一小变成几十M,这个目前好像没什么好的解决办法,毕 ...

  7. Raft 协议

    Paxos 存在的问题 Paxos 算法的描述偏学术化,缺失了很多细节,无法直接应用于工程领域.实际工程应用中的分布式算法大多是 Paxos 的变种,验证这些算法的正确性也成为了一个难题. 举个例子: ...

  8. spring-boot-route(三)实现多文件上传

    Spring Boot默认上传的单个文件大小1MB,一次上传的总文件大小为10MB. 单个文件上传使用MultipartFile参数来接收文件,多文件使用MultipartFile[]数组来接收,然后 ...

  9. 借助C++探究素数的分布

    这里使用的区间是36,144,576,2304,9216,36864,147456,589824,2359296,9437184.至于这个区间是怎么得到的,感兴趣的同鞋可前往(https://www. ...

  10. matlab中fspecial Create predefined 2-D filter以及中值滤波均值滤波以及高斯滤波

    来源: 1.https://ww2.mathworks.cn/help/images/ref/fspecial.html?searchHighlight=fspecial&s_tid=doc_ ...