Java Code Examples for android.support.test.uiautomator.UiDevice#findObject()

The following examples show how to use android.support.test.uiautomator.UiDevice#findObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermissionGranter.java    From mobile-android-samples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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");
    }
}
 
Example 2
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: MainActivityTest.java    From android-testing-guide with MIT License 6 votes vote down vote up
@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);
}
 
Example 4
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: UiHelper.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 6
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 7
Source File: UiAutomatorUtils.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
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) {
            }
        }
    }
}
 
Example 8
Source File: PermissionGranter.java    From NoNonsense-FilePicker with Mozilla Public License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 9
Source File: EspSystemDialog.java    From espresso-macchiato with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: UIAutomatorWD.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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());
    }
}
 
Example 11
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 12
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    return allowButton.exists();
}
 
Example 13
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean viewExistsContains(UiDevice device, String textToMatch) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().textContains(textToMatch));
    return view.exists();
}
 
Example 14
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().text(messageText));
    return view.exists();
}
 
Example 15
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static void allowPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject allow = device.findObject(new UiSelector().text(TEXT_ALLOW));
    allow.click();
}
 
Example 16
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
    UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
    return allowButton.exists();
}
 
Example 17
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static void denyPermission(UiDevice device) throws UiObjectNotFoundException
{
    UiObject deny = device.findObject(new UiSelector().text(TEXT_DENY));
    deny.click();
}
 
Example 18
Source File: MainActivityTest.java    From otp-authenticator with MIT License 4 votes vote down vote up
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])));
    }
}
 
Example 19
Source File: EspSystemDialog.java    From espresso-macchiato with MIT License 4 votes vote down vote up
protected boolean dialogIsShownWith(String expectedMessage) {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject dialog = device.findObject(new UiSelector().textMatches(expectedMessage));
    return dialog.exists();
}
 
Example 20
Source File: UIUtil.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
    UiObject view = device.findObject(new UiSelector().text(messageText));
    return view.exists();
}