1. InstrumentationRegistry類
1.1. 類說明:
一個暴露的注冊實例,持有instrumentation運行的進程和參數,還提供了一種簡便的方法調用instrumentation, application context和instrumentation參數。
1.2 相關API
返回類型 | API |
static Bundle | getArguments(): 返回一個instrumentation參數副本 |
static Context | getContext(): 返回instrumentation對應包的Context |
static Instrumentation | getInstrumentation(): 返回當前運行的instrumentation |
static Context | getTargetContext(): 返回一個目標應用程序的Context |
static void | registerInstance(Instrumentation instrumentation, Bundle arguments):記錄或暴露當前instrumentation運行和instrumentation參數包的副本,存儲在注冊中心 |
1.3 簡單示例
1.3.1
package com.test.auto.hellouiautomator; import android.app.Instrumentation; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.UiDevice; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import java.io.IOException; /** * Created by auto on 16/3/3. */ @RunWith(AndroidJUnit4.class) public class TestClass01 { public UiDevice mDevice; public Instrumentation instrumentation; @Before public void setUp(){ instrumentation = InstrumentationRegistry.getInstrumentation(); mDevice = UiDevice.getInstance(instrumentation); } @Test public void testCase01() throws IOException { //獲取運行時的參數 Bundle args = InstrumentationRegistry.getArguments(); //輸出到運行報告中 instrumentation.sendStatus(100, args); //獲取測試包的Context Context testContext = InstrumentationRegistry.getContext(); //獲取被測應用的Context Context testedContext = InstrumentationRegistry.getTargetContext(); //通過Context來啟動一個Activity,e.g.瀏覽器 String url = "https://www.baidu.com"; Intent i1 = new Intent(Intent.ACTION_VIEW); i1.setData(Uri.parse(url)); i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); testContext.startActivity(i1); //通過目標Context來啟動撥號功能 Intent i2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + 10086)); i2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); testedContext.startActivity(i2); Bundle inputBundle = new Bundle(); inputBundle.putString("key", "value"); //注入一個Bundle InstrumentationRegistry.registerInstance(instrumentation, inputBundle); //獲取運行參數 Bundle outBundle = InstrumentationRegistry.getArguments(); //輸出到結果報告中 instrumentation.sendStatus(110,outBundle); } }
1.3.2 運行結果
am instrument -w -r -e test 123 com.test.auto.hellouiautomator
2. UiDevice新增API
2.1 API 介紹
返回類型 | API |
void | dumpWindowHierarchy(OutPutStream out): 獲取當前頁面層級到輸出流 |
String | executeShellCommand(String cmd): 執行一個shell命令。備注:此方法只支持api21以上,手機需要5.0系統以上 |
UiObject2 | findObject(BySelector selector): 返回第一個匹配條件的對象 |
UiObject | findObject(UiSelector selector): 返回一個匹配條件的代表視圖的UiObject對象 |
List<UiObject2> | findObjects(BySelector selector): 返回所有匹配條件的對象 |
<R> R | wait(SearchCondition<R> condition, long timeout): 等待的條件得到滿足 |
2.2 代碼示例
package com.test.auto.hellouiautomator; import android.app.Instrumentation; import android.os.Bundle; import android.os.Environment; 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.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.support.test.uiautomator.Until; import android.widget.TextView; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; /** * Created by auto on 16/3/3. */ @RunWith(AndroidJUnit4.class) public class TestClass01 { public UiDevice mDevice; public Instrumentation instrumentation; @Before public void setUp(){ instrumentation = InstrumentationRegistry.getInstrumentation(); mDevice = UiDevice.getInstance(instrumentation); } @Test public void testCase01() throws IOException, UiObjectNotFoundException { //dumpWindowHierarchy(OutPutStream out) File file = new File(Environment.getExternalStorageDirectory()+File.separator+"dump.xml"); if(file.exists()){ file.delete(); } file.createNewFile(); OutputStream outputStream = new FileOutputStream(file); mDevice.dumpWindowHierarchy(outputStream); //executeShellCommand(String cmd) mDevice.executeShellCommand("am start -n com.tencent.mobileqq/.activity.SplashActivity "); //findObject(BySelector selector) mDevice.wait(Until.findObject(By.text("聯系人")),2000); UiObject2 uiObject2 = mDevice.findObject(By.text("聯系人")); uiObject2.click(); //findObject(UiSelector selector) UiObject uiObject = mDevice.findObject(new UiSelector().text("短信")); uiObject.click(); //findObjects(BySelector selector) List <UiObject2> uiObject21 = mDevice.findObjects(By.clazz(TextView.class)); Bundle bundle = new Bundle(); for (UiObject2 a:uiObject21) { bundle.putString("TextView", a.getText()); } instrumentation.sendStatus(123,bundle); } }
2 UiAutomator2 - UiObject2

3 UiObject2 API
返回 | API [ 基礎動作 ] | 說明 |
void | clear() | 清除編輯框里的內容 |
void | click() | 點擊一個對象 |
<R> R | clickAndWait(EventCondition<R> condition, long timeout) | 點擊一個對象然后等待在超時時間內條件成立則通過,否則拋出異常 |
void | drag(Point dest) | 拖拽這個對象到指定位置 |
void | drag(Point dest, int speed) | 自定義速度拖拽這個對象到指定位置,速度:像素數/秒 |
void | longClick() | 執行一個長時間點擊這個對象動作 |
boolean | scroll(Direction direction, float percent) | 對該對象執行一個滾動動作 |
boolean | scroll(Direction direction, float percent, int speed) | 自定義滾動速度對該對象執行一個滾動動作 |
void | legacySetText(java.lang.String text) | 設置文本內容通過發送keycode |
void | setText(java.lang.String text) | 設置文本內容如果這個對象是一個可編輯的字段 |
返回 | API [ 手勢 ] | 說明 |
void | pinchClose(float percent) | 對該對象執行關閉手勢 |
void | pinchClose(float percent, int speed) | 自定義速度對該對象執行關閉手勢 |
void | pinchOpen(float percent) | 對該對象執行打開手勢 |
void | pinchOpen(float percent, int speed) | 自定義速度對該對象執行打開手勢 |
boolean | fling(Direction direction) | 對該對象執行一個滑動的手勢,Direction代表從哪個方向作為起點 |
boolean | fling(Direction direction, int speed) | 自定義速度對對象執行一個滑動的手勢 |
void | swipe(Direction direction, float percent) | 執行一個滑動手勢 |
void | swipe(Direction direction, float percent, int speed) | 執行一個滑動手勢,可定義滑動速度 |
void | setGestureMargin(int margin) | 以像素為單位設置邊緣用於手勢 |
void | setGestureMargins(int left, int top, int right, int bottom) | 以像素為單位設置邊緣用於手勢 |
返回 | API [ 獲取屬性 ] | 說明 |
String | getApplicationPackage() | 返回應用程序的包名稱 |
String | getClassName() | 返回對象的類名稱 |
String | getContentDescription() | 返回該對象的內容描述 |
String | getResourceName() | 返回這個對象的完全限定的資源名的id |
String | getText() | 返回此對象的文本值 |
Rect | getVisibleBounds() | 返回該對象的可見范圍在屏幕坐標 |
Point | getVisibleCenter() | 返回一個指向這個對象的可見范圍的中心 |
返回 | API [ 屬性判斷 ] | 說明 |
boolean | isCheckable() | 返回此對象是否具有checkable屬性 |
boolean | isChecked() | 返回此對象是否具有checked屬性 |
boolean | isClickable() | 返回此對象是否具有clickable屬性 |
boolean | isEnabled() | 返回此對象是否具有enabled屬性 |
boolean | isFocusable() | 返回此對象是否具有focusable屬性 |
boolean | isFocused() | 返回此對象是否具有focused屬性 |
boolean | isLongClickable() | 返回此對象是否具有longclickable屬性 |
boolean | isScrollable() | 返回此對象是否具有scrollable屬性 |
boolean | isSelected() | 返回此對象是否具有selected屬性 |
返回 | API [ 獲取子元素 ] | 說明 |
UiObject2 | findObject(BySelector selector) | 搜索所有的元素在這個對象層級之下,並返回第一個對象與搜索條件相匹配 |
List<UiObject2> | findObjects(BySelector selector) | 搜索所有的元素在這個對象層級之下,並返回所有對象與搜索條件相匹配 |
List<UiObject2> | getChildren() | 返回這個對象下的直接子元素的集合 |
UiObject2 | getParent() | 返回該對象的父類 |
int | getChildCount() | 返回這個對象直屬子元素的數量 |
boolean | equals(java.lang.Object object) | 比較兩個對象是否相等 |
int | hashCode() | 獲取對象的hashCode |
boolean | hasObject(BySelector selector) | 返回該對象是否存在 |
void | recycle() | 回收這個對象 |
<R> R | wait(SearchCondition<R> condition, long timeout) | 等待的條件得到滿足 |
<R> R | wait(UiObject2Condition<R> condition, long timeout) | 等待的條件得到滿足 |