androidx.test.uiautomator.Until Java Examples

The following examples show how to use androidx.test.uiautomator.Until. 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: BaseElementHandler.java    From za-Farmer with MIT License 6 votes vote down vote up
public UiObject2 getElement(BySelector bySelector, int timeout) throws UiObjectNotFoundException {

        UiObject2 uiObject;
        if (this.order > 0) {
            List<UiObject2> objs = getUiDevice().wait(Until.findObjects(bySelector), timeout);
            if (objs == null) {
                uiObject = null;
            } else {
                uiObject = objs.get(this.order - 1);
            }
        } else {
            uiObject = getUiDevice().wait(Until.findObject(bySelector), timeout);
        }
        if (uiObject == null)
            throw new UiObjectNotFoundException("element not found");

        return uiObject;
    }
 
Example #2
Source File: ChangeTextBehaviorTest.java    From testing-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the blueprint app
    Context context = getApplicationContext();
    final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
Example #3
Source File: TestUtils.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
private static void waitForAppToLaunch(String appPackage) throws JSONException {
    long start = elapsedRealtime();
    boolean waitStatus;
    do {
        Device.waitForIdle();
        waitStatus = getUiDevice().wait(Until.hasObject(
                androidx.test.uiautomator.By.pkg(appPackage).depth(0)),
                IMPLICIT_TIMEOUT);
        if (waitStatus) {
            return;
        }
        Logger.error("Unable to find AUT. Is it System UI or AUT ARN on arm emu?");
        Logger.debug("Page sources:" + source().getValue());
        Response response = findElement(By.xpath("//*[@text='Close app' or @text='Wait' or " +
                "@text='OK' or @text='Open app again']"));
        if (response.isSuccessful()) {
            click(response.getElementId());
        }
        waitForMillis(DEFAULT_POLLING_INTERVAL);
    } while (elapsedRealtime() - start < APP_LAUNCH_TIMEOUT);
    throw new TimeoutException("app to launch");
}
 
Example #4
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testA_cancelSignIn() throws UiObjectNotFoundException {
    getProgressBar().waitUntilGone(NETWORK_TIMEOUT);
    onView(withId(R.id.clear_data_btn)).check(matches(isDisplayed()));
    onView(withId(R.id.clear_data_btn)).perform(click());
    onView(withId(R.id.browser_sign_in_btn)).check(matches(isDisplayed()));

    onView(withId(R.id.browser_sign_in_btn)).perform(click());
    mDevice.wait(Until.findObject(By.pkg(CHROME_STABLE)), TRANSITION_TIMEOUT);

    UiSelector selector = new UiSelector();
    UiObject closeBrowser = mDevice.findObject(selector.resourceId(ID_CLOSE_BROWSER));
    closeBrowser.click();

    mDevice.wait(Until.findObject(By.pkg(SAMPLE_APP)), TRANSITION_TIMEOUT);
    onView(withText(R.string.auth_canceled)).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
}
 
Example #5
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 6 votes vote down vote up
private void customTabInteraction(boolean enterUserName) throws UiObjectNotFoundException {
    mDevice.wait(Until.findObject(By.pkg(CHROME_STABLE)), TRANSITION_TIMEOUT);
    acceptChromePrivacyOption();
    UiSelector selector = new UiSelector();
    if (enterUserName) {
        UiObject username = mDevice.findObject(selector.resourceId(ID_USERNAME));
        username.setText(USERNAME);
    }
    UiObject password = mDevice.findObject(selector.resourceId(ID_PASSWORD));
    password.setText(PASSWORD);
    UiObject signIn = mDevice.findObject(selector.resourceId(ID_SUBMIT));
    signIn.click();
    mDevice.wait(Until.findObject(By.pkg(SAMPLE_APP)), TRANSITION_TIMEOUT);
    //wait for token exchange
    getProgressBar().waitUntilGone(NETWORK_TIMEOUT);
}
 
Example #6
Source File: BaseElementHandler.java    From za-Farmer with MIT License 6 votes vote down vote up
public UiObject2 getElement(BySelector bySelector, int swipCount, String direction) throws UiObjectNotFoundException {
    UiObject2 uiObject = null;
    for (int i = 0; i < swipCount; i++) {
        uiObject = getUiDevice().wait(Until.findObject(bySelector), 1000);
        if (uiObject != null) {
            break;
        }
        Swipe upSwip = new Swipe();
        upSwip.setDirection(direction);
        upSwip.runSelf();
    }
    if (uiObject == null) {
        throw new UiObjectNotFoundException("element not found");
        // bySelector.
    } else {
        return uiObject;
    }
}
 
Example #7
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 5 votes vote down vote up
private void unlockKeystore() throws UiObjectNotFoundException {
    onView(withText("Unlock Screen"))
            .inRoot(isDialog())
            .check(matches(isDisplayed()))
            .perform(click());
    mDevice.wait(Until.findObject(By.pkg(SETTINGS_APP)), TRANSITION_TIMEOUT);
    UiSelector selector = new UiSelector();
    UiObject passwordText = mDevice.findObject(selector.focused(true));
    passwordText.setText(PINCODE);
    mDevice.pressEnter();
    mDevice.wait(Until.findObject(By.pkg(SAMPLE_APP)), TRANSITION_TIMEOUT);
    getProgressBar().waitUntilGone(NETWORK_TIMEOUT);
}
 
Example #8
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 5 votes vote down vote up
@Test
public void test3_signInWithSession() {
    onView(withId(R.id.container)).perform(swipeUp());
    onView(withId(R.id.browser_sign_in_btn)).check(matches(isDisplayed()));
    onView(withId(R.id.browser_sign_in_btn)).perform(click());

    mDevice.wait(Until.findObject(By.pkg(CHROME_STABLE)), TRANSITION_TIMEOUT);
    mDevice.wait(Until.findObject(By.pkg(SAMPLE_APP)), TRANSITION_TIMEOUT);
    getProgressBar().waitUntilGone(NETWORK_TIMEOUT);
    onView(withId(R.id.clear_data_btn)).check(matches(isDisplayed()));

}
 
Example #9
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 5 votes vote down vote up
@Test
public void test6_signOutOfOkta() {
    getProgressBar().waitUntilGone(NETWORK_TIMEOUT);
    signInIfNotAlready();
    onView(withId(R.id.logout_btn)).check(matches(isDisplayed()));
    onView(withId(R.id.logout_btn)).perform(click());

    mDevice.wait(Until.findObject(By.pkg(CHROME_STABLE)), TRANSITION_TIMEOUT);
    mDevice.wait(Until.findObject(By.pkg(SAMPLE_APP)), TRANSITION_TIMEOUT);

    onView(withText(R.string.sign_out_success)).inRoot(new ToastMatcher()).check(matches(isDisplayed()));
}
 
Example #10
Source File: EraseAllUserDataTest.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void systemBarHomeViewTest() throws UiObjectNotFoundException  {

    // Initialize UiDevice instance
    final int LAUNCH_TIMEOUT = 5000;

    // Open a webpage
    TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);
    TestHelper.inlineAutocompleteEditText.clearTextField();
    TestHelper.inlineAutocompleteEditText.setText(webServer.url(TEST_PATH).toString());
    TestHelper.hint.waitForExists(waitingTime);
    TestHelper.pressEnterKey();
    TestHelper.waitForWebContent();
    // Switch out of Focus, pull down system bar and select delete browsing history
    TestHelper.pressHomeKey();
    TestHelper.openNotification();
    TestHelper.notificationBarDeleteItem.waitForExists(waitingTime);
    TestHelper.notificationBarDeleteItem.click();

    // Wait for launcher
    final String launcherPackage = TestHelper.mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    TestHelper.mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    // Launch the app
    mActivityTestRule.launchActivity(new Intent(Intent.ACTION_MAIN));
    // Verify that it's on the main view, not showing the previous browsing session
    TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);
    assertTrue(TestHelper.inlineAutocompleteEditText.exists());
}
 
Example #11
Source File: MultitaskingTest.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private void checkNewTabPopup() {
    TestHelper.mDevice.wait(Until.findObject(
            By.res(TestHelper.getAppName(), "snackbar_text")), 5000);

    TestHelper.mDevice.wait(Until.gone(
            By.res(TestHelper.getAppName(), "snackbar_text")), 5000);
}
 
Example #12
Source File: WaitUntilGone.java    From za-Farmer with MIT License 5 votes vote down vote up
@Override
protected void runSelf() {
    if ((getUiDevice().wait(Until.gone(getElementSelector()), 5000))) {
        screenshot(TYPE_LOG_SUCCESS);
    } else {
        LogUtils.getInstance().error("WaitUntilGone Faild");
    }
}
 
Example #13
Source File: ChangeTextBehaviorTest.java    From testing-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeText_sameActivity() {
    // Type text and then press the button.
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
            .setText(STRING_TO_BE_TYPED);
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt"))
            .click();

    // Verify the test is displayed in the Ui
    UiObject2 changedText = mDevice
            .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")),
                    500 /* wait 500ms */);
    assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
}
 
Example #14
Source File: ChangeTextBehaviorTest.java    From testing-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeText_newActivity() {
    // Type text and then press the button.
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
            .setText(STRING_TO_BE_TYPED);
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "activityChangeTextBtn"))
            .click();

    // Verify the test is displayed in the Ui
    UiObject2 changedText = mDevice
            .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "show_text_view")),
                    500 /* wait 500ms */);
    assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
}
 
Example #15
Source File: AndroidTestsUtility.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public static boolean checkElementExists(int resourceId) {
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    Context context = ApplicationProvider.getApplicationContext();
    return uiDevice.wait(Until.hasObject(By.res(toResourceName(context, resourceId))), AndroidTestsConfig.TIMEOUT);
}
 
Example #16
Source File: AndroidTestsUtility.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public static boolean checkElementExists(String className) {
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    uiDevice.wait(Until.hasObject(By.clazz(className)), AndroidTestsConfig.TIMEOUT);
    UiObject2 element = uiDevice.findObject(By.clazz(className));
    return element.isEnabled() && element.isClickable() && element.isFocusable();
}
 
Example #17
Source File: LineAppLoginPage.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public void tapAllowButton() {
    uiDevice.wait(Until.hasObject(By.pkg(AndroidTestsConfig.LINE_PACKAGE_NAME).depth(0)),
            AndroidTestsConfig.TIMEOUT);
    assertTrue(checkElementExists(AndroidTestsConfig.ANDROID_WIDGET_BUTTON));
    uiDevice.findObjects(By.clazz(AndroidTestsConfig.ANDROID_WIDGET_BUTTON)).get(0).click();
}
 
Example #18
Source File: BrowserScreenScreenshots.java    From focus-android with Mozilla Public License 2.0 4 votes vote down vote up
private void takeScreenshotOfTabsTrayAndErase() throws Exception {
    final UiObject mozillaImage = device.findObject(new UiSelector()
            .resourceId("download")
            .enabled(true));

    UiObject imageMenuTitle = device.findObject(new UiSelector()
            .resourceId(TestHelper.getAppName() + ":id/topPanel")
            .enabled(true));
    UiObject openNewTabTitle = device.findObject(new UiSelector()
            .resourceId(TestHelper.getAppName() + ":id/design_menu_item_text")
            .text(getString(R.string.contextmenu_open_in_new_tab))
            .enabled(true));
    UiObject multiTabBtn = device.findObject(new UiSelector()
            .resourceId(TestHelper.getAppName() + ":id/tabs")
            .enabled(true));
    UiObject eraseHistoryBtn = device.findObject(new UiSelector()
            .text(getString(R.string.tabs_tray_action_erase))
            .enabled(true));

    assertTrue(mozillaImage.waitForExists(waitingTime));
    mozillaImage.dragTo(mozillaImage, 7);
    assertTrue(imageMenuTitle.waitForExists(waitingTime));
    assertTrue(imageMenuTitle.exists());
    Screengrab.screenshot("Image_Context_Menu");

    //Open a new tab
    openNewTabTitle.click();
    TestHelper.mDevice.wait(Until.findObject(
            By.res(TestHelper.getAppName(), "snackbar_text")), 5000);
    Screengrab.screenshot("New_Tab_Popup");
    TestHelper.mDevice.wait(Until.gone(
            By.res(TestHelper.getAppName(), "snackbar_text")), 5000);

    assertTrue(multiTabBtn.waitForExists(waitingTime));
    multiTabBtn.click();
    assertTrue(eraseHistoryBtn.waitForExists(waitingTime));
    Screengrab.screenshot("Multi_Tab_Menu");

    eraseHistoryBtn.click();

    device.wait(Until.findObject(
            By.res(TestHelper.getAppName(), "snackbar_text")), waitingTime);

    Screengrab.screenshot("YourBrowsingHistoryHasBeenErased");
}
 
Example #19
Source File: SwitchContextTest.java    From focus-android with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void settingsToFocus() throws UiObjectNotFoundException {

    // Initialize UiDevice instance
    final int LAUNCH_TIMEOUT = 5000;
    final String SETTINGS_APP = "com.android.settings";
    final UiObject settings = TestHelper.mDevice.findObject(new UiSelector()
            .packageName(SETTINGS_APP)
            .enabled(true));

    // Open a webpage
    TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);
    TestHelper.inlineAutocompleteEditText.clearTextField();
    TestHelper.inlineAutocompleteEditText.setText(webServer.url(TEST_PATH).toString());
    TestHelper.hint.waitForExists(waitingTime);
    TestHelper.pressEnterKey();

    // Assert website is loaded
    TestHelper.waitForWebSiteTitleLoad();

    // Switch out of Focus, open settings app
    TestHelper.pressHomeKey();

    // Wait for launcher
    final String launcherPackage = TestHelper.mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    TestHelper.mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getInstrumentation()
            .getTargetContext()
            .getApplicationContext();
    final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(SETTINGS_APP);
    context.startActivity(intent);

    // switch to Focus
    settings.waitForExists(waitingTime);
    assertTrue(settings.exists());
    TestHelper.openNotification();

    // If simulator has more recent message, the options need to be expanded
    TestHelper.expandNotification();
    TestHelper.notificationOpenItem.click();

    // Verify that it's on the main view, showing the previous browsing session
    TestHelper.browserURLbar.waitForExists(waitingTime);
    assertTrue(TestHelper.browserURLbar.exists());
    TestHelper.waitForWebSiteTitleLoad();
}
 
Example #20
Source File: DeviceAutomator.java    From device-automator with MIT License 2 votes vote down vote up
/**
 * Asserts that the foreground app has the given package name. Waits for up to the given timeout
 * for the given package to become the foreground app.
 *
 * @param packageName package name to check against the foreground app.
 * @return {@link DeviceAutomator} for method chaining.
 */
public DeviceAutomator checkForegroundAppIs(String packageName, long timeout) {
    mDevice.wait(Until.hasObject(By.pkg(packageName).depth(0)), 5000);
    assertTrue(mDevice.hasObject(By.pkg(packageName).depth(0)));
    return this;
}
 
Example #21
Source File: DeviceAutomator.java    From device-automator with MIT License 2 votes vote down vote up
/**
 * Waits for the ui element specified in {@link #onDevice(UiObjectMatcher)} to be enabled with
 * the given timeout.
 *
 * @return {@link DeviceAutomator} for method chaining.
 */
public DeviceAutomator waitForEnabled(final long timeout) {
    mDevice.findObject(mMatcher.getBySelector()).wait(Until.enabled(true), timeout);
    return this;
}