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. 除了對組成安卓應(yīng)用的單獨的組件(如,,activities、services,、和content providers)進行單元測試,,測試應(yīng)用運行時的界面行為也很必要。UI測試確保應(yīng)用在一系列用戶操作后,,如鍵盤輸入,、按壓工具欄、菜單,、對話框,、圖 片或其他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. 功能或黑盒測試不需要測試人員知曉應(yīng)用的內(nèi)部實現(xiàn)細節(jié),,只要明白在一系列用戶操作后返回期望的UI輸出即可。這種測試方法運行開發(fā),、測試角色的可以由組織中的不同團隊來擔任,。 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測試的通用方式是手工運行測試,驗證應(yīng)用如期望般運行,。然而,,此種方法非常耗時、無趣,、易出錯,。更有效、更可靠的方式是使用軟件
測試框架自動化UI測試,。自動化測試涉及創(chuàng)建程序執(zhí)行測試任務(wù)(測試用例)來覆蓋指定的用戶場景,,然后使用測試框架自動化重復(fù)地運行測試用例,。
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 分析應(yīng)用的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捕獲的測試設(shè)備的界面 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 安裝待測應(yīng)用到待測設(shè)備 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. 若你閱讀本文檔,可能意味著待測的安卓應(yīng)用還沒有發(fā)布,。若有有APK文件的安裝文件,,你可以使用adb工具安裝APK文件到待測設(shè)備上,。為了解更多如何安裝APK的知識請參考ADB文檔。 Identify the application’s UI components 識別待測應(yīng)用的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測試前,,首先識別待測應(yīng)用的UI組件,。一般來說,適合測試的是可以訪問的,,用戶可以交互的UI組件,。UI組件還應(yīng)該包含可見的文本標簽, android:contentDescription 值,,或全部,。可以使用uiautomatorviewer工具來方便地查看待測應(yīng)用在屏幕上的 可見 對象,。關(guān)于如何使用該工具分析應(yīng)用屏幕的更多信息,,可以參考本文檔的相應(yīng)部分。關(guān)于安卓提供的通用類型的UI組件,,可以參考安卓官方文檔,。 Ensure that the application is accessible 確保待測應(yīng)用可訪問 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工具依賴安卓的可訪問特性來執(zhí)行功能界面測試,。你應(yīng)該在你的程序中加入下述簡單的優(yōu)化來支持uiautomator測試框架:
For more information about implementing and testing accessibility, see Making Applications Accessible . 關(guān)于實現(xiàn)和測試可訪問性的更多信息,,可以參考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應(yīng)用開發(fā)者不需要考慮是否支持可訪問性,,既然UI組件繼承View和ViewGroup類,。然而,對于使用定制試圖 組件提供用戶體驗的應(yīng)用,,無法獲得自由標準androidUI組件才有的可訪問性支持,。如果你的測試項目有類似情況,確保應(yīng)用開發(fā)者通過實現(xiàn) AccessibilityNodeProvider 接口類來 暴露定制組件給android可訪問性服務(wù),。關(guān)于更多信息,,可以參考android官方站點。 Configure your development environment 配置測試開發(fā)環(huán)境 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進行開發(fā),,android sdk提供額外的工具幫助開發(fā)者編寫,、構(gòu)建uiautomator測試用例,。首先需要創(chuàng)建一個包括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. 若測試環(huán)境開發(fā)環(huán)境部署eclipse,,確保 <android-sdk>/platforms/<sdk>目錄下的 uiautomator.jar 和android.jar在類路徑中,。一旦完成上述配置,就可以開始編寫uiautomator測試用例了,。 Creating uiautomator Tests 創(chuàng)建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 . 為創(chuàng)建使用uiautomator測試框架的測試,,創(chuàng)建繼承UiAutomatorTestCase類的測試用例即可。在eclipse中,,測試用例文件 維護在項目的src文件中,。稍后,會被測試用例構(gòu)建為jar文件,,然后復(fù)制jar文件到待測設(shè)備,。jar文件不是APK文件,獨立存在于待測設(shè)備上的待測 應(yīng)用,。因為 the UiAutomatorTestCase 繼承 junit.framework.TestCase,,可以使用junit斷言類Assert來驗證UI元素放火期望的結(jié) 果。為了解更多關(guān)于JUnit的姿勢,,可以訪問站點,。創(chuàng)建測試用例時,首先應(yīng)該訪問包含待測應(yīng)用的待測設(shè)備,。最佳實踐是,,從設(shè)備的主屏 幕開始運行測試。從主屏幕開始,,你能使用uiautomator提供的API類來模擬用戶動作,、檢測UI組件。本文提供一個例子來演示如何使用 uiautomator執(zhí)行測試,。見下文,。 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包含關(guān)鍵的類,可以用來捕獲和操控待測安卓應(yīng)用的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代表設(shè)備狀態(tài),。在測試時,可以調(diào)用UiDevice實例的方法來檢查不同屬性的狀態(tài),,如當前的屏幕旋轉(zhuǎn)方向貨展示大小,。測試代碼還能使用UiDevice實例來執(zhí)行設(shè)備級的操作,如強制設(shè)備橫豎屏,,按壓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元素,。為創(chuàng)建一個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.
必要時,,可以重用測試項目中已經(jīng)創(chuàng)建的UiObject實例。注意,,測試用例每次使用UiObject實例來點擊UI元素或查詢屬性
時,,uiautomator測試框架會搜索當前的界面來尋找匹配。在下面的代碼中,,uiautomator測試框架搜索帶有OK文本屬性的UI元素,。若發(fā)
現(xiàn)匹配,并且該元素啟用,,框架會模擬用戶的在該元素上的點擊操作,。
You can also restrict the search to find only elements of a specific class. For example, to find matches of the Button class:
還可以限制搜索在幾個特定的類中尋找元素,例如,為發(fā)現(xiàn)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視圖下,,你能獲取視頻集合的數(shù)目:
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:
若想從視頻集合中尋找?guī)в形谋驹?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:
類似的,,還能模擬其他用戶操作,如,,如想模擬選定一個關(guān)聯(lián)視頻的多選框,,可以使用如下代碼:
UiScrollable
For example, the following code shows how to simulate scrolling down the Settings menu and clicking on an About tablet option:
UiScrollable代碼可滑動的UI元素集合??梢允褂肬iScrollable類來模擬界面的橫豎屏的滑動,。該技術(shù)可以應(yīng)用于界面元素隱藏在屏幕外,可以通過滑動來展示的情況下,。例如,下面的代碼演示如何模擬下滑設(shè)置按鈕,,然后點擊About tablet選項,。
For more information about these APIs, see the
uiautomator
reference.
更多關(guān)于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.
下面的代碼例子演示一個簡單的測試用例,,它可以用來模擬用戶在一個安卓設(shè)備上啟動設(shè)置Settings應(yīng)用。該測試用例模擬用戶完成這樣的場景的所有步驟,,包括打開主屏幕,,啟動全部應(yīng)用All Apps
屏幕,滑動到設(shè)置應(yīng)圖標上,,點擊該圖標進入設(shè)置應(yīng)用,。
// 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 構(gòu)建和部署uiautomator測試 Once you have coded your test, follow these steps to build and deploy your test JAR to your target Android test device: 完成測試編碼后,,依據(jù)下面的步驟來構(gòu)建和部署你的測試jar文件到android測試設(shè)備上:
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. 更多關(guān)于uiautomator的語法語義,子命令或選項,,請參考uiautomator的官方引用文檔,。 Best Practices 最佳實踐 Here are some best practices for functional UI testing with the uiautomator framework: 下面是一些使用uiautomator測試框架進行功能界面測試的最佳實踐:
除非注明,,文章均為
LoggingSelenium網(wǎng)站
|
|
來自: 白雪~~~ > 《APP自動化測試》