はじめに.
2015.03.13, UIAutomator2.0のリリースアナウンスがあった.  
We’re pleased to announce the release of UIAutomator 2.0!  - Google+
UIAutomator2.0はAndroid Instrumentationベースに生まれ変わり, ./gradlew connectedCheckコマンドでテスト実行することができるようになった. 
UIAutomator2.0はAndroid Testing Support Libraryにパッケージされた. UIAutomator2.0の導入にはSDK ManagerからAndroid Support Repositoryをダウンロードすること.
UIAutomatorはAndroid Developersサイトでも記載されている.  
Testing Support Library - Android Developers
NOTE
類似のUI Testing Framework “Espresso”は1つのアプリに絞ったTesting Frameworkであるため, きめ細やかな制御が可能.
一方, “UI Automator”は複数のアプリを跨いだクロスアプリケーションテストが可能
UIAutomatorのAPI ReferenceおよびSample codeは下記.  
 - android.support.test - Android Developers 
 - googlesamples android-testing - GitHub
UIAutomatorは Requires Android 4.3 (API level 18) or higher.
準備
まずはbuild.gradleを編集する.
android {
    defaultConfig {
        minSdkVersion 18  // Requires Android 4.3 (API level 18)
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    packagingOptions {
        exclude 'LICENSE.txt'  // Duplicate files copied
    }
}
dependencies {
    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    // UiAutomator Testing
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
}現状, そのままビルドするとLICENSE.txtの重複コピーでエラーが出るためpackagingOptionsを指定する. minSdkVersionの設定と必要なライブラリが揃えばandroidTestフォルダにTestCaseを持つクラスを追加する.  
テストクラスのひな形は下記. 
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ApplicationTest {
}サンプルコードを参考にすればすぐにでもUIテストを開始できる.  
下記はランチャーからテスト対象のアプリを起動するまでのSetup処理. 
// Copyright 2015, The Android Open Source Project
@Before
public void startMainActivityFromHomeScreen() {
        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        // Start from the home screen
        mDevice.pressHome();
        // Wait for launcher
        final String launcherPackage = getLauncherPackageName();
        assertThat(launcherPackage, notNullValue());
        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
        // Launch the blueprint app
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
        context.startActivity(intent);
        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
    }APIs
UIAutomatorではデバイスへのファサードとしてUiDeviceクラスを定義している.  
UiDeviceクラスを使えは次のことも実現できる. 
- Change the device rotation
- Press a D-pad button
- Press the Back, Home, or Menu buttons
- Open the notification shade
- Take a screenshot of the current window
UIAutomator APIには次のクラスが定義される.
- UiCollection: Enumerates a container’s UI elements for the purpose of counting, or targeting sub-elements by their visible text or content-description property.
- UiObject: Represents a UI element that is visible on the device.
- UiScrollable: Provides support for searching for items in a scrollable UI container.
- UiSelector: Represents a query for one or more target UI elements on a device.
- Configurator: Allows you to set key parameters for running UI Automator tests.
これらを駆使してUIテストシナリオを作成していく.
以上.