久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Android uiautomator 使用入門官方教程

 白雪~~~ 2015-01-08

Android uiautomator 使用入門官方教程

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

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ù)地運行測試用例,。
Overview 概述

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

Android SDK提供下述工具;來支持自動化的功能界面測試:

  • uiautomatorviewer – A GUI tool to scan and analyze the UI components of an Android application. 掃描,、分析待測應(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)建定制功能界面測試API和自動化運行測試用例的引擎的JAVA類庫,。

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測試框架的工作流程

Here’s a short overview of the steps required to automate UI testing:

簡單介紹下UI自動化測試的主要步驟:

  • 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. 安裝待測應(yīng)用到待測設(shè)備,,準備測試,,分享待測應(yīng)用的UI組件,,區(qū)別待測應(yīng)用可以被uiautomator測試框架訪問,。
  • Create automated tests to simulate specific user interactions on your application. 創(chuàng)建自動化測試來模擬指定的用戶和待測應(yīng)用的交互
  • Compile your test cases into a JAR file and install it on your test device along with your app. 編譯測試用例成jar文件,安裝到裝有待測應(yīng)用的待測設(shè)備上
  • Run the tests and view the test results.  執(zhí)行測試,、檢查測試結(jié)果
  • Correct any bugs or defects discovered in testing .修正測試時發(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.
在寫測試用例之前,,你最好熟悉待測應(yīng)用的UI組件(包括視圖views和控件controls)。 uiautomatorviewer可以幫助你實現(xiàn)這一點,,uiautomatorviewer獲取當前UI界面的快照,,提供一個可視化的界面,來檢查布 局層次,、查看每一個顯示在設(shè)備上的UI組件的屬性,。在以后的uiautomator測試中,你可以利用uiautomator提供的信息來選擇特定的UI 組件,。

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:

  • 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.

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


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

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

  • Use the  android:contentDescription  attribute to label the  ImageButton ImageView CheckBox  and other user interface controls.
  • 使用  android:contentDescription 屬性作為  ImageButtonImageViewCheckBox 和其他界面控件的標簽
  • 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).
  • 用于反饋給用戶信息(如狀態(tài)、聲明信息)的控件的圖標應(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)時所有的UI元素也是可訪問的,,
  • 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工具訪問,。你還能通過打開如TalkBack 或Explore by Touch的可訪問服務(wù),使用方向控制鍵盤來測試應(yīng)用的可訪問性,。

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庫的測試項目,步驟如下:

  • 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)建一個eclipse java項目,,注意命名,,如: “MyAppNameTests”。在項目中,,編寫測試用例來測試指定的待測應(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: 在項目資源管理器視圖下,右擊新建的項目,,選擇屬性-java 構(gòu)建路徑,,完成下述設(shè)置:
    • Click Add Library > JUnit then select JUnit3 to add JUnit support.  添加junit3類庫,支持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. 添加類庫 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.

若測試環(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組件,。

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),。在測試時,可以調(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
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代表一種搜索標準,,可以在當前展示界面上查詢和獲取特定元素的句柄。若找到多于一個的匹配元素,,則返回布局層次結(jié)構(gòu)上的第一個匹配元 素作為目標UiObject,。當構(gòu)造一個UiSelector對象時,可以使用鏈式調(diào)用多個屬性來縮小查詢范圍,。如無匹配元素,,則返回異常 UiAutomatorObjectNotFoundException 。你還可以使用 childSelector()  方法來嵌套多個Uiselector實例,。例如,。下面的代碼演示如何制定查詢來定位在當前界面的第一個ListView,然后在返回的ListView內(nèi)定位一個帶有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代表一個UI元素,。為創(chuàng)建一個UiObject實例,使用用來描述如何搜索,、選定UI元素的UiSelector實例:

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.

必要時,,可以重用測試項目中已經(jīng)創(chuàng)建的UiObject實例。注意,,測試用例每次使用UiObject實例來點擊UI元素或查詢屬性 時,,uiautomator測試框架會搜索當前的界面來尋找匹配。在下面的代碼中,,uiautomator測試框架搜索帶有OK文本屬性的UI元素,。若發(fā) 現(xià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:

還可以限制搜索在幾個特定的類中尋找元素,例如,為發(fā)現(xiàn)Button類的匹配:
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代表元素條目的集合,,例如音樂專輯中的歌曲或郵箱收件箱列表,。類似UiObject,需要指定 UiSelector來構(gòu)造UiCollection,。 用于構(gòu)造UiCollection的UiSelector一般搜索容器或包裹器類的界面元素,,這樣的容器或包裹器類的界面元素包含其他子UI元素,例如包 含子元素的布局視圖,。下面舉例說明,,下面的代碼片段演示如何構(gòu)造一個UiCollection實例,該實例代表一個包含在FrameLayout布局中的 視頻專輯,。

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:

如果視頻專輯包含在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的標簽的視頻,然后模擬用戶在該視頻上進行點擊,,可使用如下代碼:
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:

類似的,,還能模擬其他用戶操作,如,,如想模擬選定一個關(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代碼可滑動的UI元素集合??梢允褂肬iScrollable類來模擬界面的橫豎屏的滑動,。該技術(shù)可以應(yīng)用于界面元素隱藏在屏幕外,可以通過滑動來展示的情況下,。例如,下面的代碼演示如何模擬下滑設(shè)置按鈕,,然后點擊About tablet選項,。
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的信息,請參考官方參考文檔,。
A sample uiautomator test case  uiautomator測試用例的一個例子

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)用,。
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)在在主屏幕上,接下來,,需要模擬用戶進入全部應(yīng)用的屏幕上,。
//若你使用uiautomatorviewer工具捕獲主屏幕的快照,注意一下”全部應(yīng)用All Apps“的按鈕的content-description屬性值為“Apps”.
//我們使用該屬性創(chuàng)建一個UiSelector 對象來定位該按鈕,。

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

// Simulate a click to bring up the All Apps screen.
//模擬點擊進入全部應(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選項卡內(nèi),。為模擬用戶進入該選項卡,我們創(chuàng)建一個UiSelector 對象來定位該帶“Apps”文本標簽屬性的選項卡,。
       UiObject  appsTab  =   new   UiObject ( new   UiSelector ()
. text ( “Apps” ));

// Simulate a click to enter the Apps tab.
//模擬點擊進入Apps選項卡
      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.
//接下來在apps選項卡內(nèi),,我們模擬用戶滑動屏幕直至找到設(shè)置應(yīng)用的圖標。
//既然容器視圖可以滑動,,我們可以使用UiScrollable 對象,。

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

// Set the swiping mode to horizontal (the default is vertical)
//社會中滑動模式為水平,默認為垂直滑動
      appViews . setAsHorizontalList ();

// Create a UiSelector to find the Settings app and simulate      
// a user click to launch the app.
//創(chuàng)建一個UiSelector對象來定位設(shè)置應(yī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ù)期一致
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測試

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

  • 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)建配置文件來構(gòu)建要輸出的JAR文件。為了生成構(gòu)建配置文件,,打開一個命令行終端,,運行下面的命令: <android-sdk> /tools/android create uitest-project -n   <name>   -t 1 -p   <path>,其中 <name>是包含uiautomator測試源文件的測試項目名稱,,<path>  是對應(yī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  進入包括build.xml文件的測試項目目錄下,,使用命令ant build構(gòu)建你的測試的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 /  部署生成的測試的JAR文件到待測設(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  運行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:
下面的例子演示如何運行實現(xiàn)在 LaunchSettings.jar文件中的一個測試用例,,該測試用例包含在包 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的語法語義,子命令或選項,,請參考uiautomator的官方引用文檔,。

Best Practices  最佳實踐

Here are some best practices for functional UI testing with the uiautomator framework:

下面是一些使用uiautomator測試框架進行功能界面測試的最佳實踐:

  • 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ù)必要在待測應(yīng)用要運行的各種設(shè)備上驗證待測應(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)在各種通用場景下測試UI界面,,比如忽然來電,、網(wǎng)絡(luò)中斷和用戶切換應(yīng)用到設(shè)備上的其他應(yīng)用等等。

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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多