標籤: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(),清楚文字框內的內容。