电竞比分网-中国电竞赛事及体育赛事平台

分享

Android uiautomator 使用入門(mén)官方教程

 白雪~~~ 2015-01-08

Android uiautomator 使用入門(mén)官方教程

本文英文原文 http://developer./tools/testing/testing_ui.html
已把其翻譯為中文,希望各位多多了解uiautomator 安卓自動(dòng)化測(cè)試工具,非常好用,我很喜歡!

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è)試用例。
Overview 概述

The Android SDK provides the following tools to support automated, functional UI testing on your application:

Android SDK提供下述工具;來(lái)支持自動(dòng)化的功能界面測(cè)試:

  • uiautomatorviewer – A GUI tool to scan and analyze the UI components of an Android application. 掃描、分析待測(cè)應(yīng)用的UI組件的圖像工具
  • uiautomator – A Java library containing APIs to create customized functional UI tests, and an execution engine to automate and run the tests. 包含創(chuàng)建定制功能界面測(cè)試API和自動(dòng)化運(yùn)行測(cè)試用例的引擎的JAVA類(lèi)庫(kù)。

To use these tools, you must have the following versions of the Android development tools installed:

為使用這些工具,需要安裝的android工具工具的版本信息:

  • Android SDK Tools, Revision 21 or higher
  • Android SDK Platform, API 16 or higher

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è)試的主要步驟:

  • Prepare to test by installing the app on a test device, analyzing the app’s UI components, and ensuring that your application is accessible by the test automation framework. 安裝待測(cè)應(yīng)用到待測(cè)設(shè)備,準(zhǔn)備測(cè)試,分享待測(cè)應(yīng)用的UI組件,區(qū)別待測(cè)應(yīng)用可以被uiautomator測(cè)試框架訪問(wèn)。
  • Create automated tests to simulate specific user interactions on your application. 創(chuàng)建自動(dòng)化測(cè)試來(lái)模擬指定的用戶(hù)和待測(cè)應(yīng)用的交互
  • Compile your test cases into a JAR file and install it on your test device along with your app. 編譯測(cè)試用例成jar文件,安裝到裝有待測(cè)應(yīng)用的待測(cè)設(shè)備上
  • Run the tests and view the test results.  執(zhí)行測(cè)試、檢查測(cè)試結(jié)果
  • Correct any bugs or defects discovered in testing .修正測(cè)試時(shí)發(fā)現(xiàn)的缺陷

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.
在寫(xiě)測(cè)試用例之前,你最好熟悉待測(cè)應(yīng)用的UI組件(包括視圖views和控件controls)。 uiautomatorviewer可以幫助你實(shí)現(xiàn)這一點(diǎn),uiautomatorviewer獲取當(dāng)前UI界面的快照,提供一個(gè)可視化的界面,來(lái)檢查布 局層次、查看每一個(gè)顯示在設(shè)備上的UI組件的屬性。在以后的uiautomator測(cè)試中,你可以利用uiautomator提供的信息來(lái)選擇特定的UI 組件。

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:

  • Connect your Android device to your development machine.
  • Open a terminal window and navigate to <android-sdk>/tools/.
  • Run the tool with this command: $ uiautomatorviewer
  • To capture a screen for analysis, click the Device Screenshot button in the GUI of the uiautomatorviewer tool.

    Note: If you have more than one device connected, specify the device for screen capture by setting theANDROID_SERIAL environment variable:

    • Find the serial numbers for your connected devices by running this command: $ adb devices
    • Set the ANDROID_SERIAL environment variable to select the device to test:
      • In Windows: set  ANDROID_SERIAL =< device serial number >
      • In UNIX: export  ANDROID_SERIAL =< device serial number >

    If you are connected to only a single device, you do not need to set the ANDROID_SERIAL environment variable.

  • View the UI properties for your application:
    • Hover over the snapshot in the left-hand panel to see the UI components identified by the uiautomatorviewer tool. You can view the component’s properties listed in the lower right-hand panel, and the layout hierarchy in the upper right-hand panel.
    • Optionally, click on the Toggle NAF Nodes button to see UI components that are not accessible to the uiautomatortesting framework. Only limited information may be available for these components.

分析待測(cè)程序的UI組件的步驟:
     1. 將android設(shè)備連接到有開(kāi)發(fā)環(huán)境機(jī)器上
     2. 打開(kāi)命令行終端窗口,進(jìn)入android sdk tool所在目錄
     3. 運(yùn)行以下命令:
           $ uiautomatorviewer
     4. 捕獲到待分析的界面后,點(diǎn)擊設(shè)備快照“Device Screenshot”按鈕
         注意:如果你連接了多個(gè)設(shè)備,需要指定ANDROID_SERIAL 環(huán)境變量,來(lái)說(shuō)明對(duì)哪個(gè)設(shè)備進(jìn)行截屏:
                  a. 運(yùn)行以下命令,找到你的設(shè)備序列號(hào)
                       $ adb devices
                  b. 設(shè)置環(huán)境變量ANDROID_SERIAL
                       在windows上:
                              set ANDROID_SERIAL=<device serial number>
                       在*nix上:
                              export ANDROID_SERIAL=<device serial number>
          如果你只連接一個(gè)設(shè)備,則不需要設(shè)置


      5. 查看應(yīng)用的UI屬性
          1. 在快照的左側(cè)面板上,可以看到uiautomatorviewer顯示的UI組件;在右側(cè),下邊是組件的屬性,上邊是布局的層次
          2. 你也可以點(diǎn)擊”Toggle NAF Nodes“按鈕,來(lái)顯示uiautomator框架無(wú)法訪問(wèn)的UI組件。對(duì)于那些組件,只有有限的屬性信息可以供uiautomator使用。

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è)試框架:

  • Use the  android:contentDescription  attribute to label the  ImageButton ImageView CheckBox  and other user interface controls.
  • 使用  android:contentDescription 屬性作為  ImageButtonImageViewCheckBox 和其他界面控件的標(biāo)簽
  • Provide an  android:hint  attribute  instead  of a content description for  EditText  fields
  • 為  EditText  輸入框提供  android:hint  屬性,而不是 android:contentDescription
  • Associate an  android:hint  attribute with any graphical icons used by controls that provide feedback to the user (for example, status or state information).
  • 用于反饋給用戶(hù)信息(如狀態(tài)、聲明信息)的控件的圖標(biāo)應(yīng)該包含 android:hint 屬性
  • Make sure that all the user interface elements are accessible with a directional controller, such as a trackball or D-pad.
  • 確保在使用方向控制鍵(如trackball或 D-pad)時(shí)所有的UI元素也是可訪問(wèn)的,
  • Use the uiautomatorviewer tool to ensure that the UI component is accessible to the testing framework. You can also test the application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls.
  • 使用uiautomatorviewer工具確保UI組件可以被uiautomator工具訪問(wèn)。你還能通過(guò)打開(kāi)如TalkBack 或Explore by Touch的可訪問(wèn)服務(wù),使用方向控制鍵盤(pán)來(lái)測(cè)試應(yīng)用的可訪問(wèn)性。

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)目,步驟如下:

  • Create a new Java project in Eclipse, and give your project a name that is relevant to the tests you’re about to create (for example, “MyAppNameTests”). In the project, you will create the test cases that are specific to the application that you want to test.  創(chuàng)建一個(gè)eclipse java項(xiàng)目,注意命名,如: “MyAppNameTests”。在項(xiàng)目中,編寫(xiě)測(cè)試用例來(lái)測(cè)試指定的待測(cè)應(yīng)用。
  • From the Project Explorer, right-click on the new project that you created, then select Properties > Java Build Path, and do the following: 在項(xiàng)目資源管理器視圖下,右擊新建的項(xiàng)目,選擇屬性-java 構(gòu)建路徑,完成下述設(shè)置:
    • Click Add Library > JUnit then select JUnit3 to add JUnit support.  添加junit3類(lèi)庫(kù),支持junit。
    • Click Add External JARs… and navigate to the SDK directory. Under the platforms directory, select the latest SDK version and add both the uiautomator.jar and android.jar files. 添加類(lèi)庫(kù) uiautomator.jar 和 android.jar。

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組件。

UiDevice

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
Represents a search criteria to query and get a handle on specific elements in the currently displayed UI. If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. When constructing a  UiSelector , you can chain together multiple properties to refine your search. If no matching UI element is found, a UiAutomatorObjectNotFoundException  is thrown. You can use the  childSelector()  method to nest multiple  UiSelector  instances. For example, the following code example shows how to specify a search to find the first  ListView  in the currently displayed UI, then search within that  ListView  to find a UI element with the text property Apps.
UiSelector代表一種搜索標(biāo)準(zhǔn),可以在當(dāng)前展示界面上查詢(xún)和獲取特定元素的句柄。若找到多于一個(gè)的匹配元素,則返回布局層次結(jié)構(gòu)上的第一個(gè)匹配元 素作為目標(biāo)UiObject。當(dāng)構(gòu)造一個(gè)UiSelector對(duì)象時(shí),可以使用鏈?zhǔn)秸{(diào)用多個(gè)屬性來(lái)縮小查詢(xún)范圍。如無(wú)匹配元素,則返回異常 UiAutomatorObjectNotFoundException 。你還可以使用 childSelector()  方法來(lái)嵌套多個(gè)Uiselector實(shí)例。例如。下面的代碼演示如何制定查詢(xún)來(lái)定位在當(dāng)前界面的第一個(gè)ListView,然后在返回的ListView內(nèi)定位一個(gè)帶有Apps文本屬性的界面元素。
UiObject  appItem  =   new   UiObject ( new   UiSelector ()
. className ( “android.widget.ListView” ). instance ( 1 )
. childSelector ( new   UiSelector (). text ( “Apps” )));

UiObject
Represents a UI element. To create a  UiObject  instance, use a UiSelector that describes how to search for, or select, the UI element.

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” ));
UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ));

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)擊操作。
if ( okButton . exists ()   &&  okButton . isEnabled ())
{
okButton
. click ();
}

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)的匹配:
UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” )
. className ( “android.widget.Button” ));
UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” )
. className ( “android.widget.Button” ));

UiCollection
Represents a collection of items, for example songs in a music album or a list of emails in an inbox. Similar to a UiObject , you construct a  UiCollection  instance by specifying a  UiSelector . The UiSelector  for a  UiCollection should search for a UI element that is a container or wrapper of other child UI elements (such as a layout view that contains child UI elements). For example, the following code snippet shows how to construct a  UiCollection  to represent a video album that is displayed within a  FrameLayout :
UiCollection代表元素條目的集合,例如音樂(lè)專(zhuān)輯中的歌曲或郵箱收件箱列表。類(lèi)似UiObject,需要指定 UiSelector來(lái)構(gòu)造UiCollection。 用于構(gòu)造UiCollection的UiSelector一般搜索容器或包裹器類(lèi)的界面元素,這樣的容器或包裹器類(lèi)的界面元素包含其他子UI元素,例如包 含子元素的布局視圖。下面舉例說(shuō)明,下面的代碼片段演示如何構(gòu)造一個(gè)UiCollection實(shí)例,該實(shí)例代表一個(gè)包含在FrameLayout布局中的 視頻專(zhuān)輯。

UiCollection  videos  =   new   UiCollection ( new   UiSelector ()
. className ( “android.widget.FrameLayout” ));

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ù)目:
int  count  =  videos . getChildCount ( new   UiSelector ()
. className ( “android.widget.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:

若想從視頻集合中尋找?guī)в形谋驹?Cute Baby Laughing的標(biāo)簽的視頻,然后模擬用戶(hù)在該視頻上進(jìn)行點(diǎn)擊,可使用如下代碼:
UiObject  video  =  videos . getChildByText ( new   UiSelector ()
. className ( “android.widget.LinearLayout” ),   “Cute Baby Laughing” );
video
. click ();

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)視頻的多選框,可以使用如下代碼:
UiObject  checkBox  =  video . getChild ( new   UiSelector ()
. className ( “android.widget.Checkbox” ));
if (! checkBox . isSelected ())  checkbox . click ();

UiScrollable
Represents a scrollable collection of UI elements. You can use the  UiScrollable  class to simulate vertical or horizontal scrolling across a display. This technique is helpful when a UI element is positioned off-screen and you need to scroll to bring it into view.

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)。
UiScrollable  settingsItem  =   new   UiScrollable ( new   UiSelector ()
. className ( “android.widget.ListView” ));
UiObject  about  =  settingsItem . getChildByText ( new   UiSelector ()
. className ( “android.widget.LinearLayout” ),   “About  tablet” );
about
. click ()

For more information about these APIs, see the  uiautomator  reference. 更多關(guān)于API的信息,請(qǐng)參考官方參考文檔。
A sample uiautomator test case  uiautomator測(cè)試用例的一個(gè)例子

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)用。
package  com . uia . example . my ;

// Import the uiautomator libraries
import  com . android . uiautomator . core . UiObject ;
import  com . android . uiautomator . core . UiObjectNotFoundException ;
import  com . android . uiautomator . core . UiScrollable ;
import  com . android . uiautomator . core . UiSelector ;
import  com . android . uiautomator . testrunner . UiAutomatorTestCase ;

public   class   LaunchSettings   extends   UiAutomatorTestCase   {

public   void  testDemo ()   throws   UiObjectNotFoundException   {

// Simulate a short press on the HOME button.
//模擬觸壓一下主屏幕鍵
      getUiDevice (). pressHome ();

// We’re now in the home screen. Next, we want to simulate 
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot 
// of the Home screen, notice that the All Apps button’s 
// content-description property has the value “Apps”.  We can 
// use this property to create a UiSelector to find the button.
//現(xiàn)在在主屏幕上,接下來(lái),需要模擬用戶(hù)進(jìn)入全部應(yīng)用的屏幕上。
//若你使用uiautomatorviewer工具捕獲主屏幕的快照,注意一下”全部應(yīng)用All Apps“的按鈕的content-description屬性值為“Apps”.
//我們使用該屬性創(chuàng)建一個(gè)UiSelector 對(duì)象來(lái)定位該按鈕。

       UiObject  allAppsButton  =   new   UiObject ( new   UiSelector ()
. description ( “Apps” ));

// Simulate a click to bring up the All Apps screen.
//模擬點(diǎn)擊進(jìn)入全部應(yīng)用的屏幕
      allAppsButton . clickAndWaitForNewWindow ();

// In the All Apps screen, the Settings app is located in 
// the Apps tab. To simulate the user bringing up the Apps tab,
// we create a UiSelector to find a tab with the text 
// label “Apps”.
//在全部應(yīng)用的屏幕上,設(shè)置應(yīng)用位于Apps選項(xiàng)卡內(nèi)。為模擬用戶(hù)進(jìn)入該選項(xiàng)卡,我們創(chuàng)建一個(gè)UiSelector 對(duì)象來(lái)定位該帶“Apps”文本標(biāo)簽屬性的選項(xiàng)卡。
       UiObject  appsTab  =   new   UiObject ( new   UiSelector ()
. text ( “Apps” ));

// Simulate a click to enter the Apps tab.
//模擬點(diǎn)擊進(jìn)入Apps選項(xiàng)卡
      appsTab . click ();

// Next, in the apps tabs, we can simulate a user swiping until
// they come to the Settings app icon.  Since the container view 
// is scrollable, we can use a UiScrollable object.
//接下來(lái)在apps選項(xiàng)卡內(nèi),我們模擬用戶(hù)滑動(dòng)屏幕直至找到設(shè)置應(yīng)用的圖標(biāo)。
//既然容器視圖可以滑動(dòng),我們可以使用UiScrollable 對(duì)象。

UiScrollable  appViews  =   new   UiScrollable ( new   UiSelector ()
. scrollable ( true ));

// Set the swiping mode to horizontal (the default is vertical)
//社會(huì)中滑動(dòng)模式為水平,默認(rèn)為垂直滑動(dòng)
      appViews . setAsHorizontalList ();

// Create a UiSelector to find the Settings app and simulate      
// a user click to launch the app.
//創(chuàng)建一個(gè)UiSelector對(duì)象來(lái)定位設(shè)置應(yīng)用,模擬用戶(hù)點(diǎn)擊來(lái)啟動(dòng)該應(yīng)用
       UiObject  settingsApp  =  appViews . getChildByText ( new   UiSelector ()
. className ( android . widget . TextView . class . getName ()),
“Settings” );
settingsApp
. clickAndWaitForNewWindow ();

// Validate that the package name is the expected one
//驗(yàn)證包名與預(yù)期一致
UiObject  settingsValidation  =   new   UiObject ( new   UiSelector ()
. packageName ( “com.android.settings” ));
assertTrue
( “Unable to detect Settings” ,
settingsValidation
. exists ());
}
}

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è)備上:

  • Create the required build configuration files to build the output JAR. To generate the build configuration files, open a terminal and run the following command: <android-sdk> /tools/android create uitest-project -n  <name>  -t 1 -p  <path> The <name> is the name of the project that contains your uiautomator test source files, and the <path> is the path to the corresponding project directory.    創(chuàng)建需要的構(gòu)建配置文件來(lái)構(gòu)建要輸出的JAR文件。為了生成構(gòu)建配置文件,打開(kāi)一個(gè)命令行終端,運(yùn)行下面的命令: <android-sdk> /tools/android create uitest-project -n   <name>   -t 1 -p   <path>,其中 <name>是包含uiautomator測(cè)試源文件的測(cè)試項(xiàng)目名稱(chēng),<path>  是對(duì)應(yīng)的測(cè)試項(xiàng)目目錄的路徑。
  • From the command line, set the ANDROID_HOME variable: 在命令行窗口中,設(shè)置ANDROID_HOME變量。
    • In Windows: set  ANDROID_HOME =< path_to_your_sdk >
    • In UNIX: export  ANDROID_HOME =< path_to_your_sdk >
  • Go to the project directory where your build.xml file is located and build your test JAR.  ant build  進(jìn)入包括build.xml文件的測(cè)試項(xiàng)目目錄下,使用命令ant build構(gòu)建你的測(cè)試的JAR文件。
  • Deploy your generated test JAR file to the test device by using the adb push command: adb push  <path_to_output_jar>   / data / local / tmp /  部署生成的測(cè)試的JAR文件到待測(cè)設(shè)備上的命令為:db push command:adb push <path_to_output_jar> /data/local/tmp/
  • Here’s an example: 例子如下:

    adb push  ~ /dev/ workspace / LaunchSettings / bin / LaunchSettings . jar  / data / local / tmp /

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:
下面的例子演示如何運(yùn)行實(shí)現(xiàn)在 LaunchSettings.jar文件中的一個(gè)測(cè)試用例,該測(cè)試用例包含在包 com.uia.example.my中。
adb shell uiautomator runtest  LaunchSettings . jar  - c com . uia . example . my . LaunchSettings

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í)踐:

  • Ensure that you validate the same UI functions on your application across the various types of devices that your application might run on (for example, devices with different screen densities).
  • 務(wù)必要在待測(cè)應(yīng)用要運(yùn)行的各種設(shè)備上驗(yàn)證待測(cè)應(yīng)用的界面功能  (例如,不同屏幕密度的各種設(shè)備)
  • You should also test your UI against common scenarios such as in-coming phone calls, network interruptions, and user-initiated switching to other applications on the device.
  • 還應(yīng)在各種通用場(chǎng)景下測(cè)試UI界面,比如忽然來(lái)電、網(wǎng)絡(luò)中斷和用戶(hù)切換應(yīng)用到設(shè)備上的其他應(yīng)用等等。

除非注明,文章均為 LoggingSelenium網(wǎng)站
原創(chuàng),歡迎轉(zhuǎn)載!轉(zhuǎn)載請(qǐng)注明本文地址,謝謝。本文地址: http:///?p=546
.非常感謝!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多