手机自动化测试:appium源码分析之bootstrap二
2015-11-06 14:13
429 查看
手机自动化测试: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(),清除文本框内的内容。
相关文章推荐
- 手机自动化测试:appium源码分析之bootstrap一
- 欢迎使用CSDN-markdown编辑器
- IOS 给Label添加下划线,删除线,不同的颜色和大小
- AutoMapper(四)
- iOS 开发 高级:使用 宏定义macros (#,##,...,__VA_ARGS_)
- <iOS>GCD使用经验与技巧
- Android实战技巧之六:PreferenceActivity使用详解
- Android开发环境的安装
- UVA 10652 Board Wrapping(凸包)
- Android 开源源码解析
- android ActionBar
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- android 使用lint + studio ,排查客户端无用string,drawable,layout资源
- Android基础 -- Activity之间传递数据(bitmap和map对象)
- android中popupwindow弹出后,屏幕背景变成半透明
- iOS_静态库合并
- Android GridView属性集合
- 微信开发(六)微信分享接入
- iOS_二维码扫描(iOS自带)
- 学Android---布局(四)TableLayout