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