2017年6月22日 星期四

BySelector API





https://developer.android.com/reference/android/support/test/uiautomator/BySelector.html


回傳值 方法名 說明
BySelector checkable(boolean isCheckable) 依照"是否可以被勾選"來選取
BySelector checked(boolean isChecked) 依照"是否被勾選"來選取
BySelector clazz(String packageName, String className) 依照"套件名"和"類別名"來選取
BySelector clazz(Class clazz) 依照"類別名"來選取 [.class]
BySelector clazz(Pattern className) 依照"類別名"來選取 [正則表達式配對]
BySelector clazz(String className) 依照"類別名"來選取
BySelector clickable(boolean isClickable) 依照"是否可以被點擊"來選取
BySelector depth(int min, int max) 設定搜索範圍層級
BySelector depth(int exactDepth) 設定指定層級
BySelector desc(String contentDescription) 依照"特殊敘述"來選取
BySelector desc(Pattern contentDescription) 依照"特殊敘述"來選取 [正則表達式配對]
BySelector descContains(String substring) 依照"特殊敘述"來選取 [包含此文字]
BySelector descEndsWith(String substring) 依照"特殊敘述"來選取 [結尾文字配對]
BySelector descStartsWith(String substring) 依照"特殊敘述"來選取 [開頭文字配對]
BySelector enabled(boolean isEnabled) 依照"是否開啟狀態"選取
BySelector focusable(boolean isFocusable) 依照"是否可以被聚焦"來選取 [文字框常有此屬性]
BySelector focused(boolean isFocused) 依照"是否被聚焦"來選取 [文字聚焦時會跳出虛擬鍵盤]
BySelector hasChild(BySelector childSelector) 有指定子物件的條件
BySelector hasDescendant(BySelector descendantSelector) 有指定後代物件的條件
BySelector hasDescendant(BySelector descendantSelector, int maxDepth) 有指定後代物件的條件且限制最大層級
BySelector longClickable(boolean isLongClickable) 依照"是否可以長按"來選取
BySelector maxDepth(int max) 設定搜索最大層級
BySelector minDepth(int min) 設定搜索最小層級
BySelector pkg(Pattern applicationPackage) 依照"套件名"來選取 [正則表達式配對]
BySelector pkg(String applicationPackage) 依照"套件名"來選取
BySelector res(String resourceName) 依照"資源名"來選取
BySelector res(String resourcePackage, String resourceId) 依照"套件名""資源名"來選取
BySelector res(Pattern resourceName) 依照"資源名"來選取 [正則表達式配對]
BySelector scrollable(boolean isScrollable) 依照"是否可以滾動"來選取
BySelector selected(boolean isSelected) 依照"是否可以被選擇"來選取
BySelector text(Pattern textValue) 依照"文字內容"來選取 [正則表達式配對]
BySelector text(String textValue) 依照"文字內容"來選取
BySelector textContains(String substring) 依照"文字內容"來選取 [包含此文字]
BySelector textEndsWith(String substring) 依照"文字內容"來選取 [開頭文字配對]
BySelector textStartsWith(String substring) 依照"文字內容"來選取 [結尾文字配對]


@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

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

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

        //各種尋找物件的配對條件
        UiObject2 uiobject1=uiDevice.findObject(By.clazz("clazz"));
        UiObject2 uiobject2=uiDevice.findObject(By.desc("desc"));
        UiObject2 uiobject3=uiDevice.findObject(By.res("res"));
        UiObject2 uiobject4=uiDevice.findObject(By.text("text"));
        UiObject2 uiobject5=uiDevice.findObject(By.scrollable(true));
        UiObject2 uiobject6=uiDevice.findObject(By.hasChild(By.focusable(true)));

        //利用正規式條件去尋找指定物件 (假設文字內容是這樣的格式 1900-01-01)
        Pattern pattern=Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
        UiObject2 uiobject7=uiDevice.findObject(By.text(pattern));

        //找尋指定物件且符合層級的範圍
        UiObject2 uiobject8=uiDevice.findObject(By.text("test").depth(2,8));

    }