1. 程式人生 > >單元測試 -- UIAutomator 2.0

單元測試 -- UIAutomator 2.0

UI 自動化測試

聽說可以模擬螢幕操作, 感覺挺有意思的, 有機會就學了一下;
// 今天試了下, 模擬點選螢幕, 可惜一秒只可以點選5~6次, (ノ ̄(エ) ̄)ノ

新增依賴

    androidTestCompile 'com.android.support.test:runner:0.4'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' //defaultConfig內新增 android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
import
android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.Until; import
android.util.Log; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertNotNull; /** * Created by cold on 16/9/10. */ @RunWith(AndroidJUnit4.class)//@RunWith別忘了 public class UITest { private String TAG = "UITest"; private UiDevice uiDevice; private Context gContext; private Context currContext; private final long timeout = 2000; @Before public void init() { gContext = InstrumentationRegistry.getContext(); currContext = InstrumentationRegistry.getTargetContext(); uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); Log.w(TAG, "init"); } @Test public void testUI() { uiDevice.findObject(By.text("設定")).click();//從display區域中找到包含"設定"的控制元件, 點選 UiObject2 o1 = uiDevice.wait(Until.findObject(By.text("藍芽")), timeout);//wait 等待 timeout之後在執行第一個引數 assertNotNull(o1); o1.click(); } }

執行的時候, 在該類上右擊run’類名’

API

  • pressBack() 模擬按下返回按鈕
  • pressHome() 模擬按下主選單鍵
  • sleep() 熄屏
  • wakeUp() 喚醒螢幕
  • isScreenOn() 檢查螢幕是否開啟
  • getDisplayWidth() 獲取display區域寬度
  • getDisplayHeight() 獲取display區域高度
  • click(int x, int y) 點選指定座標
  • getCurrentPackageName() 獲取當前應用包名
  • getCurrentActivityName() 獲取當前 Activity 名稱
  • findObject(BySelector selector) 通過 BySelector 找到 Object
  • wait(SearchCondition condition, long timeout) 等待 timeout 後執行 condition
  • swipe(int startX, int startY, int endX, int endY, int steps) 模擬滑動螢幕
  • drag(int startX, int startY, int endX, int endY, int steps) 模擬拖動
  • checkable(boolean isCheckable)
  • checked(boolean isChecked)
  • clazz(String packageName, String className)
  • clazz(String className)
  • depth(int min, int max)
  • desc(String contentDescription)
  • pkg(String applicationPackage)
  • res(String resourcePackage, String resourceId)

By
By的方法都是static方法, 用來生成一個BySelector;

  • desc(String contentDescription)
  • pkg(Pattern applicationPackage)
  • ……
  • getContext() 測試程式的Context
  • getTargetContext() 目標程式的Context
  • click() 模擬單擊這個物件
  • click(long duration) 點選時間
  • clickAndWait(EventCondition condition, long timeout)
  • findObject(BySelector selector)
  • getChildCount() 獲取子view數量
  • getClassName()
  • getParent()
  • isCheckable()
  • isClickable()
  • isFocused()
  • recycle() 回收這個物件
  • wait(UiObject2Condition condition, long timeout)

Until

Until類裡都是靜態方法 返回 UiObject2Condition<> 物件

  • clickable(boolean isClickable)
  • focused(boolean isFocused)
  • ………

指令

adb shell uiautomator dump      生成當前頁面的xml格式ui層次描述至預設路徑
adb shell uiautomator dump [file]   生成當前頁面的xml格式ui層次描述至指定路徑

adb shell pm list instrumentation   檢視device上已存在的uiautomator
adb shell am instrument -w     無引數執行所有測試
adb shell am instrument -w -e func true     有引數執行所有功能測試
adb shell am instrument -w -e unit true     有引數執行所有單元測試
adb shell am instrument -w -e class         執行一個獨立的測試例子

// demo
Running all tests: adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner

Running all small tests: adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunner

Running all medium tests: adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunner

Running all large tests: adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunner

Filter test run to tests with given annotation: adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

Filter test run to tests without given annotation: adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

// 執行單個用例, 用例資訊通過"adb shell pm list instrumentation"查詢
Running a single testcase: adb shell am instrument -w -e class com.android.foo.FooTest com.android.foo/android.test.InstrumentationTestRunner

Running a single test: adb shell am instrument -w -e class com.android.foo.FooTest#testFoo com.android.foo/android.test.InstrumentationTestRunner

Running multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/android.test.InstrumentationTestRunner

Running all tests in a java package: adb shell am instrument -w -e package com.android.foo.subpkg com.android.foo/android.test.InstrumentationTestRunner

Including performance tests: adb shell am instrument -w -e perf true com.android.foo/android.test.InstrumentationTestRunner

To debug your tests, set a break point in your code and pass: -e debug true

To run in 'log only' mode -e log true This option will load and iterate through all test classes and methods, but will bypass actual test execution. Useful for quickly obtaining info on the tests to be executed by an instrumentation command.

To generate EMMA code coverage: -e coverage true Note: this requires an emma instrumented build. By default, the code coverage results file will be saved in a /data//coverage.ec file, unless overridden by coverageFile flag (see below)

To specify EMMA code coverage results file path: -e coverageFile /sdcard/myFile.ec 
in addition to the other arguments.