UI Automator

来源:互联网 发布:深大大数据 编辑:程序博客网 时间:2024/05/29 15:09

UI Automator:
UI Automator测试框架提供了一组api来构建UI测试执行用户程序和系统程序交互。UI Automator api允许您执行操作,如打开设置菜单或应用程序启动器在测试设备。UI Automator测试框架非常适合写黑box-style自动化测试,在测试代码不依赖于目标应用程序的内部实现细节。

UI Automator测试框架的关键特性包括:
1、 一个查看器检查布局层次结构。有关更多信息,请参见UI Automator Viewer. 。
2、一个API来检索状态信息,在目标设备上执行操作。有关更多信息,请参见Access to device state
3、api支持cross-app UI测试。有关更多信息,请参见UI Automator APIs .。

需要Android 4.3(api Level 18)或者更高

UI Automator Viewer
uiautomatorviewer工具提供了一个方便的GUI来扫描和分析当前Android设备上显示的UI组件。您可以使用这个工具来检查布局层次结构和视图UI组件的属性显示在设备的前景。这个信息可以让你使用UI Automator创建更细粒度的测试,例如通过创建一个UI选择器匹配一个特定的可见属性。
uiautomatorviewer工具位于< android sdk > /工具/目录中。

访问设备状态
UI Automator测试框架提供了一个UiDevice类来访问和在设备上执行操作的目标应用程序正在运行。您可以调用它的方法来访问设备属性如电流取向或显示尺寸。UiDevice类也让您进行如下操作:
1、改变设备旋转
2、按方向键按钮
3、按返回,Home,或者菜单按钮
4、打开通知栏
5、截取当前窗口屏幕
例如:模拟一个按钮按下,调用UiDevice.pressHome()方法。

UI Automator APIs
UI Automator api允许您编写健壮的测试,而不需要知道您锁定的应用程序的实现细节。您可以使用这些api来捕获和操纵跨多个应用程序的UI组件:
1、UiCollection:列举了一个容器的UI元素计数的目的,或针对子元素可见文本或内容描述属性。
2、UiObject:代表一个UI元素是可见的在设备上。
3、UiScrollable:支持可滚动的UI中寻找物品的容器。
4、UiSelector:代表一个查询一个或多个目标设备上的UI元素。
5、Configurator:允许你设置运行UI Automator测试的关键参数。

例如,下面的代码展示了如何编写一个测试脚本,打开默认的应用程序启动器的装置:

// Initialize UiDevice instance
mDevice=UiDevice.getInstance(getInstrumentation());

// perform a short press on the HOME button
mDevice.pressHome();
// Bring up the default launcher by searching for
// a UI component that matches the content-description for the launcher button
UiObject allAppsButton = mDevice
.findObject(new UiSelector().description("Apps"));

// Perform a click on the button to bring up the launcher
allAppsButton.clickAndWaitForNewWindow();

了解更多关于使用UI Automator,看APi reference 和Testing UI for Multiple Apps

测试支持库设置
安卓测试支持库包包含在最新版本的Android支持库,您可以获得作为一个补充通过Android SDK管理器下载。
从SDK Manager下载Android Support Repository:
1.Start the Android SDK Manager.
2.在SDK管理器窗口中,滚动到结束的软件包列表,找到 Extras folder,如果有必要,扩大显示其内容。
3.选择Android支持库项目。
4.点击安装包…按钮。
下载后,工具安装支持库文件到你现有的Android SDK目录。库文件位于以下目录的SDK:< SDK > /配件/ android / m2repository目录。
安卓测试支持库类位于android.support下。测试包。
使用Android Gradle项目中的测试支持库,这些依赖项添加在您的构建.gradle文件:

dependencies{androidTestCompile 'com.android.support.test:runner:0.4'//Set this dependency to use JUnit 4 rulesandroidTestCompile 'com.android.support.test:rules:0.4'//Set this dependency to build and run Espresso testsandroidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'//Set this dependency to build and run UI Automator testsandroidTestCompile 'com.android.supper.test.uiautomator:uiautomator-v18:2.1.2'}

设置 AndroidJUnitRunner为默认的test instrunmentation runner在你的Gradle project、构建指定依赖性在build.gradle文件。

android{    defaultConfig{        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }}

强烈建议您使用安卓系统测试支持库一起Android Studio IDE。Android Studio提供支持测试开发的功能,如:
1、灵活的Gradle-based构建系统,支持你的测试代码的依赖关系管理
2、单一项目结构包含您的单元测试和测试测试代码和应用程序的源代码
3、支持虚拟或物理设备上部署和运行测试,从命令行或图形用户界面

0 0