2017年6月22日 星期四

Until API






回傳值 方法名 說明
UiObject2Condition<Boolean> checkable(boolean isCheckable) "是否可以被勾選"的條件
UiObject2Condition<Boolean> checked(boolean isChecked) "是否被勾選"的條件
UiObject2Condition<Boolean> clickable(boolean isClickable) "是否可以被點擊"的條件
UiObject2Condition<Boolean> descContains(String substring) "特定描述"的條件 [包含此文字]
UiObject2Condition<Boolean> descEndsWith(String substring) "特定描述"的條件 [結尾文字配對]
UiObject2Condition<Boolean> descEquals(String contentDescription) "特定描述"的條件 [相等]
UiObject2Condition<Boolean> descMatches(String regex) "特定描述"的條件 [正則表達式]
UiObject2Condition<Boolean> descMatches(Pattern regex) "特定描述"的條件 [使用Pattern的正則表達式]
UiObject2Condition<Boolean> descStartsWith(String substring) "特定描述"的條件 [包含此文字]
UiObject2Condition<Boolean> enabled(boolean isEnabled) "是否開啟狀態"的條件
SearchCondition<UiObject2> findObject(BySelector selector) "找到指定物件"的條件
SearchCondition<List<UiObject2>> findObjects(BySelector selector) "找到指定物件"的條件 [同種類]
SearchCondition<UiObject2> focusable(boolean isFocusable) "是否可以被聚焦"的條件
SearchCondition<UiObject2> focused(boolean isFocused) "是否被聚焦"的條件
SearchCondition<Boolean> gone(BySelector selector) "指定物件消失"的條件
SearchCondition<Boolean> hasObject(BySelector selector) "指定物件出現"的條件
UiObject2Condition<Boolean> longClickable(boolean isLongClickable) "是否可以被長按"的條件
EventCondition<Boolean> newWindow() "新畫面出現"的條件
EventCondition<Boolean> scrollFinished(Direction direction) "滾動方向結束"的條件
UiObject2Condition<Boolean> scrollable(boolean isScrollable) "是否可以滾動"的條件
UiObject2Condition<Boolean> selected(boolean isSelected) "是否可以被選擇"的條件
UiObject2Condition<Boolean> textContains(String substring) "文字內容"的條件 [包含此文字]
UiObject2Condition<Boolean> textEndsWith(String substring) "文字內容"的條件 [結尾文字配對]
UiObject2Condition<Boolean> textEquals(String text) "文字內容"的條件 [相等]
UiObject2Condition<Boolean> textMatches(String regex) "文字內容"的條件 [正則表達式]
UiObject2Condition<Boolean> textMatches(Pattern regex) "文字內容"的條件 [使用Pattern的正則表達式]
UiObject2Condition<Boolean> textNotEquals(String text) "文字內容"的條件 [不相等]
UiObject2Condition<Boolean> textStartsWith(String substring) "文字內容"的條件 [開頭文字配對]

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    // 初始化/實例化(重要)
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    @Test
    public void apitest() throws UiObjectNotFoundException, InterruptedException {

        //UiObject2Condition
        //開關Wifi (假設已經開啟) 點擊後如果切換鈕屬性變成false 則執行下一步 不然就等10秒再執行
        UiObject2 uiobject=uiDevice.findObject(By.res("com.android.settings:id/switch_widget"));
        uiobject.click();
        uiobject.wait(Until.checked(false),10000);
        uiDevice.pressBack();

        //SearchCondition
        //接著等待指定條件有觸發就點擊新物件 反之最多再等10秒一樣
        uiDevice.wait(Until.hasObject(By.res("android:id/title")),10000);
        uiDevice.findObject(By.text("Bluetooth")).click();

        //EventCondition
        //點擊指定物件等待畫面更新在執行下一步 (最多等待15)
        UiObject2 uiObject2=uiDevice.findObject(By.text("test"));
        uiObject2.clickAndWait(Until.newWindow(),15000);
        uiDevice.pressBack();

    }
}