处理控件还是处理坐标

文章作者:w88手机版登录网络 上传时间:2019-07-03

Swipe

自个儿定义为滑行,但它字面包车型大巴意味又不是,事件的情势类似于时辰候拿着一块石头片,朝水面飞过去,假令你手法能够那么便是swipe走的路径,要是你手法十一分,接触水面包车型大巴时候就没再飞起来那就能够被人笑话的。

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);   } } 

任凭它怎么定义,先深入分析源代码最终再定义。

    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();  

第一从命令里获取參数,然后分析出所不可不的3个变量:起首点start、终点end、步骤steps。然后拿走器械对象,定义2个村办Point对象,以备后用。

接下来分标准管理,管理控件仍然管理坐标。

控件

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

先是得到控件对象,再通过getAbsolutePosition传入区别的參数拿到在该控件上点击的初始点和结束点。

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;   }

地点的一大段代码,看起来极度复杂,事实上极其easy,业务特别轻易驾驭,管理那样的点的时候就必须猜测相当多东西。下边包车型客车代码首先深入分析x坐标然后剖析y坐标。x和y坐标的臆度和拍卖时同样的,所以自个儿不过讲一下x坐标。

第一估算x坐标是还是不是为0,假使为0,定义初步点的x坐标为控件的中央点的横坐标。要是x的坐标小于1,表明坐标为绝对坐标,用百分比来求值,此时快要与幅度做乘积运算获得详细值。假如上面2种意况都不吻合,那就是事无巨细坐标值,这就一贯因素的x坐标值加上控件的边框左坐标值。最终依据传入的boolean值来推论是或不是做三个不只有边界的验证。借使高出边界就跑出十一分。y坐标的拿走格局接近。最后获得坐标值并再次来到,回到execute方法中。

坐标

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

经过调用getDeviceAbsPos()方法获得坐标值来初叶化从前宣称的村办Point对象.

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;   }

恍如于地点的方式,也是要先估量传过来的坐标值是或不是低于1,就算小于1,当作百分比来球坐标值。假诺赶过显示屏的范围抛出非常,最后回到坐标值回到execute方法。

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

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

末段调用UiDevice.swipe方法来运作命令,估摸是还是不是运营成功。

总结

运转swipe命令有第22中学命令格式

  • 控件
  • 坐标

坐标又分为相对坐标百分比和相对坐标二种格局。


本文由w88手机版登录-www.w88zhan.com-优德w88app官方登录发布于w88手机版登录网络,转载请注明出处:处理控件还是处理坐标

关键词: