zoukankan      html  css  js  c++  java
  • 手机自动化测试:appium源码分析之bootstrap二

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

     

    在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类,

    private static HashMap<String, CommandHandler> map = new HashMap<String, CommandHandler>();

    static {
    map.put("waitForIdle", new WaitForIdle());
    map.put("clear", new Clear());
    map.put("orientation", new Orientation());
    map.put("swipe", new Swipe());
    map.put("flick", new Flick());
    map.put("drag", new Drag());
    map.put("pinch", new Pinch());
    map.put("click", new Click());
    map.put("touchLongClick", new TouchLongClick());
    map.put("touchDown", new TouchDown());
    map.put("touchUp", new TouchUp());
    map.put("touchMove", new TouchMove());
    map.put("getText", new GetText());
    map.put("setText", new SetText());
    map.put("getName", new GetName());
    map.put("getAttribute", new GetAttribute());
    map.put("getDeviceSize", new GetDeviceSize());
    map.put("scrollTo", new ScrollTo());
    map.put("find", new Find());
    map.put("getLocation", new GetLocation());
    map.put("getSize", new GetSize());
    map.put("wake", new Wake());
    map.put("pressBack", new PressBack());
    map.put("dumpWindowHierarchy", new DumpWindowHierarchy());
    map.put("pressKeyCode", new PressKeyCode());
    map.put("longPressKeyCode", new LongPressKeyCode());
    map.put("takeScreenshot", new TakeScreenshot());
    map.put("updateStrings", new UpdateStrings());
    map.put("getDataDir", new GetDataDir());
    map.put("performMultiPointerGesture", new MultiPointerGesture());
    map.put("openNotification", new OpenNotification());
    }

    我们来看WaitForIdle:

    package io.appium.android.bootstrap.handler;

    import com.android.uiautomator.core.UiDevice;
    import io.appium.android.bootstrap.AndroidCommand;
    import io.appium.android.bootstrap.AndroidCommandResult;
    import io.appium.android.bootstrap.CommandHandler;
    import org.json.JSONException;

    import java.util.Hashtable;

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

    * Based on the element Id, clear that element.

    */
    public class WaitForIdle 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 {
    final Hashtable<String, Object> params = command.params();
    long timeout = 10;
    if (params.containsKey("timeout")) {
    timeout = (Integer) params.get("timeout");
    }

    UiDevice d = UiDevice.getInstance();
    d.waitForIdle(timeout);
    return getSuccessResult(true);
    }
    }

    首先是获取命令里面参数,然后初始化一个timeout私有变量,如果参数里不含时间,那么就用这个默认的时间。然后调用uiautomator的UiDevice的对象里方法waitForIdle(),该方法就在timeout时间内界面上没有其他操作,处于空闲状态。

    Clear:

    package io.appium.android.bootstrap.handler;

    import com.android.uiautomator.core.UiObjectNotFoundException;
    import io.appium.android.bootstrap.*;
    import org.json.JSONException;

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

    * Based on the element Id, clear that element.

    */
    public class Clear 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()) {
    try {
    final AndroidElement el = command.getElement();
    el.clearText();
    return getSuccessResult(true);
    } catch (final UiObjectNotFoundException e) {
    return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
    e.getMessage());
    } catch (final Exception e) { // handle NullPointerException
    return getErrorResult("Unknown error clearing text");
    }
    }
    return getErrorResult("Unknown error");
    }
    }

    Clear的方法中就看e1.clearText()方法,其他的我在click中都有涉及。
    private final UiObject el;
    public void clearText() throws UiObjectNotFoundException {
    el.clearTextField();
    }
    实际上调用的是uiautomator中的UiObject.clearTextField(),清除文本框内的内容。

  • 相关阅读:
    win8装oracle 10g 弹出:环境变量path的值超过1023字符,无法设置改值
    Modern UI for WPF 开源项目(3):用模板创建我的第一个Modern UI app
    安装oracle11G,10G时都会出现:注册ocx时出现OLE初始化错误或ocx装载错误对话框
    win8快捷键
    Win7/Win8 系统下安装Oracle 10g 提示“程序异常终止,发生未知错误”的解决方法
    Delphi FireDAC 连接SQL Server一些要注意的地方
    Delphi XE5 for Android (四)
    Delphi D10.X VCL和FireMonkey之间的常见差异介绍
    delphi给App授予权限
    github无法登陆的解决办法
  • 原文地址:https://www.cnblogs.com/poptest/p/4942462.html
Copyright © 2011-2022 走看看