Scrollto for bootstrap

Source: Internet
Author: User
Scrollto


package io.appium.android.bootstrap.handler;import com.android.uiautomator.core.UiObject;import com.android.uiautomator.core.UiObjectNotFoundException;import com.android.uiautomator.core.UiScrollable;import com.android.uiautomator.core.UiSelector;import io.appium.android.bootstrap.*;import org.json.JSONException;import java.util.Hashtable;/** * This handler is used to scroll to elements in the Android UI. *  * Based on the element Id of the scrollable, scroll to the object with the * text. *  */public class ScrollTo extends CommandHandler {  /*   * @param command The {@link AndroidCommand}   *    * @return {@link AndroidCommandResult}   *    * @throws JSONException   *    * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.   * bootstrap.AndroidCommand)   */  @Override  public AndroidCommandResult execute(final AndroidCommand command)      throws JSONException {    if (!command.isElementCommand()) {      return getErrorResult("A scrollable view is required for this command.");    }    try {      Boolean result;      final Hashtable<String, Object> params = command.params();      final String text = params.get("text").toString();      final String direction = params.get("direction").toString();      final AndroidElement el = command.getElement();      if (!el.getUiObject().isScrollable()) {        return getErrorResult("The provided view is not scrollable.");      }      final UiScrollable view = new UiScrollable(el.getUiObject().getSelector());      if (direction.toLowerCase().contentEquals("horizontal")          || view.getClassName().contentEquals(              "android.widget.HorizontalScrollView")) {        view.setAsHorizontalList();      }      view.scrollToBeginning(100);      view.setMaxSearchSwipes(100);      result = view.scrollTextIntoView(text);      view.waitForExists(5000);      // make sure we can get to the item      UiObject listViewItem = view.getChildByInstance(          new UiSelector().text(text), 0);      // We need to make sure that the item exists (visible)      if (!(result && listViewItem.exists())) {        return getErrorResult("Could not scroll element into view: " + text);      }      return getSuccessResult(result);    } catch (final UiObjectNotFoundException e) {      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());    } catch (final NullPointerException e) { // el is null      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());    } catch (final Exception e) {      return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());    }  }}

In uiautomator, you sometimes need to find an item in a rolling list, and the position of this item is not fixed. In this case, we can use the scrollto of uiscrollable to find the control of specific text. The scrollto of Bootstrap is to encapsulate such a requirement.


First, determine whether the control can be rolled, and then create a uiscrollable object, because the default scrolling method is vertical. If you need a horizontal direction, you must set the direction to horizontal.


view.setAsHorizontalList();


Then, roll the scroll control to the first place, and set the maximum search range to 100 times, because it is impossible to search forever. Then start calling


view.scrollTextIntoView(text);


Start rolling, and finally confirm whether to scroll to the set goal:


 view.waitForExists(5000);

Because there may be a new time, it takes 5 seconds to call the method.


UiObject listViewItem = view.getChildByInstance(          new UiSelector().text(text), 0);

Finally, check the result, obtain the Text object, determine whether it exists, and return the result to the client.




Scrollto for bootstrap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.