準備你的專案並編寫第一個 UIAutomator 測試

將所需的庫新增到 Android 模組的 build.gradle 的 dependencies 部分:

android {
...
  defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
}

dependencies {
  ...
  androidTestCompile 'com.android.support.test:runner:0.5'
  androidTestCompile 'com.android.support.test:rules:0.5'
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
  androidTestCompile 'com.android.support:support-annotations:23.4.0'
}

⚠請注意,當然版本的平均時間可能不同。

與此更改同步後。

然後在 androidTest 資料夾中新增一個新的 Java 類:

public class InterAppTest extends InstrumentationTestCase {

  private UiDevice device;

  @Override
  public void setUp() throws Exception {
     device = UiDevice.getInstance(getInstrumentation());
  }

  public void testPressHome() throws Exception {
    device.pressHome();
  }
}

通過右鍵單擊類選項卡並在執行InterAppTest 上執行此測試。