Java Code Examples for android.support.test.uiautomator.UiObject#exists()

The following examples show how to use android.support.test.uiautomator.UiObject#exists() . 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: UIAnimatorTest.java    From Expert-Android-Programming with MIT License 7 votes vote down vote up
@Test
public void testChangeText_sameActivity() {


    UiObject skipButton = mDevice.findObject(new UiSelector()
            .text("SKIP").className("android.widget.TextView"));

    // Simulate a user-click on the OK button, if found.
    try {
        if (skipButton.exists() && skipButton.isEnabled()) {
                skipButton.click();
        }
    } catch (UiObjectNotFoundException e) {
        e.printStackTrace();
    }
}
 
Example 2
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());
            UiSelector selector = new UiSelector()
                    .clickable(true)
                    .checkable(false)
                    .index(GRANT_BUTTON_INDEX);
            UiObject allowPermissions = device.findObject(selector);

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

    } catch (UiObjectNotFoundException e) {
        System.out.println("There is no permissions dialog to interact with");
    }
}
 
Example 3
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 4
Source File: UiWatchers.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public boolean handleAnr() {
    UiObject window = sDevice.findObject(new UiSelector()
            .className("com.android.server.am.AppNotRespondingDialog"));
    String errorText = null;
    if (window.exists()) {
        try {
            errorText = window.getText();
        } catch (UiObjectNotFoundException e) {
            Log.e(TAG, "dialog gone?", e);
        }
        onAnrDetected(errorText);
        postHandler();
        return true; // triggered
    }
    return false; // no trigger
}
 
Example 5
Source File: UiWatchers.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public boolean handleAnr2() {
    UiObject window = sDevice.findObject(new UiSelector().packageName("android")
            .textContains("isn't responding."));
    if (!window.exists()) {
        window = sDevice.findObject(new UiSelector().packageName("android")
                .textContains("沒有回應"));
    }
    if (window.exists()) {
        String errorText = null;
        try {
            errorText = window.getText();
        } catch (UiObjectNotFoundException e) {
            Log.e(TAG, "dialog gone?", e);
        }
        onAnrDetected(errorText);
        postHandler();
        return true; // triggered
    }
    return false; // no trigger
}
 
Example 6
Source File: UiWatchers.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public boolean handleCrash() {
    UiObject window = sDevice.findObject(new UiSelector()
            .className("com.android.server.am.AppErrorDialog"));
    if (window.exists()) {
        String errorText = null;
        try {
            errorText = window.getText();
        } catch (UiObjectNotFoundException e) {
            Log.e(TAG, "dialog gone?", e);
        }
        onCrashDetected(errorText);
        postHandler();
        return true; // triggered
    }
    return false; // no trigger
}
 
Example 7
Source File: UiWatchers.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public boolean handleCrash2() {
    UiObject window = sDevice.findObject(new UiSelector().packageName("android")
            .textContains("has stopped"));
    if (!window.exists()) {
        window = sDevice.findObject(new UiSelector().packageName("android")
                .textContains("已停止運作"));
    }
    if (window.exists()) {
        String errorText = null;
        try {
            errorText = window.getText();
        } catch (UiObjectNotFoundException e) {
            Log.e(TAG, "dialog gone?", e);
        }
        UiHelper.takeScreenshots("[CRASH]");
        onCrashDetected(errorText);
        postHandler();
        return true; // triggered
    }
    return false; // no trigger
}
 
Example 8
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 9
Source File: UiHelper.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public static boolean handleCommonDialog() {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject button = null;
    for (String keyword : Config.COMMON_BUTTONS) {
        button = device.findObject(new UiSelector().text(keyword).enabled(true));
        if (button != null && button.exists()) {
            break;
        }
    }
    try {
        // sometimes it takes a while for the OK button to become enabled
        if (button != null && button.exists()) {
            button.waitForExists(5000);
            button.click();
            Log.i("AppCrawlerAction", "{Click} " + button.getText() + " Button succeeded");
            return true; // triggered
        }
    } catch (UiObjectNotFoundException e) {
        Log.w(TAG, "UiObject disappear");
    }
    return false; // no trigger
}
 
Example 10
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 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: 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 13
Source File: SignInActivityTest.java    From ribot-app-android with Apache License 2.0 5 votes vote down vote up
private void allowPermissionsIfNeeded()  {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = mDevice.findObject(new UiSelector().text("Allow"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
                Timber.w(e, "There is no permissions dialog to interact with ");
            }
        }
    }
}
 
Example 14
Source File: UiScreen.java    From AppCrawler with Apache License 2.0 5 votes vote down vote up
private boolean parseSignature(UiObject uiObject) {
    //Log.v(TAG, new Exception().getStackTrace()[0].getMethodName() + "()");
    if ((uiObject == null) || !uiObject.exists())
        return false;

    if (signature.length() > Config.sScreenSignatueLength)
        return true;

    try {
        // Screen signature: use classname list in the view hierarchy
        String classname = "";
        for (String tmp : uiObject.getClassName().split("\\.")) {
            classname = tmp;
        }
        signature = signature.concat(classname + ";");

        // Add all children recursively
        for (int i = 0; i < uiObject.getChildCount(); i++) {
            parseSignature(uiObject.getChild(new UiSelector().index(i)));
        }
    } catch (UiObjectNotFoundException e) {
        Log.e(TAG, "uiObject does not exists during parsing");
        return false;
    }

    return true;
}
 
Example 15
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 16
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 17
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 18
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 19
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 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();
}