Android uiautomator 使用入门官方教程
本文英文原文 http://developer./tools/testing/testing_ui.html
In addition to unit testing the individual components that make up your Android application (such as activities, services, and content providers), it is also important that you test the behavior of your application’s user interface (UI) when it is running on a device. UI testing ensures that your application returns the correct UI output in response to a sequence of user actions on a device, such as entering keyboard input or pressing toolbars, menus, dialogs, images, and other UI controls. 除了对组成安卓应用的单独的组件(如,activities、services、和content providers)进行单元测试,测试应用运行时的界面行为也很必要。UI测试确保应用在一系列用户操作后,如键盘输入、按压工具栏、菜单、对话框、图 片或其他UI空间,返回正确的UI输出。 Functional or black-box UI testing does not require testers to know the internal implementation details of the app, only its expected output when a user performs a specific action or enters a specific input. This approach allows for better separation of development and testing roles in your organization. 功能或黑盒测试不需要测试人员知晓应用的内部实现细节,只要明白在一系列用户操作后返回期望的UI输出即可。这种测试方法运行开发、测试角色的可以由组织中的不同团队来担任。 One common approach to UI testing is to run tests manually and verify that the app is behaving as expected. However, this approach can be time-consuming, tedious, and error-prone. A more efficient and reliable approach is to automate the UI testing with a software testing framework. Automated testing involves creating programs to perform testing tasks (test cases) to cover specific usage scenarios, and then using the testing framework to run the test cases automatically and in a repeatable manner.
UI测试的通用方式是手工运行测试,验证应用如期望般运行。然而,此种方法非常耗时、无趣、易出错。更有效、更可靠的方式是使用软件
测试框架自动化UI测试。自动化测试涉及创建程序执行测试任务(测试用例)来覆盖指定的用户场景,然后使用测试框架自动化重复地运行测试用例。
The Android SDK provides the following tools to support automated, functional UI testing on your application: Android SDK提供下述工具;来支持自动化的功能界面测试:
To use these tools, you must have the following versions of the Android development tools installed: 为使用这些工具,需要安装的android工具工具的版本信息:
Workflow for the the uiautomator testing framework 使用uiautomator测试框架的工作流程 Here’s a short overview of the steps required to automate UI testing: 简单介绍下UI自动化测试的主要步骤:
Analyzing Your Application’s UI 分析应用的UI界面
Before you start writing your test cases, it’s helpful to
familiarize yourself with the UI components (including the views and
controls) of the targeted application. You can use the uiautomatorviewer
tool to take a snapshot of the foreground UI screen on any Android
device that is connected to your development machine. The
uiautomatorviewer tool provides a convenient visual interface to inspect
the layout hierarchy and view the properties of the individual UI
components that are displayed on the test device. Using this
information, you can later create uiautomator tests with selector
objects that target specific UI components to test.
Figure 1. The uiautomatorviewer showing the captured interface of a test deviice. 图1 uiautomatorviewer捕获的测试设备的界面 To analyze the UI components of the application that you want to test:
分析待测程序的UI组件的步骤:
Preparing to Test 准备测试 Before using the uiautomator testing framework, complete these pre-flight tasks: 在开始使用uiautomator测试框架之前,完成下述准备工作: Load the application to a device 安装待测应用到待测设备 If you are reading this document, chances are that the Android application that you want to test has not been published yet. If you have a copy of the APK file, you can install the APK onto a test device by using the adb tool. To learn how to install an APK file using the adb tool, see the adb documentation. 若你阅读本文档,可能意味着待测的安卓应用还没有发布。若有有APK文件的安装文件,你可以使用adb工具安装APK文件到待测设备上。为了解更多如何安装APK的知识请参考ADB文档。 Identify the application’s UI components 识别待测应用的UI组件 Before writing your uiautomator tests, first identify the UI components in the application that you want to test. Typically, good candidates for testing are UI components that are visible and that users can interact with. The UI components should also have visible text labels, android:contentDescription values, or both. You can inspect the visible screen objects in an application conveniently by using the uiautomatorviewer tool. For more information about how to analyze an application screen with this tool, see the section Analyzing Your Application’s UI . For more information about the common types of UI components provided by Android, see User Interface . 在编写uiautomator测试前,首先识别待测应用的UI组件。一般来说,适合测试的是可以访问的,用户可以交互的UI组件。UI组件还应该包含可见的文本标签, android:contentDescription 值,或全部。可以使用uiautomatorviewer工具来方便地查看待测应用在屏幕上的 可见 对象。关于如何使用该工具分析应用屏幕的更多信息,可以参考本文档的相应部分。关于安卓提供的通用类型的UI组件,可以参考安卓官方文档。 Ensure that the application is accessible 确保待测应用可访问 This step is required because the uiautomator tool depends on the accessibility features of the Android framework to execute your functional UI tests. You should include these minimum optimizations to support the uiautomator tool: 本步骤非常必要,因为uiautomator工具依赖安卓的可访问特性来执行功能界面测试。你应该在你的程序中加入下述简单的优化来支持uiautomator测试框架:
For more information about implementing and testing accessibility, see Making Applications Accessible . 关于实现和测试可访问性的更多信息,可以参考android官方站点。 Note: To identify the non-accessible components in the UI, click on the Toggle NAF Nodes option in theuiautomatorviewer tool. Generally, Android application developers get accessibility support for free, courtesy of the View and ViewGroup classes. However, some applications use custom view components to provide a richer user experience. Such custom components won’t get the accessibility support that is provided by the standard Android UI components. If this applies to your application, ensure that the application developer exposes the custom drawn UI components to Android accessibility services, by implementing the AccessibilityNodeProvider class. For more information about making custom view components accessible, see Making Applications Accessible . 备注:为识别不可访问的UI组件,使用uiautomatorviewer工具的的Toggle NAF Nodes选项。通常,android应用开发者不需要考虑是否支持可访问性,既然UI组件继承View和ViewGroup类。然而,对于使用定制试图 组件提供用户体验的应用,无法获得自由标准androidUI组件才有的可访问性支持。如果你的测试项目有类似情况,确保应用开发者通过实现 AccessibilityNodeProvider 接口类来 暴露定制组件给android可访问性服务。关于更多信息,可以参考android官方站点。 Configure your development environment 配置测试开发环境 If you’re developing in Eclipse, the Android SDK provides additional tools that help you write test cases using uiautomatorand buiild your JAR file. In order to set up Eclipse to assist you, you need to create a project that includes the uiautomatorclient library, along with the Android SDK library. To configure Eclipse: 若使用eclipse进行开发,android sdk提供额外的工具帮助开发者编写、构建uiautomator测试用例。首先需要创建一个包括uiautomator客户端库文件和android sdk库的测试项目,步骤如下:
If you did not configure Eclipse as your development environment, make sure that the uiautomator.jar and android.jarfiles from the <android-sdk>/platforms/<sdk> directory are in your Java class path. Once you have completed these prerequisite tasks, you’re almost ready to start creating your uiautomator tests. 若测试环境开发环境部署eclipse,确保 <android-sdk>/platforms/<sdk>目录下的 uiautomator.jar 和android.jar在类路径中。一旦完成上述配置,就可以开始编写uiautomator测试用例了。 Creating uiautomator Tests 创建uiautomator测试用例 To build a test that runs in the uiautomator framework, create a test case that extends the UiAutomatorTestCase class. In Eclipse, the test case file goes under the src directory in your project. Later, you will build the test case as a JAR file, then copy this file to the test device. The test JAR file is not an APK file and resides separately from the application that you want to test on the device. Because the UiAutomatorTestCase class extends junit.framework.TestCase, you can use the JUnit Assert class to test that UI components in the app return the expected results. To learn more about JUnit, you can read the documentation on the home page. The first thing your test case should do is access the device that contains the target app. It’s also good practice to start the test from the Home screen of the device. From the Home screen (or some other starting location you’ve chosen in the target app), you can use the classes provided by the uiautomator API to simulate user actions and to test specific UI components. For an example of how to put together a uiautomator test case, see the sample test case . 为创建使用uiautomator测试框架的测试,创建继承UiAutomatorTestCase类的测试用例即可。在eclipse中,测试用例文件 维护在项目的src文件中。稍后,会被测试用例构建为jar文件,然后复制jar文件到待测设备。jar文件不是APK文件,独立存在于待测设备上的待测 应用。因为 the UiAutomatorTestCase 继承 junit.framework.TestCase,可以使用junit断言类Assert来验证UI元素放火期望的结 果。为了解更多关于JUnit的姿势,可以访问站点。创建测试用例时,首先应该访问包含待测应用的待测设备。最佳实践是,从设备的主屏 幕开始运行测试。从主屏幕开始,你能使用uiautomator提供的API类来模拟用户动作、检测UI组件。本文提供一个例子来演示如何使用 uiautomator执行测试。见下文。 uiautomator API uiautomator 编程接口API The uiautomator API is bundled in the uiautomator.jar file under the <android-sdk>/platforms/ directory. The API includes these key classes that allow you to capture and manipulate UI components on the target app: 包含uiautomator API的 uiautomator.jar位于 <android-sdk>/platforms/ 目录下,uiautomator API包含关键的类,可以用来捕获和操控待测安卓应用的UI组件。 Represents the device state. In your tests, you can call methods on the UiDevice instance to check for the state of various properties, such as current orientation or display size. Your tests also can use the UiDevice instance to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons. UiDevice代表设备状态。在测试时,可以调用UiDevice实例的方法来检查不同属性的状态,如当前的屏幕旋转方向货展示大小。测试代码还能使用UiDevice实例来执行设备级的操作,如强制设备横竖屏,按压d-pad硬件按钮,或按压主屏幕键和菜单键。 To get an instance of UiDevice and simulate a Home button press: 获取UiDevice实例,模拟按压主屏幕键的代码如下: getUiDevice (). pressHome ();
UiSelector
UiObject
The following code example shows how to construct UiObject instances that represent a Cancel button and a OKbutton in your application. UiObject代表一个UI元素。为创建一个UiObject实例,使用用来描述如何搜索、选定UI元素的UiSelector实例:
UiObject
cancelButton
=
new
UiObject
(
new
UiSelector
().
text
(
“Cancel”
));
You can reuse the UiObject instances that you have created in other parts of your app testing, as needed. Note that the uiautomator test framework searches the current display for a match every time your test uses a UiObject instance to click on a UI element or query a property. In the following code example, the uiautomator test framework searches for a UI element with the text property OK. If a match is found and if the element is enabled, the framework simulates a user click action on the element.
必要时,可以重用测试项目中已经创建的UiObject实例。注意,测试用例每次使用UiObject实例来点击UI元素或查询属性
时,uiautomator测试框架会搜索当前的界面来寻找匹配。在下面的代码中,uiautomator测试框架搜索带有OK文本属性的UI元素。若发
现匹配,并且该元素启用,框架会模拟用户的在该元素上的点击操作。
You can also restrict the search to find only elements of a specific class. For example, to find matches of the Button class:
还可以限制搜索在几个特定的类中寻找元素,例如,为发现Button类的匹配:
UiCollection
UiCollection
videos
=
new
UiCollection
(
new
UiSelector
()
If the videos are listed within a LinearLayout view, and you want to to retrieve the number of videos in this collection:
如果视频专辑包含在LinearLayout视图下,你能获取视频集合的数目:
If you want to find a specific video that is labeled with the text element Cute Baby Laughing from the collection and simulate a user-click on the video:
若想从视频集合中寻找带有文本元素 Cute Baby Laughing的标签的视频,然后模拟用户在该视频上进行点击,可使用如下代码:
Similarly, you can simulate other user actions on the UI object. For example, if you want to simulate selecting a checkbox that is associated with the video:
类似的,还能模拟其他用户操作,如,如想模拟选定一个关联视频的多选框,可以使用如下代码:
UiScrollable
For example, the following code shows how to simulate scrolling down the Settings menu and clicking on an About tablet option:
UiScrollable代码可滑动的UI元素集合。可以使用UiScrollable类来模拟界面的横竖屏的滑动。该技术可以应用于界面元素隐藏在屏幕外,可以通过滑动来展示的情况下。例如,下面的代码演示如何模拟下滑设置按钮,然后点击About tablet选项。
For more information about these APIs, see the
uiautomator
reference.
更多关于API的信息,请参考官方参考文档。
The following code example shows a simple test case which simulates a user bringing up the Settings app in a stock Android device. The test case mimics all the steps that a user would typically take to perform this task, including opening the Home screen, launching the All Apps screen, scrolling to the Settings app icon, and clicking on the icon to enter the Settings app.
下面的代码例子演示一个简单的测试用例,它可以用来模拟用户在一个安卓设备上启动设置Settings应用。该测试用例模拟用户完成这样的场景的所有步骤,包括打开主屏幕,启动全部应用All Apps
屏幕,滑动到设置应图标上,点击该图标进入设置应用。
// Import the uiautomator libraries
public class LaunchSettings extends UiAutomatorTestCase { public void testDemo () throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
// We’re now in the home screen. Next, we want to simulate
// Simulate a click to bring up the All Apps screen.
// In the All Apps screen, the Settings app is located in
// Simulate a click to enter the Apps tab.
// Next, in the apps tabs, we can simulate a user swiping until
// Set the swiping mode to horizontal (the default is vertical)
// Create a UiSelector to find the Settings app and simulate
// Validate that the package name is the expected one Building and Deploying Your uiautomator Tests 构建和部署uiautomator测试 Once you have coded your test, follow these steps to build and deploy your test JAR to your target Android test device: 完成测试编码后,依据下面的步骤来构建和部署你的测试jar文件到android测试设备上:
Running uiautomator Tests 运行uiautomator测试用例
Here’s an example of how to run a test that is implemented
in the LaunchSettings.jar file. The tests are bundled in
thecom.uia.example.my package:
To learn more about the syntax, subcommands, and options for uiautomator, see the uiautomator reference. 更多关于uiautomator的语法语义,子命令或选项,请参考uiautomator的官方引用文档。 Best Practices 最佳实践 Here are some best practices for functional UI testing with the uiautomator framework: 下面是一些使用uiautomator测试框架进行功能界面测试的最佳实践:
除非注明,文章均为
LoggingSelenium网站
|
|
来自: 白雪~~~ > 《APP自动化测试》