https://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html
回傳值 | 方法名 | 說明 |
UiSelector | checkable (boolean val) | 依照"是否可以被勾選"來選取 |
UiSelector | checked (boolean val) | 依照"是否被勾選"來選取 |
UiSelector | childSelector (UiSelector selector) | 選取物件的子物件 |
UiSelector | className (String className) | 依照"類別名"來選取 |
<T>UiSelector | className (Class<T> type) | 依照"類別名"來選取 [.class] |
UiSelector | classNameMatches (String regex) | 依照"類別名"來選取 [正則表達式配對] |
UiSelector | clickable (boolean val) | 依照"是否可以被點擊"來選取 |
UiSelector | description (String desc) | 依照"特殊敘述"來選取 |
UiSelector | descriptionContains (String desc) | 依照"特殊敘述"來選取 [包含此文字] |
UiSelector | descriptionMatches (String regex) | 依照"特殊敘述"來選取 [正則表達式配對] |
UiSelector | descriptionStartsWith (String desc) | 依照"特殊敘述"來選取 [開頭文字配對] |
UiSelector | enabled (boolean val) | 依照"是否開啟狀態"選取 |
UiSelector | focusable (boolean val) | 依照"是否可以被聚焦"來選取 [文字框常有此屬性] |
UiSelector | focused (boolean val) | 依照"是否被聚焦"來選取 [文字聚焦時會跳出虛擬鍵盤] |
UiSelector | fromParent (UiSelector selector) | 選取物件來自子物件的父物件 |
UiSelector | index(int index) | 依照"索引"來選取 [同一層多種Layout會依序給定索引] |
UiSelector | instance (int instance) | 依照"實例"來選取 [同一種物件中第幾個物件] |
UiSelector | longClickable (boolean val) | 依照"是否可以長按"來選取 |
UiSelector | packageName (String name) | 依照"套件名"來選取 |
UiSelector | packageNameMatches (String regex) | 依照"套件名"來選取 [包含此文字] |
UiSelector | resourceId (String id) | 依照"資源名"來選取 |
UiSelector | resourceIdMatches (String regex) | 依照"資源名"來選取 [包含此文字] |
UiSelector | scrollable (boolean val) | 依照"是否可以滾動"來選取 |
UiSelector | selected (boolean val) | 依照"是否可以被選擇"來選取 |
UiSelector | text (String text) | 依照"文字內容"來選取 |
UiSelector | textContains (String text) | 依照"文字內容"來選取 [包含此文字] |
UiSelector | textMatches (String regex) | 依照"文字內容"來選取 [正則表達式配對] |
UiSelector | textStartsWith (String text) | 依照"文字內容"來選取 [開頭文字配對] |
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
// 初始化/實例化(重要)
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
@Test
public void apitest() throws UiObjectNotFoundException {
//各種尋找物件的配對條件
UiObject uiobject=uiDevice.findObject(new UiSelector().className("class"));
UiObject uiobject2=uiDevice.findObject(new UiSelector().description("desc"));
UiObject uiobject3=uiDevice.findObject(new UiSelector().resourceId("res"));
UiObject uiObject4=uiDevice.findObject(new UiSelector().text("text"));
//尋找可以滾動的物件
UiObject uiobject5=uiDevice.findObject(new UiSelector().scrollable(true));
//尋找指定物件所在同一層級的第2個物件 (0為第1個)
UiObject uiobject6=uiDevice.findObject(new UiSelector().resourceId("res").index(1));
//尋找同種類指定物件中的第3個物件 (0為第1個)
UiObject uiobject7=uiDevice.findObject(new UiSelector().resourceId("res").instance(2));
//利用正規式條件去尋找指定物件 (假設文字內容是這樣的格式 1900-01-01)
UiObject uiobject8=uiDevice.findObject(new UiSelector().textMatches("[0-9]{4}-[0-9]{2}-[0-9]{2}"));
//一般都這樣使用 除非物件要重複使用 (建議使用resourceID去尋找 比較有唯一性)
uiDevice.findObject(new UiSelector().resourceId("resID")).clickAndWaitForNewWindow();
}
}