bootstrap之WaitForIdle&&Clear

來源:互聯網
上載者:User

標籤:appium   bootstrap   

(上篇文章寫完才發現,說好的按順序但是回頭一看完全不是按順序的)明明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);  }}

上面的代碼處理過程很簡單,首先都是擷取命令裡面的參數,然後初始化一個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(),清楚文字框內的內容。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.