Search in sources :

Example 1 with UiSelector

use of androidx.test.uiautomator.UiSelector in project android by nextcloud.

the class InitialTest method clickByText.

/**
 * Helper to click on object that match the text value.
 *
 * @param text the text
 * @throws UiObjectNotFoundException
 */
private void clickByText(String text) throws UiObjectNotFoundException {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject obj = device.findObject(new UiSelector().text(text));
    obj.clickAndWaitForNewWindow();
}
Also used : UiObject(androidx.test.uiautomator.UiObject) UiSelector(androidx.test.uiautomator.UiSelector) UiDevice(androidx.test.uiautomator.UiDevice)

Example 2 with UiSelector

use of androidx.test.uiautomator.UiSelector 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 3 with UiSelector

use of androidx.test.uiautomator.UiSelector in project quickstart-android by firebase.

the class TestAddNumber method testAddNumber.

@Test
public void testAddNumber() {
    ViewInteraction firstNumber = onView(withId(R.id.fieldFirstNumber));
    ViewInteraction secondNumber = onView(withId(R.id.fieldSecondNumber));
    ViewInteraction addButton = onView(withId(R.id.buttonCalculate));
    firstNumber.perform(replaceText("32"));
    secondNumber.perform(replaceText("16"));
    addButton.perform(scrollTo(), click());
    Assert.assertTrue(new UiObject(new UiSelector().resourceId("com.google.samples.quickstart.functions:id/fieldAddResult").text("48")).waitForExists(60000));
}
Also used : UiObject(androidx.test.uiautomator.UiObject) UiSelector(androidx.test.uiautomator.UiSelector) ViewInteraction(androidx.test.espresso.ViewInteraction) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 4 with UiSelector

use of androidx.test.uiautomator.UiSelector 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 5 with UiSelector

use of androidx.test.uiautomator.UiSelector 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)5 UiSelector (androidx.test.uiautomator.UiSelector)5 UiObjectNotFoundException (androidx.test.uiautomator.UiObjectNotFoundException)3 ActivityManager (android.app.ActivityManager)1 Instrumentation (android.app.Instrumentation)1 Context (android.content.Context)1 ViewInteraction (androidx.test.espresso.ViewInteraction)1 LargeTest (androidx.test.filters.LargeTest)1 UiDevice (androidx.test.uiautomator.UiDevice)1 UiWatcher (androidx.test.uiautomator.UiWatcher)1 IOException (java.io.IOException)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1