https://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html
回傳值 | 方法名 | 說明 |
boolean | click (int x, int y) | 點擊螢幕指定座標 |
void | dumpWindowHierarchy (File dest) | 紀錄當前畫面的結構圖(.uix) [指定路徑] [需搭配takeScreenshot()] |
void | dumpWindowHierarchy (OutputStream out) | 紀錄當前畫面的結構圖(.uix) [指定路徑] [需搭配takeScreenshot()] |
UiObject2 | findObject (BySelector selector) | 找尋物件 [UiObject2] |
String | executeShellCommand(String cmd) | 執行 adb shell 指令 |
UiObject | findObject (UiSelector selector) | 找尋物件 [UiObject] |
List<UiObject2> | findObjects (BySelector selector) | 找尋同一種多項物件 [UiObject2] |
void | freezeRotation () | 暫時關閉螢幕翻轉功能 |
String | getCurrentPackageName () | 取得當前的套件名 |
int | getDisplayHeight () | 取得螢幕高度 [單位:PX] |
int | getDisplayRotation () | 取得翻轉狀態 |
Point | getDisplaySizeDp () | 螢幕長寬 [單位:DP] |
int | getDisplayWidth () | 取得螢幕寬度 [單位:PX] |
UiDevice | getInstance (Instrumentation instrumentation) | UiDevice實例化 |
String | getLauncherPackageName () | 取得Launcher的套件名 |
String | getProductName () | 取得設備名 |
boolean | hasAnyWatcherTriggered () | 是否有監聽器觸發過 |
boolean | hasObject (BySelector selector) | 找到指定物件時 |
boolean | hasWatcherTriggered (String watcherName) | 是否有指定的監聽器觸發過 |
boolean | isNaturalOrientation () | 是否直立狀態 |
boolean | isScreenOn () | 螢幕是否亮起 |
boolean | openNotification () | 開啟提示狀態欄 |
boolean | openQuickSettings () | 開啟快速設定欄 |
R | performActionAndWait (Runnable action, EventCondition<R> condition, long timeout) | 等待條件觸發後執行動作接著再等待多少時間 |
boolean | pressBack() | 按下返回鍵 |
boolean | pressDelete () | 按下刪除鍵 |
boolean | pressEnter () | 按下輸入鍵 |
boolean | pressHome () | 按下首頁鍵 |
boolean | pressKeyCode (int keyCode) | 使用按鍵事件 |
boolean | pressKeyCode (int keyCode, int metaState) | 使用按鍵事件 [組合鍵] |
boolean | pressMenu () | 按下菜單鍵 |
boolean | pressRecentApps () | 按下Recent鍵 |
boolean | pressSearch () | 按下搜索鍵 |
void | registerWatcher (String name, UiWatcher watcher) | 設置監聽器 [名字] |
void | removeWatcher (String name) | 移除指定監聽器 [名字] |
void | resetWatcherTriggers () | 重置所有監聽器 [消除所有觸發過的紀錄] |
void | runWatchers () | 強制執行所有監聽器 |
void | setCompressedLayoutHeirarchy (boolean compressed) | 設置Layout是否開啟壓縮模式 [開啟時LayoutHeirarchy會較為精簡] |
void | setOrientationLeft () | 設置螢幕方向向左躺 [暫時鎖住翻轉] |
void | setOrientationNatural () | 設置螢幕方向為直立 [暫時鎖住翻轉] |
void | setOrientationRight () | 設置螢幕方向向右躺 [暫時鎖住翻轉] |
void | sleep () | 使手機睡眠 [Device suspend] |
boolean | swipe (int startX, int startY, int endX, int endY, int steps) | 滑動畫面 [起點/終點/需要的步數] |
boolean | swipe (Point[] segments,int segmentSteps) | 滑動畫面 [Point包含起點和終點/需要的步數] |
boolean | takeScreenshot (File storePath, float scale, int quality) | 擷取手機當前畫面 [檔案路徑/比例/解析度] |
boolean | takeScreenshot (File storePath) | 擷取手機當前畫面 [檔案路徑] |
void | unfreezeRotation () | 取消鎖定畫面翻轉 |
R | wait (SearchCondition<R> condition, long timeout) | 等待某些條件達成 [條件/設定最長等待時間] |
void | waitForIdle (long timeout) | 等待直到畫面閒置 [設定最長等待時間] |
void | waitForIdle () | 等待直到畫面閒置 [默認:10秒] |
boolean | waitForWindowUpdate (String packageName, long timeout) | 等待直到新的畫面出現 [新的package被執行] |
void | wakeUp () | 使手機喚醒 [Device resume] |
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
// 初始化/實例化(重要)
UiDevice uiDevice=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
@Test
public void apitest() throws IOException, UiObjectNotFoundException, RemoteException, InterruptedException {
//點擊螢幕中心座標
int x=uiDevice.getDisplayHeight()/2;
int y=uiDevice.getDisplayHeight()/2;
uiDevice.click(x,y);
//記錄畫面的當前結構圖 (.uix/.png 兩者要搭前來才有辦法查詢)(需要開啟storage權限)
File file=new File(Environment.getExternalStorageDirectory()+"/Pictures/test.uix");
uiDevice.dumpWindowHierarchy(file);
File file2=new File(Environment.getExternalStorageDirectory()+"/Pictures/test.png");
uiDevice.takeScreenshot(file2);
//找尋物件
//UiObject
uiDevice.findObject(new UiSelector().text("test")).click();
//UiObject2
uiDevice.findObject(By.text("test")).click();
//List<UiObject2>
List<UiObject2> list=uiDevice.findObjects(By.text("test"));
for(UiObject2 ui:list){
ui.click();
}
//螢幕畫面左躺5秒 (如果設備是立著的話 腳本執行時有效 一但結束會以當下重心為主 有沒有凍結都沒用 此API本身就有自帶凍結翻轉的功能了)
uiDevice.setOrientationLeft();
Thread.sleep(5*1000);
//畫面是否有此物件且執行動作
if(uiDevice.hasObject(By.text("test")))
uiDevice.pressBack();
else
uiDevice.pressHome();
//執行按鍵事件
uiDevice.pressKeyCode(KeyEvent.KEYCODE_0);
uiDevice.pressKeyCode(KeyEvent.KEYCODE_AT); //@
//判斷手機是否睡眠狀態 (亮著就關 關著就亮)
if(uiDevice.isScreenOn())
uiDevice.sleep();
else
uiDevice.wakeUp();
//滑動螢幕
//坐標法
uiDevice.swipe(100,100,500,500,10);
//Point法
Point[] points=new Point[2];
points[0].x=100;
points[0].y=100;
points[1].x=500;
points[1].y=500;
uiDevice.swipe(points,10);
//UiWatcher相關
uiDevice.registerWatcher("testWatch", new UiWatcher() {
@Override
public boolean checkForCondition() {
if(uiDevice.hasObject(By.text("test"))){
uiDevice.pressBack();
return true;
}
return false;
}
});
}
}