android.support.test.uiautomator.UiDevice#findObject ( )源码实例Demo

下面列出了android.support.test.uiautomator.UiDevice#findObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

public void allowPermissionsIfNeeded() {
    try {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            sleep(PERMISSIONS_DIALOG_DELAY);
            UiDevice device = UiDevice.getInstance(getInstrumentation());
            UiObject allowPermissions = device.findObject(new UiSelector()
                    .clickable(true)
                    .checkable(false)
                    .index(GRANT_BUTTON_INDEX));

            if (allowPermissions.exists()) {
                allowPermissions.click();
            }
        }

    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
源代码2 项目: android-testing-guide   文件: MainActivityTest.java
@Ignore
@Test
public void testUiAutomatorAPI() throws UiObjectNotFoundException, InterruptedException {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    UiSelector editTextSelector = new UiSelector().className("android.widget.EditText").text("this is a test").focusable(true);
    UiObject editTextWidget = device.findObject(editTextSelector);
    editTextWidget.setText("this is new text");

    Thread.sleep(2000);

    UiSelector buttonSelector = new UiSelector().className("android.widget.Button").text("CLICK ME").clickable(true);
    UiObject buttonWidget = device.findObject(buttonSelector);
    buttonWidget.click();

    Thread.sleep(2000);
}
 
源代码3 项目: SweetBlue   文件: UIUtil.java
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException
{
    if (textToMatch == null || textToMatch.length < 1)
    {
        return false;
    }
    UiObject view;
    boolean contains = false;
    for (int i = 0; i < textToMatch.length; i++)
    {
        view = device.findObject(new UiSelector().textContains(textToMatch[i]));
        contains = view.exists();
        if (contains) return true;
    }
    return false;
}
 
源代码4 项目: AppCrawler   文件: UiHelper.java
public static void inputRandomTextToEditText() {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject edit = null;
    int i = 0;
    do {
        edit = null;
        edit = device.findObject(new UiSelector().className("android.widget.EditText").instance(i++));
        if (edit != null && edit.exists()) {
            try {
                Random rand = new Random();
                String text = Config.RANDOM_TEXT[rand.nextInt(Config.RANDOM_TEXT.length - 1)];
                edit.setText(text);
            } catch (UiObjectNotFoundException e) {
                // Don't worry
            }
        }
    } while (edit != null && edit.exists());
}
 
源代码5 项目: SweetBlue   文件: UIUtil.java
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException
{
    if (textToMatch == null || textToMatch.length < 1)
    {
        return false;
    }
    UiObject view;
    boolean contains = false;
    for (int i = 0; i < textToMatch.length; i++)
    {
        view = device.findObject(new UiSelector().textContains(textToMatch[i]));
        contains = view.exists();
        if (contains) return true;
    }
    return false;
}
 
源代码6 项目: UIAutomatorWD   文件: UIAutomatorWD.java
public void skipPermission(JSONArray permissionPatterns, int scanningCount) {
    UiDevice mDevice = Elements.getGlobal().getmDevice();

    // if permission list is empty, avoid execution
    if (permissionPatterns.size() == 0) {
        return;
    }

    // regular check for permission scanning
    try {
        for (int i = 0; i < scanningCount; i++) {
            inner:
            for (int j = 0; j < permissionPatterns.size(); j++) {
                String text = permissionPatterns.getString(j);
                UiObject object = mDevice.findObject(new UiSelector().text(text));
                if (object.exists()) {
                    object.click();
                    break inner;
                }
            }

            Thread.sleep(3000);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause().toString());
    }
}
 
源代码7 项目: Forage   文件: UiAutomatorUtils.java
public static void allowPermissionsIfNeeded(UiDevice device) {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = device.findObject(new UiSelector().text(TEXT_ALLOW));

        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException ignored) {
            }
        }
    }
}
 
源代码8 项目: NoNonsense-FilePicker   文件: PermissionGranter.java
public static void allowPermissionsIfNeeded(Activity activity, String permissionNeeded) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(activity, permissionNeeded)) {
            sleep(PERMISSIONS_DIALOG_DELAY);
            UiDevice device = UiDevice.getInstance(getInstrumentation());
            UiObject allowPermissions = device.findObject(new UiSelector().clickable(true).index(GRANT_BUTTON_INDEX));
            if (allowPermissions.exists()) {
                allowPermissions.click();
            }
        }
    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
源代码9 项目: espresso-macchiato   文件: EspSystemDialog.java
protected void click(String target) {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject button = device.findObject(new UiSelector().text(target));

    try {
        button.click();
    } catch (UiObjectNotFoundException e) {
        throw new IllegalStateException(e);
    }
}
 
源代码10 项目: SweetBlue   文件: UIUtil.java
/**
 * Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
 * will try upper casing the text if it is not found.
 */
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
    UiObject accept = device.findObject(new UiSelector().text(buttonText));
    if (accept.exists())
    {
        accept.click();
        return;
    }
    accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
    accept.click();
}
 
源代码11 项目: SweetBlue   文件: UIUtil.java
/**
 * Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
 * will try upper casing the text if it is not found.
 */
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
    UiObject accept = device.findObject(new UiSelector().text(buttonText));
    if (accept.exists())
    {
        accept.click();
        return;
    }
    accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
    accept.click();
}
 
源代码12 项目: SweetBlue   文件: UIUtil.java
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().text(messageText));
    return view.exists();
}
 
源代码13 项目: espresso-macchiato   文件: EspSystemDialog.java
protected boolean dialogIsShownWith(String expectedMessage) {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject dialog = device.findObject(new UiSelector().textMatches(expectedMessage));
    return dialog.exists();
}
 
源代码14 项目: SweetBlue   文件: UIUtil.java
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    return allowButton.exists();
}
 
源代码15 项目: otp-authenticator   文件: MainActivityTest.java
public void test004Rearrange() throws InterruptedException, UiObjectNotFoundException {
    UiDevice mDevice = UiDevice.getInstance(getInstrumentation());


    UiObject start =  mDevice.findObject(new UiSelector().textContains(codes[0][0]));
    UiObject end =  mDevice.findObject(new UiSelector().textContains(codes[1][0]));

    start.dragTo(start, 10);
    start.dragTo(end, 10);

    mDevice.pressBack();

    Thread.sleep(2000);

    String t = codes[0][0];
    codes[0][0] = codes[1][0];
    codes[1][0] = t;

    for(int i = 0; i < codes.length; i++){
        onData(anything()).inAdapterView(withId(R.id.listView))
                .atPosition(i)
                .onChildView(withId(R.id.textViewLabel))
                .check(matches(withText(codes[i][0])));
    }

    start =  mDevice.findObject(new UiSelector().textContains(codes[0][0]));
    end =  mDevice.findObject(new UiSelector().textContains(codes[1][0]));

    start.dragTo(start, 10);
    start.dragTo(end, 10);

    mDevice.pressBack();

    Thread.sleep(2000);

    t = codes[0][0];
    codes[0][0] = codes[1][0];
    codes[1][0] = t;

    for(int i = 0; i < codes.length; i++){
        onData(anything()).inAdapterView(withId(R.id.listView))
                .atPosition(i)
                .onChildView(withId(R.id.textViewLabel))
                .check(matches(withText(codes[i][0])));
    }
}
 
源代码16 项目: SweetBlue   文件: UIUtil.java
public static void denyPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject deny = device.findObject(new UiSelector().text(TEXT_DENY));
    deny.click();
}
 
源代码17 项目: SweetBlue   文件: UIUtil.java
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    return allowButton.exists();
}
 
源代码18 项目: SweetBlue   文件: UIUtil.java
public static void allowPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject allow = device.findObject(new UiSelector().text(TEXT_ALLOW));
    allow.click();
}
 
源代码19 项目: SweetBlue   文件: UIUtil.java
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().text(messageText));
    return view.exists();
}
 
源代码20 项目: SweetBlue   文件: UIUtil.java
public static boolean viewExistsContains(UiDevice device, String textToMatch) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().textContains(textToMatch));
    return view.exists();
}