手机自动化测试:appium源码分析之bootstrap八

手机自动化测试:appium源码分析之bootstrap八

 

 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478,今天是移动端测试开发课程开课,学员们学习热情很高。我们发现测试培训一个有趣的特点,周末学习提高的女孩子多,就业培训参加的男孩子比例高,

概念:pinchIn和pinchOut

a.2个手指同时按住A和B,同时将A滑向A1,B滑向B1,称为pinchIn

b.2个手指同时按住A1和B1,同时将A1滑向A,B1滑向B,成为pinchOut.

 

那么Pinch类就是接受这一指令的,传入的参数有方向direction,percent代表滑到对角线百分之几停止,steps代表滑到时间,一个step代表5毫秒。根据direction来判断是调用UiObject.pinchIn和UiObject.pinchOut.

 

Pinch代码:

package io.appium.android.bootstrap.handler;

 

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

 

import java.util.Hashtable;

 

/**

 * This handler is used to pinch in/out elements in the Android UI.

 *

 * Based on the element Id, pinch in/out that element.

 *

 */

public class Pinch 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();

 

    AndroidElement el;

    final String direction = params.get("direction").toString();

    final Integer percent = (Integer) params.get("percent");

    final Integer steps = (Integer) params.get("steps");

    try {

      el = command.getElement();

      if (el == null) {

        return getErrorResult("Could not find an element with elementId: "

            + params.get("elementId"));

      }

    } catch (final Exception e) { // JSONException, NullPointerException, etc.

      return getErrorResult("Unknown error:" + e.getMessage());

    }

 

    Logger.debug("Pinching " + direction + " " + percent.toString() + "%"

        + " with steps: " + steps.toString());

    boolean res;

    if (direction.equals("in")) {

      try {

        res = el.pinchIn(percent, steps);

      } catch (final UiObjectNotFoundException e) {

        return getErrorResult("Selector could not be matched to any UI element displayed");

      }

    } else {

      try {

        res = el.pinchOut(percent, steps);

      } catch (final UiObjectNotFoundException e) {

        return getErrorResult("Selector could not be matched to any UI element displayed");

      }

    }

 

    if (res) {

      return getSuccessResult(res);

    } else {

      return getErrorResult("Pinch did not complete successfully");

    }

  }

}

posted @ 2015-11-09 17:41  北京茑萝信息  阅读(244)  评论(0编辑  收藏  举报