Java Code Examples for android.support.test.uiautomator.BySelector

The following are top voted examples for showing how to use android.support.test.uiautomator.BySelector. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ChimpCheck   File: WildCardManager.java   Source Code and License 6 votes vote down vote up
public ArrayList<UiObject2> retrieveUiObject2s(BySelector topLevelSelector, BySelector childSelector, int depth) throws UiObjectNotFoundException {
    if (depth == 0) { return new ArrayList<>(); }
    ArrayList<UiObject2> currUiObjects = retrieveTopLevelUiObjects(topLevelSelector);
    ArrayList<UiObject2> baseUiObjects = new ArrayList<>();
    while (depth > 0 && currUiObjects.size() > 0) {
        ArrayList<UiObject2> tempUiObjects = new ArrayList<>();
        for(UiObject2 currUiObject: currUiObjects) {
            if (currUiObject.getChildCount() != 0) {
                tempUiObjects.addAll( retrieveChildUiObjects(currUiObject, childSelector) );
            } else {
                baseUiObjects.add( currUiObject );
            }
        }
        currUiObjects = tempUiObjects;
        depth--;
    }

    String str = "/***** Inferred Actionable UI Objects *****/\n";
    for(UiObject2 candidate: new HashSet<UiObject2>(baseUiObjects)) {
        str += "Found candidate: " + candidate.getClassName() + " " + candidate.getText() + " " + candidate.getContentDescription() + "\n";
    }
    str += "/******************************************/";
    Log.i("Chimp-wildCardManager",str);

    return baseUiObjects;
}
 
Example 2
Project: UIAutomatorWD   File: ElementController.java   Source Code and License 6 votes vote down vote up
private static BySelector getSelector(String strategy, String text) throws Exception {
    BySelector selector = null;
    switch (strategy) {
        case "CLASS_NAME":
            selector = By.clazz(text);
            break;
        case "NAME":
            selector = By.desc(text);
            if(selector == null || elements.getmDevice().findObject(selector) == null){
                selector = By.text(text);
            }
            break;
        case "ID":
            selector = By.res(text);
            break;
    }
    return selector;
}
 
Example 3
Project: android_packages_apps_tv   File: ParentalControlsTest.java   Source Code and License 6 votes vote down vote up
/**
 * Prepare the need for testRatingDependentSelect.
 * 1. Turn on parental control if it's off.
 * 2. Make sure Japan rating system is selected.
 */
private void prepareParentalControl() {
    showParentalControl();
    switchParentalControl(R.string.option_toggle_parental_controls_off);
    // Show all rating systems.
    mSidePanelHelper.assertNavigateToItem(R.string.option_program_restrictions);
    mDevice.pressDPadCenter();
    BySelector bySidePanel = mSidePanelHelper.bySidePanelTitled(
            R.string.option_program_restrictions);
    assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
    mSidePanelHelper.assertNavigateToItem(R.string.option_country_rating_systems);
    mDevice.pressDPadCenter();
    bySidePanel = mSidePanelHelper.bySidePanelTitled(R.string.option_country_rating_systems);
    assertWaitForCondition(mDevice,Until.hasObject(bySidePanel));
    mSidePanelHelper.assertNavigateToItem(R.string.option_see_all_rating_systems);
    mDevice.pressDPadCenter();
    // Make sure Japan rating system is selected.
    UiObject2 ratingSystemCheckBox = mSidePanelHelper.assertNavigateToItem("Japan")
            .findObject(ByResource.id(mTargetResources, R.id.check_box));
    if (!ratingSystemCheckBox.isChecked()) {
        mDevice.pressDPadCenter();
        getInstrumentation().waitForIdleSync();
    }
    mDevice.pressBack();
}
 
Example 4
Project: android_packages_apps_tv   File: ParentalControlsTest.java   Source Code and License 6 votes vote down vote up
private void showParentalControl() {
    // Show menu and select parental controls.
    mMenuHelper.showMenu();
    mMenuHelper.assertPressOptionsSettings();
    assertWaitForCondition(mDevice, Until.hasObject(mBySettingsSidePanel));
    mSidePanelHelper.assertNavigateToItem(R.string.settings_parental_controls);
    mDevice.pressDPadCenter();
    // Enter pin code.
    DialogHelper dialogHelper = new DialogHelper(mDevice, mTargetResources);
    dialogHelper.assertWaitForPinDialogOpen();
    dialogHelper.enterPinCodes();
    dialogHelper.assertWaitForPinDialogClose();
    BySelector bySidePanel = mSidePanelHelper.bySidePanelTitled(
            R.string.menu_parental_controls);
    assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
}
 
Example 5
Project: android_packages_apps_tv   File: PlayControlsRowViewTest.java   Source Code and License 6 votes vote down vote up
public void testKeepPausedWhileParentalControlChange() {
    // Pause the playback.
    mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_PAUSE);
    mMenuHelper.assertWaitForMenu();
    assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
    // Show parental controls fragment.
    mMenuHelper.assertPressOptionsSettings();
    assertWaitForCondition(mDevice, Until.hasObject(mBySettingsSidePanel));
    mSidePanelHelper.assertNavigateToItem(R.string.settings_parental_controls);
    mDevice.pressDPadCenter();
    DialogHelper dialogHelper = new DialogHelper(mDevice, mTargetResources);
    dialogHelper.assertWaitForPinDialogOpen();
    dialogHelper.enterPinCodes();
    dialogHelper.assertWaitForPinDialogClose();
    BySelector bySidePanel = mSidePanelHelper.bySidePanelTitled(
            R.string.menu_parental_controls);
    assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
    mDevice.pressEnter();
    mDevice.pressEnter();
    mDevice.pressBack();
    mDevice.pressBack();
    // Return to the main menu.
    mMenuHelper.assertWaitForMenu();
    assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
}
 
Example 6
Project: android_packages_apps_tv   File: LiveChannelsUiDeviceHelper.java   Source Code and License 6 votes vote down vote up
public void assertAppStarted() {
    Intent intent = mContext.getPackageManager()
            .getLaunchIntentForPackage(Constants.TV_APP_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    mContext.startActivity(intent);
    // Wait for idle state before checking the channel banner because waitForCondition() has
    // timeout.
    mUiDevice.waitForIdle();
    // Make sure that the activity is resumed.
    waitForCondition(mUiDevice, Until.hasObject(Constants.TV_VIEW));

    Assert.assertTrue(Constants.TV_APP_PACKAGE + " did not start", mUiDevice
            .wait(Until.hasObject(By.pkg(Constants.TV_APP_PACKAGE).depth(0)),
                    APPLICATION_START_TIMEOUT_MSEC));
    BySelector welcome = ByResource.id(mTargetResources, com.android.tv.R.id.intro);
    if (mUiDevice.hasObject(welcome)) {
        Log.i(TAG, "Welcome screen shown. Clearing dialog by pressing back");
        mUiDevice.pressBack();
    }
}
 
Example 7
Project: appium-uiautomator2-server   File: UiObject2Element.java   Source Code and License 6 votes vote down vote up
public Object getChild(final Object selector) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException {
    if (selector instanceof UiSelector) {
        /**
         * We can't find the child element with UiSelector on UiObject2,
         * as an alternative creating UiObject with UiObject2's AccessibilityNodeInfo
         * and finding the child element on UiObject.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);

        UiSelector uiSelector = new UiSelector();
        CustomUiSelector customUiSelector = new CustomUiSelector(uiSelector);
        uiSelector = customUiSelector.getUiSelector(nodeInfo);
        UiObject uiObject = (UiObject)  CustomUiDevice.getInstance().findObject(uiSelector);
        AccessibilityNodeInfo uiObject_nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);
        return uiObject.getChild((UiSelector) selector);
    }
    return element.findObject((BySelector) selector);
}
 
Example 8
Project: appium-uiautomator2-server   File: UiObject2Element.java   Source Code and License 6 votes vote down vote up
public List<Object> getChildren(final Object selector, final By by) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException {
    if (selector instanceof UiSelector) {
        /**
         * We can't find the child elements with UiSelector on UiObject2,
         * as an alternative creating UiObject with UiObject2's AccessibilityNodeInfo
         * and finding the child elements on UiObject.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);

        UiSelector uiSelector = new UiSelector();
        CustomUiSelector customUiSelector = new CustomUiSelector(uiSelector);
        uiSelector = customUiSelector.getUiSelector(nodeInfo);
        UiObject uiObject = (UiObject)  CustomUiDevice.getInstance().findObject(uiSelector);
        String id = UUID.randomUUID().toString();
        AndroidElement androidElement = getAndroidElement(id, uiObject, by);
        return androidElement.getChildren(selector, by);
    }
    return (List)element.findObjects((BySelector) selector);
}
 
Example 9
Project: ChimpCheck   File: Performer.java   Source Code and License 5 votes vote down vote up
public Performer(String displayAction, ChimpDriver chimpDriver, ViewManager viewManager,
                 WildCardManager wildCardManager, BySelector wildCardSelector, BySelector wildCardChildSelector,
                 Matcher<View> userDefinedMatcher){
    this.displayAction = displayAction;
    this.chimpDriver = chimpDriver;
    this.viewManager = viewManager;
    this.wildCardManager = wildCardManager;
    this.wildCardSelector = wildCardSelector;
    this.wildCardChildSelector = wildCardChildSelector;
    this.userDefinedMatcher = userDefinedMatcher;
}
 
Example 10
Project: FlickLauncher   File: LauncherInstrumentationTestCase.java   Source Code and License 5 votes vote down vote up
/**
 * Scrolls the {@param container} until it finds an object matching {@param condition}.
 * @return the matching object.
 */
protected UiObject2 scrollAndFind(UiObject2 container, BySelector condition) {
    do {
        UiObject2 widget = container.findObject(condition);
        if (widget != null) {
            return widget;
        }
    } while (container.scroll(Direction.DOWN, 1f));
    return container.findObject(condition);
}
 
Example 11
Project: UIAutomatorWD   File: ElementController.java   Source Code and License 5 votes vote down vote up
private static JSONArray getMultiElements(final BySelector sel) throws Exception {
    JSONArray res;
    List<Element> foundElements = new ArrayList<Element>();
    final List<Element> elementsFromSelector = getElements().getMultiElement(sel);
    foundElements.addAll(elementsFromSelector);
    res = elementsToJSONArray(foundElements);
    return res;
}
 
Example 12
Project: UIAutomatorWD   File: Elements.java   Source Code and License 5 votes vote down vote up
public Element getElement(BySelector sel) throws Exception {
	UiObject2 el = mDevice.findObject(sel);
	Element result = addElement(el);
	if (el != null) {
		return result;
	} else {
		throw new Exception("not found");
	}
}
 
Example 13
Project: UIAutomatorWD   File: Elements.java   Source Code and License 5 votes vote down vote up
public List<Element> getMultiElement(BySelector sel) throws Exception {
	List<UiObject2> el = mDevice.findObjects(sel);
	List<Element> result = addElements(el);
	if (result != null) {
		return result;
	} else {
		throw new Exception("not found");
	}
}
 
Example 14
Project: SimpleUILauncher   File: LauncherInstrumentationTestCase.java   Source Code and License 5 votes vote down vote up
/**
 * Scrolls the {@param container} until it finds an object matching {@param condition}.
 * @return the matching object.
 */
protected UiObject2 scrollAndFind(UiObject2 container, BySelector condition) {
    do {
        UiObject2 widget = container.findObject(condition);
        if (widget != null) {
            return widget;
        }
    } while (container.scroll(Direction.DOWN, 1f));
    return container.findObject(condition);
}
 
Example 15
Project: quickstart-android   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testLaunchDialog() {
    // Floating action button
    BySelector fabSelector = By.res(APP_PACKAGE, "invite_button");

    // Click floating action button
    Assert.assertTrue(mDevice.wait(Until.hasObject(fabSelector), UI_TIMEOUT));
    mDevice.findObject(fabSelector).click();

    // Radio button in the account picker dialog
    BySelector firstAccountSelector = By.clazz(CLASS_CHECKED_TEXT_VIEW);

    // Wait for account picker (may not appear)
    if (mDevice.wait(Until.hasObject(firstAccountSelector), UI_TIMEOUT)) {
        // Click first account
        mDevice.findObjects(firstAccountSelector).get(0).click();

        // Button with the text "OK" (enabled)
        BySelector okButtonSelector = By.clazz(CLASS_BUTTON).text("OK").enabled(true);

        // Click OK on account picker
        Assert.assertTrue(mDevice.wait(Until.hasObject(okButtonSelector), UI_TIMEOUT));
        mDevice.findObject(okButtonSelector).click();
    }

    // Check that we can see the title and message
    String titleText = mContext.getString(R.string.invitation_title);
    String messageText = mContext.getString(R.string.invitation_message);

    BySelector titleTextSelector = By.text(titleText);
    BySelector messageTextSelector = By.text(messageText);

    assertTrue(mDevice.wait(Until.hasObject(titleTextSelector), UI_TIMEOUT));
    assertTrue(mDevice.wait(Until.hasObject(messageTextSelector), UI_TIMEOUT));

    // Back out of the dialog
    mDevice.pressBack();
}
 
Example 16
Project: android_packages_apps_tv   File: LiveChannelsAppTest.java   Source Code and License 5 votes vote down vote up
public void testSettingsCancel() {
    mMenuHelper.assertPressOptionsSettings();
    BySelector byChannelSourcesSidePanel = mSidePanelHelper
            .bySidePanelTitled(R.string.settings_channel_source_item_customize_channels);
    assertWaitForCondition(mDevice, Until.hasObject(byChannelSourcesSidePanel));
    mDevice.pressBack();
    assertWaitForCondition(mDevice, Until.gone(byChannelSourcesSidePanel));
    assertHas(mDevice, Constants.MENU, false);
}
 
Example 17
Project: android_packages_apps_tv   File: LiveChannelsAppTest.java   Source Code and License 5 votes vote down vote up
public void testClosedCaptionsCancel() {
    mMenuHelper.assertPressOptionsClosedCaptions();
    BySelector byClosedCaptionSidePanel = mSidePanelHelper
            .bySidePanelTitled(R.string.side_panel_title_closed_caption);
    assertWaitForCondition(mDevice, Until.hasObject(byClosedCaptionSidePanel));
    mDevice.pressBack();
    assertWaitForCondition(mDevice, Until.gone(byClosedCaptionSidePanel));
    assertHas(mDevice, Constants.MENU, false);
}
 
Example 18
Project: android_packages_apps_tv   File: LiveChannelsAppTest.java   Source Code and License 5 votes vote down vote up
public void testDisplayModeCancel() {
    ChannelStateData data = new ChannelStateData();
    data.mTvTrackInfos.add(com.android.tv.testing.Constants.SVGA_VIDEO_TRACK);
    data.mSelectedVideoTrackId = com.android.tv.testing.Constants.SVGA_VIDEO_TRACK
            .getId();
    updateThenTune(data, TvTestInputConstants.CH_2);

    mMenuHelper.assertPressOptionsDisplayMode();
    BySelector byDisplayModeSidePanel = mSidePanelHelper
            .bySidePanelTitled(R.string.side_panel_title_display_mode);
    assertWaitForCondition(mDevice, Until.hasObject(byDisplayModeSidePanel));
    mDevice.pressBack();
    assertWaitForCondition(mDevice, Until.gone(byDisplayModeSidePanel));
    assertHas(mDevice, Constants.MENU, false);
}
 
Example 19
Project: android_packages_apps_tv   File: LiveChannelsAppTest.java   Source Code and License 5 votes vote down vote up
public void testMultiAudioCancel() {
    ChannelStateData data = new ChannelStateData();
    data.mTvTrackInfos.add(com.android.tv.testing.Constants.GENERIC_AUDIO_TRACK);
    updateThenTune(data, TvTestInputConstants.CH_2);

    mMenuHelper.assertPressOptionsMultiAudio();
    BySelector byMultiAudioSidePanel = mSidePanelHelper
            .bySidePanelTitled(R.string.side_panel_title_multi_audio);
    assertWaitForCondition(mDevice, Until.hasObject(byMultiAudioSidePanel));
    mDevice.pressBack();
    assertWaitForCondition(mDevice, Until.gone(byMultiAudioSidePanel));
    assertHas(mDevice, Constants.MENU, false);
}
 
Example 20
Project: android_packages_apps_tv   File: ParentalControlsTest.java   Source Code and License 5 votes vote down vote up
public void testRatingDependentSelect() {
    // Show ratings fragment.
    BySelector bySidePanel = mSidePanelHelper.bySidePanelTitled(
            R.string.option_program_restrictions);
    assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
    mSidePanelHelper.assertNavigateToItem(R.string.option_ratings);
    mDevice.pressDPadCenter();
    // Block rating 6 and rating 12. Check if dependent select works well.
    bySidePanel = mSidePanelHelper.bySidePanelTitled(R.string.option_ratings);
    assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
    // Test on blocking and unblocking Japanese rating.
    int blockAge = 6;
    int unBlockAge = 12;
    int maxAge = 20;
    int minAge = 4;
    for (int age = minAge; age <= maxAge; age++) {
        UiObject2 ratingCheckBox = mSidePanelHelper.assertNavigateToItem(String.valueOf(age))
                .findObject(ByResource.id(mTargetResources, R.id.check_box));
        if (ratingCheckBox.isChecked()) {
            mDevice.pressDPadCenter();
        }
    }
    mSidePanelHelper.assertNavigateToItem(String.valueOf(blockAge));
    mDevice.pressDPadCenter();
    assertRatingViewIsChecked(minAge, maxAge, blockAge, true);
    mSidePanelHelper.assertNavigateToItem(String.valueOf(unBlockAge));
    mDevice.pressDPadCenter();
    assertRatingViewIsChecked(minAge, maxAge, unBlockAge, false);
    mDevice.pressBack();
    mDevice.pressBack();
    getInstrumentation().waitForIdleSync();
}
 
Example 21
Project: android_packages_apps_tv   File: ParentalControlsTest.java   Source Code and License 5 votes vote down vote up
private void switchParentalControl(int oppositeStateResId) {
    BySelector bySidePanel = mSidePanelHelper.byViewText(oppositeStateResId);
    if (mDevice.hasObject(bySidePanel)) {
        mSidePanelHelper.assertNavigateToItem(oppositeStateResId);
        mDevice.pressDPadCenter();
        getInstrumentation().waitForIdleSync();
    }
}
 
Example 22
Project: android_packages_apps_tv   File: SidePanelHelper.java   Source Code and License 5 votes vote down vote up
public UiObject2 assertNavigateToItem(String title) {
    BySelector sidePanelSelector = ByResource.id(mTargetResources, R.id.side_panel_list);
    UiObject2 sidePanelList = mUiDevice.findObject(sidePanelSelector);
    Assert.assertNotNull(sidePanelSelector + " not found", sidePanelList);

    return UiDeviceAsserts
            .assertNavigateTo(mUiDevice, sidePanelList, By.hasDescendant(By.text(title)),
                    Direction.DOWN);
}
 
Example 23
Project: android_packages_apps_tv   File: UiDeviceAsserts.java   Source Code and License 5 votes vote down vote up
/**
 * Navigates through the focus items in a container returning the container child that has a
 * descendant matching the {@code selector}.
 * <p>
 * The navigation starts in the {@code direction} specified and
 * {@link Direction#reverse(Direction) reverses} once if needed. Fails if there is not a
 * focused
 * descendant, or if after completing both directions no focused child has a descendant
 * matching
 * {@code selector}.
 * <p>
 * Fails if the menu item can not be navigated to.
 *
 * @param uiDevice  the device under test.
 * @param container contains children to navigate over.
 * @param selector  the selector for the object to navigate to.
 * @param direction the direction to start navigating.
 * @return the object navigated to.
 */
public static UiObject2 assertNavigateTo(UiDevice uiDevice, UiObject2 container,
        BySelector selector, Direction direction) {
    int count = 0;
    while (count < 2) {
        BySelector hasFocusedDescendant = By.hasDescendant(FOCUSED_VIEW);
        UiObject2 focusedChild = null;
        SearchCondition<Boolean> untilHasFocusedDescendant = Until
                .hasObject(hasFocusedDescendant);

        boolean result = container.wait(untilHasFocusedDescendant,
                UiObject2Asserts.getAdjustedTimeout(Constants.MAX_SHOW_DELAY_MILLIS));
        if (!result) {
            // HACK: Try direction anyways because play control does not always have a
            // focused item.
            UiDeviceUtils.pressDpad(uiDevice, direction);
            UiObject2Asserts.assertWaitForCondition(container, untilHasFocusedDescendant);
        }

        for (UiObject2 c : container.getChildren()) {
            if (c.isFocused() || c.hasObject(hasFocusedDescendant)) {
                focusedChild = c;
                break;
            }
        }
        if (focusedChild == null) {
            Assert.fail("No focused item found in container " + container);
        }
        if (focusedChild.hasObject(selector)) {
            return focusedChild;
        }
        if (!UiObject2Utils.hasSiblingInDirection(focusedChild, direction)) {
            direction = Direction.reverse(direction);
            count++;
        }
        UiDeviceUtils.pressDpad(uiDevice, direction);
    }
    Assert.fail("Could not find item with  " + selector);
    return null;
}
 
Example 24
Project: appium-uiautomator2-server   File: ClassInstancePair.java   Source Code and License 5 votes vote down vote up
public BySelector getSelector() {
    String androidClass = getAndroidClass();

    //TODO: remove below comments once code get reviewed
    //below commented line related to UiAutomator V1(bootstrap) version, as we don't have possibility
    // in V2 version to use instance, so directly returning By.clazz
    // new UiSelector().className(androidClass).instance(Integer.parseInt(instance));
    return By.clazz(androidClass);
}
 
Example 25
Project: appium-uiautomator2-server   File: KnownElements.java   Source Code and License 5 votes vote down vote up
/**
 *
 * @param ui2BySelector, for finding {@link android.support.test.uiautomator.UiObject2} element derived using {@link By}
 * @param by, user provided selector criteria from appium client.
 * @return
 */
public static AndroidElement geElement(final BySelector ui2BySelector, By by) throws ElementNotFoundException, InvalidSelectorException, UiAutomator2Exception, ClassNotFoundException {
    Object ui2Object = getInstance().findObject(ui2BySelector);
    if (ui2Object == null) {
        throw new ElementNotFoundException();
    }
    String id = UUID.randomUUID().toString();
    AndroidElement androidElement = getAndroidElement(id, ui2Object, by);
    cache.put(androidElement.getId(), androidElement);
    return androidElement;
}
 
Example 26
Project: appium-uiautomator2-server   File: UiObjectElement.java   Source Code and License 5 votes vote down vote up
public Object getChild(final Object selector) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException {
    if (selector instanceof BySelector) {
        /**
         * We can't find the child element with BySelector on UiObject,
         * as an alternative creating UiObject2 with UiObject's AccessibilityNodeInfo
         * and finding the child element on UiObject2.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);
        UiObject2 uiObject2 = (UiObject2) CustomUiDevice.getInstance().findObject(nodeInfo);
        return uiObject2.findObject((BySelector) selector);
    }
    return element.getChild((UiSelector) selector);
}
 
Example 27
Project: appium-uiautomator2-server   File: UiObjectElement.java   Source Code and License 5 votes vote down vote up
public List<Object> getChildren(final Object selector, final By by) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException {
    if (selector instanceof BySelector) {
        /**
         * We can't find the child elements with BySelector on UiObject,
         * as an alternative creating UiObject2 with UiObject's AccessibilityNodeInfo
         * and finding the child elements on UiObject2.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);
        UiObject2 uiObject2 = (UiObject2) CustomUiDevice.getInstance().findObject(nodeInfo);
        return (List)uiObject2.findObjects((BySelector) selector);
    }
    return (List)this.getChildElements((UiSelector) selector);
}
 
Example 28
Project: GCM-Sample   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Click sign-in button, select account at account selector, accept consent screen, and
 * check that in the end the app is in the signed-in state.
 */
private void signInTest() {
    // Sign-in button (enabled)
    BySelector signInButtonSelector = By.clazz(CLASS_BUTTON).textContains("Sign in").enabled(true);

    // Click Sign-in button (must be enabled)
    assertTrue(mDevice.wait(Until.hasObject(signInButtonSelector), UI_TIMEOUT));
    mDevice.findObject(signInButtonSelector).click();

    // Radio button in the account picker dialog
    BySelector firstAccountSelector = By.clazz(CLASS_CHECKED_TEXT_VIEW);

    // Wait for account picker (may not appear)
    if (mDevice.wait(Until.hasObject(firstAccountSelector), UI_TIMEOUT)) {
        // Click first account
        mDevice.findObjects(firstAccountSelector).get(0).click();

        // Button with the text "OK" (enabled)
        BySelector okButtonSelector = By.clazz(CLASS_BUTTON).text("OK").enabled(true);

        // Click OK on account picker
        assertTrue(mDevice.wait(Until.hasObject(okButtonSelector), UI_TIMEOUT));
        mDevice.findObject(okButtonSelector).click();
    }

    // The Google Play Services consent screen accept button
    BySelector acceptButtonSelector = By.res(GMS_PACKAGE, "accept_button");

    // Accept consent screen and click OK button (this also may not appear)
    if (mDevice.wait(Until.hasObject(acceptButtonSelector), UI_TIMEOUT)) {
        mDevice.findObject(acceptButtonSelector).click();
    }

    // Check that the UI shows signed-in state
    assertTrue(isSignedIn());
}
 
Example 29
Project: GCM-Sample   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Click the disconnect button, check the app is in the signed-out state.
 */
private void disconnectTest() {
    // Disconnect button (enabled)
    String disconnectText = mContext.getString(R.string.disconnect);
    BySelector disconnectSelector = By.clazz(CLASS_BUTTON).text(disconnectText).enabled(true);

    // Click disconnect button
    assertTrue(mDevice.wait(Until.hasObject(disconnectSelector), UI_TIMEOUT));
    mDevice.findObject(disconnectSelector).click();

    // Check that we get to signed-out state
    String signedOutText = mContext.getString(R.string.signed_out);
    BySelector signedOutTextSelector = By.text(signedOutText);
    assertTrue(mDevice.wait(Until.hasObject(signedOutTextSelector), UI_TIMEOUT));
}
 
Example 30
Project: GCM-Sample   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Checks the UI for text containing "Signed in" to determine if the user is signed in.
 */
private boolean isSignedIn() {
    // Wait until we have an enabled button on screen
    mDevice.wait(Until.hasObject(By.clazz(CLASS_BUTTON).enabled(true)), UI_TIMEOUT);

    String signedInText = mContext.getString(R.string.signed_in_fmt, "");
    BySelector signedInTextSelector = By.textContains(signedInText);
    return mDevice.wait(Until.hasObject(signedInTextSelector), UI_TIMEOUT);
}
 
Example 31
Project: GCM-Sample   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testLaunchDialog() {
    // Floating action button
    BySelector fabSelector = By.res(APP_PACKAGE, "invite_button");

    // Click floating action button
    Assert.assertTrue(mDevice.wait(Until.hasObject(fabSelector), UI_TIMEOUT));
    mDevice.findObject(fabSelector).click();

    // Radio button in the account picker dialog
    BySelector firstAccountSelector = By.clazz(CLASS_CHECKED_TEXT_VIEW);

    // Wait for account picker (may not appear)
    if (mDevice.wait(Until.hasObject(firstAccountSelector), UI_TIMEOUT)) {
        // Click first account
        mDevice.findObjects(firstAccountSelector).get(0).click();

        // Button with the text "OK" (enabled)
        BySelector okButtonSelector = By.clazz(CLASS_BUTTON).text("OK").enabled(true);

        // Click OK on account picker
        Assert.assertTrue(mDevice.wait(Until.hasObject(okButtonSelector), UI_TIMEOUT));
        mDevice.findObject(okButtonSelector).click();
    }

    // Check that we can see the title and message
    String titleText = mContext.getString(R.string.invitation_title);
    String messageText = mContext.getString(R.string.invitation_message);

    BySelector titleTextSelector = By.text(titleText);
    BySelector messageTextSelector = By.text(messageText);

    assertTrue(mDevice.wait(Until.hasObject(titleTextSelector), UI_TIMEOUT));
    assertTrue(mDevice.wait(Until.hasObject(messageTextSelector), UI_TIMEOUT));

    // Back out of the dialog
    mDevice.pressBack();
}
 
Example 32
Project: google-services   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Click sign-in button, select account at account selector, accept consent screen, and
 * check that in the end the app is in the signed-in state.
 */
private void signInTest() {
    // Sign-in button (enabled)
    BySelector signInButtonSelector = By.clazz(CLASS_BUTTON).textContains("Sign in").enabled(true);

    // Click Sign-in button (must be enabled)
    assertTrue(mDevice.wait(Until.hasObject(signInButtonSelector), UI_TIMEOUT));
    mDevice.findObject(signInButtonSelector).click();

    // Radio button in the account picker dialog
    BySelector firstAccountSelector = By.clazz(CLASS_CHECKED_TEXT_VIEW);

    // Wait for account picker (may not appear)
    if (mDevice.wait(Until.hasObject(firstAccountSelector), UI_TIMEOUT)) {
        // Click first account
        mDevice.findObjects(firstAccountSelector).get(0).click();

        // Button with the text "OK" (enabled)
        BySelector okButtonSelector = By.clazz(CLASS_BUTTON).text("OK").enabled(true);

        // Click OK on account picker
        assertTrue(mDevice.wait(Until.hasObject(okButtonSelector), UI_TIMEOUT));
        mDevice.findObject(okButtonSelector).click();
    }

    // The Google Play Services consent screen accept button
    BySelector acceptButtonSelector = By.res(GMS_PACKAGE, "accept_button");

    // Accept consent screen and click OK button (this also may not appear)
    if (mDevice.wait(Until.hasObject(acceptButtonSelector), UI_TIMEOUT)) {
        mDevice.findObject(acceptButtonSelector).click();
    }

    // Check that the UI shows signed-in state
    assertTrue(isSignedIn());
}
 
Example 33
Project: google-services   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Click the disconnect button, check the app is in the signed-out state.
 */
private void disconnectTest() {
    // Disconnect button (enabled)
    String disconnectText = mContext.getString(R.string.disconnect);
    BySelector disconnectSelector = By.clazz(CLASS_BUTTON).text(disconnectText).enabled(true);

    // Click disconnect button
    assertTrue(mDevice.wait(Until.hasObject(disconnectSelector), UI_TIMEOUT));
    mDevice.findObject(disconnectSelector).click();

    // Check that we get to signed-out state
    String signedOutText = mContext.getString(R.string.signed_out);
    BySelector signedOutTextSelector = By.text(signedOutText);
    assertTrue(mDevice.wait(Until.hasObject(signedOutTextSelector), UI_TIMEOUT));
}
 
Example 34
Project: google-services   File: UiAutomatorTest.java   Source Code and License 5 votes vote down vote up
/**
 * Checks the UI for text containing "Signed in" to determine if the user is signed in.
 */
private boolean isSignedIn() {
    // Wait until we have an enabled button on screen
    mDevice.wait(Until.hasObject(By.clazz(CLASS_BUTTON).enabled(true)), UI_TIMEOUT);

    String signedInText = mContext.getString(R.string.signed_in_fmt, "");
    BySelector signedInTextSelector = By.textContains(signedInText);
    return mDevice.wait(Until.hasObject(signedInTextSelector), UI_TIMEOUT);
}
 
Example 35
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 5 votes vote down vote up
/**
 * Find a view based on the prefixed text in the view. The matching is case-insensitive.
 *
 * @param text Prefix to search for.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withTextStartingWith(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .textStartsWith(text);
    BySelector bySelector = By.textStartsWith(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example 36
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 5 votes vote down vote up
/**
 * Find a view based on the text contained within the view. The matching is case-sensitive.
 *
 * @param text Text to search for inside a view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withTextContaining(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .textContains(text);
    BySelector bySelector = By.textContains(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example 37
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 5 votes vote down vote up
/**
 * Find a view based on the exact text contained within the view. Matching is case-insensitive.
 *
 * @param text Exact text in the view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withText(String text, Class klass) {
    Pattern pattern = Pattern.compile("(?i)" + Pattern.quote(text));

    UiSelector uiSelector = new UiSelector()
            .textMatches(pattern.pattern());
    BySelector bySelector = By.text(pattern);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example 38
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 5 votes vote down vote up
/**
 * Find a view based on the resource id. Resource ids should be the fully qualified id,
 * ex: com.android.browser:id/url
 *
 * @param id The fully qualified id of the view, ex: com.android.browser:id/url
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withResourceId(String id, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .resourceId(id);
    BySelector bySelector = By.res(id);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example 39
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 5 votes vote down vote up
/**
 * Find a view based on it's class.
 *
 * @param klass The class of the view to find.
 * @return
 */
public static UiObjectMatcher withClass(Class klass) {
    UiSelector uiSelector = new UiSelector()
            .className(klass);
    BySelector bySelector = By.clazz(klass);

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example 40
Project: ChimpCheck   File: SwipePerformer.java   Source Code and License 4 votes vote down vote up
public SwipePerformer(ChimpDriver chimpDriver, ViewManager viewManager,
                     WildCardManager wildCardManager, BySelector wildCardSelector, BySelector wildCardChildSelector,
                     Matcher<View> userDefinedMatcher) {
    super("Swipe", chimpDriver, viewManager, wildCardManager, wildCardSelector, wildCardChildSelector,userDefinedMatcher);
}
 
Example 41
Project: ChimpCheck   File: TypePerformer.java   Source Code and License 4 votes vote down vote up
public TypePerformer(ChimpDriver chimpDriver, ViewManager viewManager,
                      WildCardManager wildCardManager, BySelector wildCardSelector, BySelector wildCardChildSelector,
                      Matcher<View> userDefinedMatcher) {
    super("Type", chimpDriver, viewManager, wildCardManager, wildCardSelector, wildCardChildSelector,userDefinedMatcher);
}
 
Example 42
Project: ChimpCheck   File: ClickPerformer.java   Source Code and License 4 votes vote down vote up
public ClickPerformer(ChimpDriver chimpDriver, ViewManager viewManager,
                      WildCardManager wildCardManager, BySelector wildCardSelector, BySelector wildCardChildSelector,
                      Matcher<View> userDefinedMatcher) {
    super("Click", chimpDriver, viewManager, wildCardManager, wildCardSelector, wildCardChildSelector, userDefinedMatcher);
}
 
Example 43
Project: ChimpCheck   File: LongClickPerformer.java   Source Code and License 4 votes vote down vote up
public LongClickPerformer(ChimpDriver chimpDriver, ViewManager viewManager,
                          WildCardManager wildCardManager, BySelector wildCardSelector, BySelector wildCardChildSelector,
                          Matcher<View> userDefinedMatcher) {
    super("Click", chimpDriver, viewManager, wildCardManager, wildCardSelector, wildCardChildSelector, userDefinedMatcher);
}
 
Example 44
Project: ChimpCheck   File: WildCardManager.java   Source Code and License 4 votes vote down vote up
public ArrayList<UiObject2> retrieveTopLevelUiObjects(BySelector uiSelector) throws UiObjectNotFoundException {
    int i = 0;
    ArrayList<UiObject2> uiObjects = (ArrayList<UiObject2>) mDevice.findObjects(uiSelector);
    return uiObjects;
}
 
Example 45
Project: ChimpCheck   File: WildCardManager.java   Source Code and License 4 votes vote down vote up
public ArrayList<UiObject2> retrieveTopLevelUIObjects2(BySelector top){
    return (ArrayList<UiObject2>)mDevice.findObjects(top);
}
 
Example 46
Project: orgzly-android   File: ListWidgetTest.java   Source Code and License 4 votes vote down vote up
private UiObject2 findObject(BySelector by) {
    return device.wait(Until.findObject(by), 1000);
}
 
Example 47
Project: orgzly-android   File: ListWidgetTest.java   Source Code and License 4 votes vote down vote up
private List<UiObject2> findObjects(BySelector by) {
    return device.wait(Until.findObjects(by), 1000);
}
 
Example 48
Project: FlickLauncher   File: LauncherInstrumentationTestCase.java   Source Code and License 4 votes vote down vote up
protected BySelector getSelectorForId(int id) {
    String name = mTargetContext.getResources().getResourceEntryName(id);
    return By.res(mTargetPackage, name);
}
 
Example 49
Project: UIAutomatorWD   File: ElementController.java   Source Code and License 4 votes vote down vote up
private static JSONObject getOneElement(final BySelector sel) throws Exception {
    final JSONObject res = new JSONObject();
    final Element element = getElements().getElement(sel);
    res.put("ELEMENT", element.getId());
    return res;
}
 
Example 50
Project: SimpleUILauncher   File: LauncherInstrumentationTestCase.java   Source Code and License 4 votes vote down vote up
protected BySelector getSelectorForId(int id) {
    String name = mTargetContext.getResources().getResourceEntryName(id);
    return By.res(mTargetPackage, name);
}
 
Example 51
Project: android_packages_apps_tv   File: SidePanelHelper.java   Source Code and License 4 votes vote down vote up
public BySelector bySidePanelTitled(int titleResId) {
    return By.copy(Constants.SIDE_PANEL)
            .hasDescendant(ByResource.text(mTargetResources, titleResId));
}
 
Example 52
Project: android_packages_apps_tv   File: SidePanelHelper.java   Source Code and License 4 votes vote down vote up
public BySelector byViewText(int textResId) {
    return By.hasDescendant(ByResource.text(mTargetResources, textResId));
}
 
Example 53
Project: android_packages_apps_tv   File: MenuHelper.java   Source Code and License 4 votes vote down vote up
public BySelector getByChannels() {
    return byChannels;
}
 
Example 54
Project: android_packages_apps_tv   File: UiDeviceAsserts.java   Source Code and License 4 votes vote down vote up
public static void assertHas(UiDevice uiDevice, BySelector bySelector, boolean expected) {
    assertEquals("Has " + bySelector, expected, uiDevice.hasObject(bySelector));
}
 
Example 55
Project: ReactiveStopwatch   File: StopwatchAcceptanceTest.java   Source Code and License 4 votes vote down vote up
private static BySelector byTextIgnoreCase(@NonNull String text) {
    return By.text(caseInsensitivePatternFrom(text));
}
 
Example 56
Project: SmoothClicker   File: ItSettingsActivity.java   Source Code and License 4 votes vote down vote up
/**
 * @param index - The idnex of the field in the list (start at 0)
 * @param text - The text of the field to get
 */
private void testFieldWithName( int index, String text ){

    if ( text == null || text.length() <= 0 || index < 0){
        fail("Wrong test");
    }

    final String DUMMY_TEXT = "Hello world";

    try {
        // Display the dialog
        UiObject field = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.TextView")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/title")
                        .text(text)
        );
        field.click();
        // Change the value
        field = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.EditText")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/edit")
        );
        final String BACKUP_TEXT = field.getText();
        field.setText(DUMMY_TEXT);
        // Confirm
        UiObject button = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.Button")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/button1")
        );
        button.click();
        w(1000);
        // Check the summary
        BySelector checkboxSettingsSelector = By.clazz("android.widget.TextView");
        List<UiObject2> summaries = mDevice.findObjects(checkboxSettingsSelector);
        UiObject2 field2 = summaries.get( index * 4 + 2 );
        assertEquals(DUMMY_TEXT, field2.getText());
        // Reset the value
        resetFieldWithText( DUMMY_TEXT, BACKUP_TEXT );
    } catch ( UiObjectNotFoundException uonfe ){
        uonfe.printStackTrace();
        fail(uonfe.getMessage());
    }

}
 
Example 57
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 4 votes vote down vote up
public UiObjectMatcher(UiSelector uiSelector, BySelector bySelector) {
    mUiSelector = uiSelector;
    mBySelector = bySelector;
}
 
Example 58
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 4 votes vote down vote up
public BySelector getBySelector() {
    return mBySelector;
}
 
Example 59
Project: android_packages_apps_tv   File: MenuHelper.java   Source Code and License 3 votes vote down vote up
/**
 * Navigate to the menu item with the text {@code itemTextResId} in the row with text
 * {@code rowTitleResId}.
 * <p>
 * Fails if the menu item can not be navigated to.
 *
 * @param rowTitleResId the resource id of the string in the desired row title.
 * @param itemTextResId the resource id of the string in the desired item.
 * @return the item navigated to.
 */
public UiObject2 assertNavigateToMenuItem(int rowTitleResId, int itemTextResId) {
    UiObject2 row = assertNavigateToRow(rowTitleResId);
    BySelector byListView = ByResource.id(mTargetResources, R.id.list_view);
    UiObject2 listView = row.findObject(byListView);
    Assert.assertNotNull(
            "Menu row '" + mTargetResources.getString(rowTitleResId) + "' does not have a "
                    + byListView, listView);
    return assertNavigateToRowItem(listView, itemTextResId);
}
 
Example 60
Project: device-automator   File: UiObjectMatcher.java   Source Code and License 3 votes vote down vote up
/**
 * Find a view based on the content description of the view. The content-description is typically used by the
 * Android Accessibility framework to provide an audio prompt for the widget when the widget is selected.
 * The content-description for the widget must match exactly with the string in your input argument.
 * Matching is case-sensitive.
 *
 * On {@link android.os.Build.VERSION_CODES#LOLLIPOP} devices and higher than matcher can be
 * used to match views in {@link android.webkit.WebView}s and browsers.
 *
 * @param text Content description of the view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withContentDescription(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .description(text);
    BySelector bySelector = By.desc(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}