Swipe of Bootstrap

Source: Internet
Author: User
Tags appium
Swipe


I define it as slide, but it does not literally mean that the event is similar to taking a rock piece as a child and flying toward the water. If you can do this, it is the route that swipe takes, if you do not use this method, it will be ridiculed if you do not fly when you touch the water.


package io.appium.android.bootstrap.handler;import com.android.uiautomator.core.UiDevice;import com.android.uiautomator.core.UiObjectNotFoundException;import io.appium.android.bootstrap.*;import io.appium.android.bootstrap.exceptions.InvalidCoordinatesException;import io.appium.android.bootstrap.utils.Point;import org.json.JSONException;import java.util.Hashtable;/** * This handler is used to swipe. *  */public class Swipe extends CommandHandler {  /*   * @param command The {@link AndroidCommand} used for this handler.   *    * @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 {    final Hashtable<String, Object> params = command.params();    final Point start = new Point(params.get("startX"), params.get("startY"));    final Point end = new Point(params.get("endX"), params.get("endY"));    final Integer steps = (Integer) params.get("steps");    final UiDevice device = UiDevice.getInstance();    Point absStartPos = new Point();    Point absEndPos = new Point();    if (command.isElementCommand()) {      try {        final AndroidElement el = command.getElement();        absStartPos = el.getAbsolutePosition(start);        absEndPos = el.getAbsolutePosition(end, false);      } catch (final UiObjectNotFoundException e) {        return getErrorResult(e.getMessage());      } catch (final InvalidCoordinatesException e) {        return getErrorResult(e.getMessage());      } catch (final Exception e) { // handle NullPointerException        return getErrorResult("Unknown error");      }    } else {      try {        absStartPos = getDeviceAbsPos(start);        absEndPos = getDeviceAbsPos(end);      } catch (final InvalidCoordinatesException e) {        return getErrorResult(e.getMessage());      }    }    Logger.debug("Swiping from " + absStartPos.toString() + " to "        + absEndPos.toString() + " with steps: " + steps.toString());    final boolean rv = device.swipe(absStartPos.x.intValue(),        absStartPos.y.intValue(), absEndPos.x.intValue(),        absEndPos.y.intValue(), steps);    if (!rv) {      return getErrorResult("The swipe did not complete successfully");    }    return getSuccessResult(rv);  }}

No matter how it is defined, analyze the source code before defining it.


    final Hashtable<String, Object> params = command.params();    final Point start = new Point(params.get("startX"), params.get("startY"));    final Point end = new Point(params.get("endX"), params.get("endY"));    final Integer steps = (Integer) params.get("steps");    final UiDevice device = UiDevice.getInstance();    Point absStartPos = new Point();    Point absEndPos = new Point();


First, obtain the parameters from the command, and then parse the required three variables: Start, end, and steps. Then obtain the device object and define two private point objects for future use.

Then it is processed by conditions to process the control or coordinates.


Widget


        final AndroidElement el = command.getElement();        absStartPos = el.getAbsolutePosition(start);        absEndPos = el.getAbsolutePosition(end, false);

First, obtain the control object, and then pass in different parameters through getabsoluteposition to obtain the start and end points for clicking on the control.


public Point getAbsolutePosition(final Point point,      final boolean boundsChecking) throws UiObjectNotFoundException,      InvalidCoordinatesException {    final Rect rect = el.getBounds();    final Point pos = new Point();    Logger.debug("Element bounds: " + rect.toShortString());    if (point.x == 0) {      pos.x = rect.width() * 0.5 + rect.left;    } else if (point.x <= 1) {      pos.x = rect.width() * point.x + rect.left;    } else {      pos.x = rect.left + point.x;    }    if (boundsChecking) {      if (pos.x > rect.right || pos.x < rect.left) {        throw new InvalidCoordinatesException("X coordinate ("            + pos.x.toString() + " is outside of element rect: "            + rect.toShortString());      }    }    if (point.y == 0) {      pos.y = rect.height() * 0.5 + rect.top;    } else if (point.y <= 1) {      pos.y = rect.height() * point.y + rect.top;    } else {      pos.y = rect.left + point.y;    }    if (boundsChecking) {      if (pos.y > rect.bottom || pos.y < rect.top) {        throw new InvalidCoordinatesException("Y coordinate ("            + pos.y.toString() + " is outside of element rect: "            + rect.toShortString());      }    }    return pos;  }

A large piece of code above looks very complicated. In fact, it is very simple, and the business is easy to understand. Many things need to be judged when dealing with such points. The above code first analyzes the X coordinate and then the Y coordinate. The judgment of X and Y coordinates is the same as that of processing, so I will only talk about X coordinates.

First, determine whether the X coordinate is 0. If it is 0, define the X coordinate of the initial point as the X coordinate of the central point of the control. If the coordinate of X is less than 1, it indicates that the coordinate is the relative coordinate, and the percentage is used to calculate the value. At this time, we need to perform the product operation with the width to obtain the specific value. If none of the above conditions match, that is, the specific coordinate value, then the X coordinate value of the element is directly added to the left coordinate value of the widget's border. Finally, we can determine whether to perform a verification that exceeds the boundary based on the input Boolean value. If the boundary is exceeded, an exception occurs. Y coordinates are obtained in a similar way. Finally, get the coordinate value and return it to the execute method.


Coordinates


absStartPos = getDeviceAbsPos(start);        absEndPos = getDeviceAbsPos(end);

Call the getdeviceabspos () method to obtain the coordinate value to initialize the previously declared private point object.


protected static Point getDeviceAbsPos(final Point point)      throws InvalidCoordinatesException {    final UiDevice d = UiDevice.getInstance();    final Point retPos = new Point(point); // copy inputed point    final Double width = (double) d.getDisplayWidth();    if (point.x < 1) {      retPos.x = width * point.x;    }    if (retPos.x > width || retPos.x < 0) {      throw new InvalidCoordinatesException("X coordinate ("          + retPos.x.toString() + " is outside of screen width: "          + width.toString());    }    final Double height = (double) d.getDisplayHeight();    if (point.y < 1) {      retPos.y = height * point.y;    }    if (retPos.y > height || retPos.y < 0) {      throw new InvalidCoordinatesException("Y coordinate ("          + retPos.y.toString() + " is outside of screen height: "          + height.toString());    }    return retPos;  }

Similar to the above method, it is also necessary to first determine whether the passed coordinate value is less than 1, if less than 1, as a percentage of the spherical coordinate value. If an exception is thrown out of the screen range, return the coordinates to the execute method.

========================================================== ========================================================== ========================================================== ========


final boolean rv = device.swipe(absStartPos.x.intValue(),        absStartPos.y.intValue(), absEndPos.x.intValue(),        absEndPos.y.intValue(), steps);

Finally, call the uidevice. Swipe method to execute the command and determine whether the execution is successful.


Summary


Run the swipe command in the format of 2.

  • Widget
  • Coordinates

Coordinates are divided into two methods: Relative Coordinate percentage and absolute coordinate.















Swipe of 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.