Swipe of Bootstrap

Source: Internet
Author: User
Tags appium
Swipe


I define it as slide, but it does not literally mean that the event form is similar to taking a rock piece as a child and flying toward the water. If you can use this method, it is the route that swipe takes, if you can't do this, it will be laughed at if you don't 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 number of shards 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, get the control object, and then pass in different records 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 easy, and the business is very easy to understand. When dealing with such a point, we need to infer a lot of things. The above code first analyzes the X coordinate and then the Y coordinate. The inference of the X and Y coordinates is the same as the processing time, so I will only talk about the 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 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 detailed value. If none of the above conditions is met, that is, the detailed coordinate value, then the X coordinate value of the element is directly added to the left coordinate value of the widget's border. Finally, the boundary verification is inferred 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 infer whether the passed coordinate value is less than 1, assuming less than 1, as a percentage of the spherical coordinate value. If an exception is thrown out of the screen range, the final returned coordinate value is returned 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 run the command to determine whether the operation is successful.


Summary


Run the swipe command in the Command Format of 2

  • Widget
  • Coordinates

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















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.