bootstrap之WaitForIdle&&Clear

(上篇文章写完才发现。说好的按顺序可是回头一看全然不是按顺序的)明明WaitForIdle才是第一个。哎,老了,后脑勺不行了。


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

上面的代码处理过程非常easy。首先都是获取命令里面的參数。然后初始化一个timeout私有变量,假设參数里不含时间。那么就用这个默认的时间。

然后调用uiautomator的UiDevice的对象里方法waitForIdle(),该方法就在timeout时间内界面上没有其它操作,处于空暇状态。这个就是封装了一下UiDevice的waitForIdle方法而已没啥可讲的。


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(),清楚文本框内的内容。




原文地址:https://www.cnblogs.com/bhlsheji/p/5183578.html