Search in sources :

Example 1 with UiObjectNotFoundException

use of androidx.test.uiautomator.UiObjectNotFoundException in project fdroidclient by f-droid.

the class UiWatchers method postHandler.

/**
 * Current implementation ignores the exception and continues.
 */
public void postHandler(String buttonText) {
    // TODO: Add custom error logging here
    String formatedOutput = String.format("UI Exception Message: %-20s\n", UiDevice.getInstance().getCurrentPackageName());
    Log.e(LOG_TAG, formatedOutput);
    UiObject buttonOK = new UiObject(new UiSelector().text(buttonText).enabled(true));
    // sometimes it takes a while for the OK button to become enabled
    buttonOK.waitForExists(5000);
    try {
        buttonOK.click();
    } catch (UiObjectNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : UiObject(androidx.test.uiautomator.UiObject) UiSelector(androidx.test.uiautomator.UiSelector) UiObjectNotFoundException(androidx.test.uiautomator.UiObjectNotFoundException)

Example 2 with UiObjectNotFoundException

use of androidx.test.uiautomator.UiObjectNotFoundException in project fdroidclient by f-droid.

the class UiWatchers method registerAnrAndCrashWatchers.

/**
 * We can use the UiDevice registerWatcher to register a small script to be executed when the
 * framework is waiting for a control to appear. Waiting may be the cause of an unexpected
 * dialog on the screen and it is the time when the framework runs the registered watchers.
 * This is a sample watcher looking for ANR and crashes. it closes it and moves on. You should
 * create your own watchers and handle error logging properly for your type of tests.
 */
public void registerAnrAndCrashWatchers() {
    UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {

        @Override
        public boolean checkForCondition() {
            UiObject window = new UiObject(new UiSelector().className("com.android.server.am.AppNotRespondingDialog"));
            String errorText = null;
            if (window.exists()) {
                try {
                    errorText = window.getText();
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "dialog gone?", e);
                }
                onAnrDetected(errorText);
                postHandler("Wait");
                // triggered
                return true;
            }
            // no trigger
            return false;
        }
    });
    // class names may have changed
    UiDevice.getInstance().registerWatcher("ANR2", new UiWatcher() {

        @Override
        public boolean checkForCondition() {
            UiObject window = new UiObject(new UiSelector().packageName("android").textContains("isn't responding."));
            if (window.exists()) {
                String errorText = null;
                try {
                    errorText = window.getText();
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "dialog gone?", e);
                }
                onAnrDetected(errorText);
                postHandler("Wait");
                // triggered
                return true;
            }
            // no trigger
            return false;
        }
    });
    UiDevice.getInstance().registerWatcher("CRASH", new UiWatcher() {

        @Override
        public boolean checkForCondition() {
            UiObject window = new UiObject(new UiSelector().className("com.android.server.am.AppErrorDialog"));
            if (window.exists()) {
                String errorText = null;
                try {
                    errorText = window.getText();
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "dialog gone?", e);
                }
                onCrashDetected(errorText);
                postHandler("OK");
                // triggered
                return true;
            }
            // no trigger
            return false;
        }
    });
    UiDevice.getInstance().registerWatcher("CRASH2", new UiWatcher() {

        @Override
        public boolean checkForCondition() {
            UiObject window = new UiObject(new UiSelector().packageName("android").textContains("has stopped"));
            if (window.exists()) {
                String errorText = null;
                try {
                    errorText = window.getText();
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "dialog gone?", e);
                }
                onCrashDetected(errorText);
                postHandler("OK");
                // triggered
                return true;
            }
            // no trigger
            return false;
        }
    });
    Log.i(LOG_TAG, "Registered GUI Exception watchers");
}
Also used : UiWatcher(androidx.test.uiautomator.UiWatcher) UiObject(androidx.test.uiautomator.UiObject) UiSelector(androidx.test.uiautomator.UiSelector) UiObjectNotFoundException(androidx.test.uiautomator.UiObjectNotFoundException)

Example 3 with UiObjectNotFoundException

use of androidx.test.uiautomator.UiObjectNotFoundException in project fdroidclient by f-droid.

the class MainActivityEspressoTest method classSetUp.

@BeforeClass
public static void classSetUp() {
    IdlingPolicies.setIdlingResourceTimeout(10, TimeUnit.MINUTES);
    IdlingPolicies.setMasterPolicyTimeout(10, TimeUnit.MINUTES);
    if (!canRunEspresso()) {
        return;
    }
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    try {
        UiDevice.getInstance(instrumentation).executeShellCommand("pm grant " + instrumentation.getTargetContext().getPackageName() + " android.permission.SET_ANIMATION_SCALE");
    } catch (IOException e) {
        e.printStackTrace();
    }
    SystemAnimations.disableAll(ApplicationProvider.getApplicationContext());
    // dismiss the ANR or any other system dialogs that might be there
    UiObject button = new UiObject(new UiSelector().text("Wait").enabled(true));
    try {
        button.click();
    } catch (UiObjectNotFoundException e) {
        Log.d(TAG, e.getLocalizedMessage());
    }
    new UiWatchers().registerAnrAndCrashWatchers();
    Context context = instrumentation.getTargetContext();
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = ContextCompat.getSystemService(context, ActivityManager.class);
    activityManager.getMemoryInfo(mi);
    long percentAvail = mi.availMem / mi.totalMem;
    Log.i(TAG, "RAM: " + mi.availMem + " / " + mi.totalMem + " = " + percentAvail);
}
Also used : Context(android.content.Context) UiObject(androidx.test.uiautomator.UiObject) UiSelector(androidx.test.uiautomator.UiSelector) Instrumentation(android.app.Instrumentation) IOException(java.io.IOException) ActivityManager(android.app.ActivityManager) UiObjectNotFoundException(androidx.test.uiautomator.UiObjectNotFoundException) BeforeClass(org.junit.BeforeClass)

Aggregations

UiObject (androidx.test.uiautomator.UiObject)3 UiObjectNotFoundException (androidx.test.uiautomator.UiObjectNotFoundException)3 UiSelector (androidx.test.uiautomator.UiSelector)3 ActivityManager (android.app.ActivityManager)1 Instrumentation (android.app.Instrumentation)1 Context (android.content.Context)1 UiWatcher (androidx.test.uiautomator.UiWatcher)1 IOException (java.io.IOException)1 BeforeClass (org.junit.BeforeClass)1