androidx.test.uiautomator.UiScrollable Java Examples

The following examples show how to use androidx.test.uiautomator.UiScrollable. 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: URLExceptionTest.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
private void OpenExceptionsFragment() throws UiObjectNotFoundException {
    mDevice.waitForIdle();
    openSettings();

    onView(withText("Privacy & Security"))
            .check(matches(isDisplayed()))
            .perform(click());

    /* change locale to non-english in the setting, verify the locale is changed */
    UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
    appViews.scrollIntoView(exceptionsTitle);
    if (exceptionsTitle.isEnabled()) {
        exceptionsTitle.click();
    }

    mDevice.waitForIdle();
}
 
Example #2
Source File: URLExceptionTest.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void AddAndRemoveExceptionTest() throws UiObjectNotFoundException {
    addException(site);

    OpenExceptionsFragment();

    onView(allOf(withText(site), withId(R.id.domainView)))
            .check(matches(isDisplayed()));
    mDevice.waitForIdle();

    removeException();
    mDevice.waitForIdle();

    // Should be in main menu
    onView(withText("Privacy & Security"))
            .check(matches(isDisplayed()));

    /* change locale to non-english in the setting, verify the locale is changed */
    UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
    appViews.scrollIntoView(exceptionsTitle);

    onView(withText("Exceptions"))
            .check(matches(isDisplayed()))
            .check(matches(not(isEnabled())));
}
 
Example #3
Source File: URLExceptionTest.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void removeAllExceptionsTest() throws UiObjectNotFoundException {
    addException(site);

    OpenExceptionsFragment();

    mDevice.waitForIdle();
    onView(withId(R.id.removeAllExceptions))
            .perform(click());

    mDevice.waitForIdle();

    // Should be in main menu
    onView(withText("Privacy & Security"))
            .check(matches(isDisplayed()));

    /* change locale to non-english in the setting, verify the locale is changed */
    UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
    appViews.scrollIntoView(exceptionsTitle);

    onView(withText("Exceptions"))
            .check(matches(isDisplayed()))
            .check(matches(not(isEnabled())));
}
 
Example #4
Source File: Device.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
public static void scrollToElement(@Nullable UiScrollable origin, UiSelector selector,
                                   @Nullable Integer maxSwipes) throws UiObjectNotFoundException {
    UiScrollable scrollableOrigin = origin == null
            ? new UiScrollable(new UiSelector().scrollable(true).instance(0))
            : origin;
    Logger.debug(String.format("Using %s as scrolling origin", scrollableOrigin.getSelector()));
    String hScrollViewClassName = android.widget.HorizontalScrollView.class.getName();
    if (Objects.equals(scrollableOrigin.getClassName(), hScrollViewClassName)) {
        scrollableOrigin.setAsHorizontalList();
    }

    int originalMaxSwipes = scrollableOrigin.getMaxSearchSwipes();
    if (maxSwipes != null && maxSwipes > 0) {
        scrollableOrigin.setMaxSearchSwipes(maxSwipes);
    }
    try {
        if (!scrollableOrigin.scrollIntoView(selector)) {
            throw new UiObjectNotFoundException(String.format("Cannot scroll to %s", selector));
        }
    } finally {
        // The number of search swipes is held in a static property of the UiScrollable class.
        // Whenever a non-default number of search swipes is used during the scroll, we must
        // always restore the setting after the operation.
        scrollableOrigin.setMaxSearchSwipes(originalMaxSwipes);
    }
}
 
Example #5
Source File: MainActivityTest.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Test public void testAddItemsAndReview() throws Exception {
  mActivityTestRule.launchActivity(new Intent());

  // Input email for account created in the setup.sh
  getById("email").setText("[email protected]");
  closeKeyboard();
  getById("button_next").clickAndWaitForNewWindow(TIMEOUT);

  //Input password
  getById("password").setText("password");
  closeKeyboard();
  getById("button_done").clickAndWaitForNewWindow(TIMEOUT);

  // Add random items
  getActionBarItem(new UiSelector().textContains("Add Random Items"), TIMEOUT).click();
  device.waitForIdle(TIMEOUT);

  // Click on the first restaurant
  getById("recycler_restaurants").getChild(new UiSelector().index(0))
      .clickAndWaitForNewWindow(TIMEOUT);

  // Click add review
  getById("fabShowRatingDialog").click();

  //Write a review
  getById("restaurant_form_text").setText("\uD83D\uDE0E\uD83D\uDE00");
  closeKeyboard();

  //Submit the review
  getById("restaurant_form_button").clickAndWaitForNewWindow(TIMEOUT);

  // Assert that the review exists
  UiScrollable ratingsList = new UiScrollable(getIdSelector("recyclerRatings"));
  ratingsList.waitForExists(TIMEOUT);
  ratingsList.scrollToBeginning(100);
  Assert.assertTrue(
      getById("recyclerRatings")
          .getChild(new UiSelector().text("\uD83D\uDE0E\uD83D\uDE00"))
          .waitForExists(TIMEOUT));
}
 
Example #6
Source File: SwitchLocaleTest.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void FrenchLocaleTest() throws UiObjectNotFoundException {

    UiObject frenchMenuItem = TestHelper.mDevice.findObject(new UiSelector()
            .className("android.widget.TextView")
            .text("Valeur par défaut du système"));
    UiObject englishMenuItem = TestHelper.mDevice.findObject(new UiSelector()
            .className("android.widget.TextView")
            .text("English (United States)"));
    UiObject englishLocaleinFr = TestHelper.mDevice.findObject(new UiSelector()
            .className("android.widget.CheckedTextView")
            .text("English (United States)"));

    /* Go to Settings */
    TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);

    openSettings();
    frenchGeneralHeading.waitForExists(waitingTime);
    frenchGeneralHeading.click();

    languageHeading.waitForExists(waitingTime);

    /* system locale is in French, check it is now set to system locale */
    frenchHeading.waitForExists(waitingTime);
    Assert.assertTrue(frenchHeading.exists());
    Assert.assertTrue(frenchMenuItem.exists());
    languageHeading.click();
    Assert.assertTrue(sysDefaultLocale.isChecked());

    /* change locale to English in the setting, verify the locale is changed */
    UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
    appViews.scrollIntoView(englishLocaleinFr);
    Assert.assertTrue(englishLocaleinFr.isClickable());
    englishLocaleinFr.click();
    englishHeading.waitForExists(waitingTime);
    Assert.assertTrue(englishHeading.exists());
    Assert.assertTrue(englishMenuItem.exists());
    mDevice.pressBack();
}
 
Example #7
Source File: UiScrollableParser.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Override
public UiSelector parse() throws UiSelectorSyntaxException, UiObjectNotFoundException {
    resetCurrentIndex();
    UiObject self = consumeConstructor();
    List<Object> results = new ArrayList<>();
    while (hasMoreDataToParse()) {
        consumePeriod();
        Object result = consumeMethodCall();
        if (result == null) {
            continue;
        }

        results.add(result);
        if (result instanceof UiScrollable) {
            setTarget((UiScrollable) result);
        }
    }

    ListIterator<Object> resultsIterator = results.listIterator(results.size());
    while (resultsIterator.hasPrevious()) {
        Object item = resultsIterator.previous();
        if (item instanceof UiObject) {
            // assign the result to the last method in the chain
            // that returns UiObject
            uiSelector = ((UiObject) item).getSelector();
            break;
        }
    }
    if (uiSelector == null && self != null) {
        // if none of the methods in the chain return UiObject instance
        // the assign the result to self instance
        uiSelector = self.getSelector();
    }
    if (uiSelector == null) {
        throw new UiSelectorSyntaxException(expression.toString(),
                "At least one method called on a UiScrollable object must return an UiObject instance");
    }
    return uiSelector;
}
 
Example #8
Source File: UiScrollableParserTests.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Override
protected UiScrollable consumeConstructor() throws UiSelectorSyntaxException,
        UiObjectNotFoundException {
    UiScrollable self = super.consumeConstructor();
    uiScrollableSpy = spy(getTarget());
    doReturn(true).when(uiScrollableSpy).scrollIntoView(any(UiSelector.class));
    doReturn(true).when(uiScrollableSpy).scrollIntoView(any(UiObject.class));
    doReturn(uiObject).when(uiScrollableSpy).getChildByText(any(UiSelector.class),
            anyString(), anyBoolean());
    doThrow(new UiObjectNotFoundException("not found")).when(uiScrollableSpy)
            .getChildByText(any(UiSelector.class), anyString());
    doReturn(true).when(uiScrollableSpy).scrollForward(anyInt());
    setTarget(uiScrollableSpy);
    return self;
}
 
Example #9
Source File: AutomatorAction.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Scrolls a {@link UiScrollable} until the given text is displayed on the screen.
 *
 * @param text the text to scroll to.
 * @return
 */
public static AutomatorAction scrollTextIntoView(final String text) {
    return new AutomatorAction() {
        @Override
        public void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
            new UiScrollable(selector).scrollTextIntoView(text);
        }
    };
}
 
Example #10
Source File: AllowListScreenshots.java    From focus-android with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void takeScreenshotsOfMenuandAllowlist() throws UiObjectNotFoundException {
    SystemClock.sleep(5000);
    onView(withId(R.id.urlView))
            .check(matches(isDisplayed()))
            .check(matches(hasFocus()))
            .perform(click(), replaceText(webServer.url("/").toString()));

    onView(withId(R.id.urlView))
            .check(matches(isDisplayed()))
            .check(matches(hasFocus()))
            .perform(pressImeActionButton());

    device.findObject(new UiSelector()
            .resourceId(TestHelper.getAppName() + ":id/webview")
            .enabled(true))
            .waitForExists(waitingTime);

    TestHelper.menuButton.perform(click());
    Screengrab.screenshot("BrowserViewMenu");
    onView(withId(R.id.blocking_switch)).perform(click());

    // Open setting
    onView(withId(R.id.menuView))
            .check(matches(isDisplayed()))
            .perform(click());
    onView(withId(R.id.settings))
            .check(matches(isDisplayed()))
            .perform(click());
    onView(withText(R.string.preference_privacy_and_security_header)).perform(click());

    UiScrollable settingsView = new UiScrollable(new UiSelector().scrollable(true));
    if (settingsView.exists()) {        // On tablet, this will not be found
        settingsView.scrollToEnd(5);
        onView(withText(R.string.preference_exceptions)).perform(click());
    }

    onView(withId(R.id.removeAllExceptions))
            .check(matches(isDisplayed()));
    Screengrab.screenshot("ExceptionsDialog");
    onView(withId(R.id.removeAllExceptions))
            .perform(click());

}
 
Example #11
Source File: ChangeSearchEngineTest.java    From focus-android with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void SearchTest() throws UiObjectNotFoundException {
    UiObject searchMenu = TestHelper.settingsMenu.getChild(new UiSelector()
            .className("android.widget.LinearLayout")
            .instance(2));

    UiObject searchEngineSelectorLabel = searchMenu.getChild(new UiSelector()
            .resourceId("android:id/title")
            .text("Search")
            .enabled(true));

    String searchString = String.format("mozilla focus - %s Search", mSearchEngine);
    UiObject googleWebView = TestHelper.mDevice.findObject(new UiSelector()
            .description(searchString)
            .className("android.webkit.WebView"));

    // Open [settings menu] and select Search
    assertTrue(TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime));
    openSettings();
    TestHelper.settingsHeading.waitForExists(waitingTime);
    searchEngineSelectorLabel.waitForExists(waitingTime);
    searchEngineSelectorLabel.click();

    // Open [settings menu] > [search engine menu] and click "Search engine" label
    UiObject defaultSearchEngineLabel = TestHelper.settingsMenu.getChild(new UiSelector()
            .className("android.widget.LinearLayout")
            .instance(0));
    defaultSearchEngineLabel.waitForExists(waitingTime);
    defaultSearchEngineLabel.click();

    // Open [settings menu] > [search engine menu] > [search engine list menu]
    // then select desired search engine
    UiScrollable searchEngineList = new UiScrollable(new UiSelector()
            .resourceId(TestHelper.getAppName() + ":id/search_engine_group").enabled(true));
    UiObject defaultEngineSelection = searchEngineList.getChildByText(new UiSelector()
            .className(RadioButton.class), mSearchEngine);
    defaultEngineSelection.waitForExists(waitingTime);
    assertTrue(defaultEngineSelection.getText().equals(mSearchEngine));
    defaultEngineSelection.click();
    TestHelper.pressBackKey();
    TestHelper.settingsHeading.waitForExists(waitingTime);
    UiObject defaultSearchEngine = TestHelper.mDevice.findObject(new UiSelector()
            .text(mSearchEngine)
            .resourceId("android:id/summary"));
    assertTrue(defaultSearchEngine.getText().equals(mSearchEngine));
    TestHelper.pressBackKey();
    TestHelper.pressBackKey();

    // Do search on blank spaces and press enter for search (should not do anything)
    TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);
    TestHelper.inlineAutocompleteEditText.clearTextField();
    TestHelper.inlineAutocompleteEditText.setText("   ");
    TestHelper.pressEnterKey();
    assertTrue(TestHelper.inlineAutocompleteEditText.exists());

    // Do search on text string
    TestHelper.inlineAutocompleteEditText.clearTextField();
    TestHelper.inlineAutocompleteEditText.setText("mozilla ");
    // Would you like to turn on search suggestions? Yes No
    // fresh install only)
    if (TestHelper.searchSuggestionsTitle.exists()) {
        TestHelper.searchSuggestionsButtonYes.waitForExists(waitingTime);
        TestHelper.searchSuggestionsButtonYes.click();
    }

    // verify search hints... "mozilla firefox", "mozilla careers", etc.TestHelper.suggestionList.waitForExists(waitingTime);
    TestHelper.mDevice.pressKeyCode(KEYCODE_SPACE);
    TestHelper.suggestionList.waitForExists(waitingTime);
    assertTrue(TestHelper.suggestionList.getChildCount() >= 1);

    onView(allOf(withText(containsString("mozilla")),
            withId(R.id.searchView)))
            .check(matches(isDisplayed()));
    // we expect min=1, max=5
    int count = 0;
    int maxCount = 3;
    //while (count <= maxCount) {
    while (count < maxCount) {
        onView(allOf(withText(containsString("mozilla")),
                withId(R.id.suggestion),
                isDescendantOfA(childAtPosition(withId(R.id.suggestionList), count))))
                .check(matches(isDisplayed()));
        count++;
    }

    // Tap URL bar, check it displays search term (instead of URL)
   TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime);
   TestHelper.inlineAutocompleteEditText.click();
   TestHelper.inlineAutocompleteEditText.clearTextField();
   TestHelper.inlineAutocompleteEditText.setText("mozilla focus");
   TestHelper.pressEnterKey();
   googleWebView.waitForExists(waitingTime);
   TestHelper.progressBar.waitUntilGone(webPageLoadwaitingTime);

   // Search for words: <Google|DuckDuckGo|etc.>, mozilla, focus
   assertTrue(TestHelper.browserURLbar.getText().contains(mSearchEngine.toLowerCase()));
   assertTrue(TestHelper.browserURLbar.getText().contains("mozilla"));
   assertTrue(TestHelper.browserURLbar.getText().contains("focus"));
}
 
Example #12
Source File: UiScrollableParser.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
UiScrollableParser(String expression) {
    super(UiScrollable.class, expression);
}