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

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

GetName

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 get the text of elements that support it.

*/

public class GetName extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @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()) {

return getErrorResult("Unable to get name without an element.");

}

try {

final AndroidElement el = command.getElement();

return getSuccessResult(el.getContentDesc());

} catch (final UiObjectNotFoundException e) {

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());

} catch (final Exception e) { // handle NullPointerException

return getErrorResult("Unknown error");

}

}

}

最终会调用UiObject.getContentDescription()方法获得控件当描述信息

GetAttribute

package io.appium.android.bootstrap.handler;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import io.appium.android.bootstrap.exceptions.NoAttributeFoundException;

import org.json.JSONException;

import java.util.Hashtable;

/**

* This handler is used to get an attribute of an element.

*

*/

public class GetAttribute extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @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()) {

// only makes sense on an element

final Hashtable<String, Object> params = command.params();

try {

final AndroidElement el = command.getElement();

final String attr = params.get("attribute").toString();

if (attr.equals("name") || attr.equals("text")

|| attr.equals("className")) {

return getSuccessResult(el.getStringAttribute(attr));

} else {

return getSuccessResult(String.valueOf(el.getBoolAttribute(attr)));

}

} catch (final NoAttributeFoundException e) {

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

e.getMessage());

} catch (final UiObjectNotFoundException e) {

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

e.getMessage());

} catch (final Exception e) { // el is null

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

e.getMessage());

}

} else {

return getErrorResult("Unable to get attribute without an element.");

}

}

}

该事件是为获取控件相关信息设置的,来看看可以获取哪些信息,其实就是uiautomatorviewer里的信息。

public String getStringAttribute(final String attr)

throws UiObjectNotFoundException, NoAttributeFoundException {

String res;

if (attr.equals("name")) {

res = getContentDesc();

if (res.equals("")) {

res = getText();

}

} else if (attr.equals("text")) {

res = getText();

} else if (attr.equals("className")) {

res = getClassName();

} else {

throw new NoAttributeFoundException(attr);

}

return res;

}

文本值:content-desc、text、className.

public boolean getBoolAttribute(final String attr)

throws UiObjectNotFoundException, NoAttributeFoundException {

boolean res;

if (attr.equals("enabled")) {

res = el.isEnabled();

} else if (attr.equals("checkable")) {

res = el.isCheckable();

} else if (attr.equals("checked")) {

res = el.isChecked();

} else if (attr.equals("clickable")) {

res = el.isClickable();

} else if (attr.equals("focusable")) {

res = el.isFocusable();

} else if (attr.equals("focused")) {

res = el.isFocused();

} else if (attr.equals("longClickable")) {

res = el.isLongClickable();

} else if (attr.equals("scrollable")) {

res = el.isScrollable();

} else if (attr.equals("selected")) {

res = el.isSelected();

} else if (attr.equals("displayed")) {

res = el.exists();

} else {

throw new NoAttributeFoundException(attr);

}

return res;

}

boolean类型的值:如上等等。

GetDeviceSize

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 org.json.JSONObject;

/**

* This handler is used to get the size of the screen.

*

*/

public class GetDeviceSize extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @return {@link AndroidCommandResult}

*

* @throws JSONException

*

* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

* bootstrap.AndroidCommand)

*/

@Override

public AndroidCommandResult execute(final AndroidCommand command) {

if (!command.isElementCommand()) {

// only makes sense on a device

final UiDevice d = UiDevice.getInstance();

final JSONObject res = new JSONObject();

try {

res.put("height", d.getDisplayHeight());

res.put("width", d.getDisplayWidth());

} catch (final JSONException e) {

getErrorResult("Error serializing height/width data into JSON");

}

return getSuccessResult(res);

} else {

return getErrorResult("Unable to get device size on an element.");

}

}

}

获取屏幕的长和宽,调用的是UiDevice的方法:getDisplayHeight()和getDisplayWidth()

GetSize

package io.appium.android.bootstrap.handler;

import android.graphics.Rect;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

import org.json.JSONObject;

/**

* This handler is used to get the size of elements that support it.

*

*/

public class GetSize extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @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()) {

// Only makes sense on an element

final JSONObject res = new JSONObject();

try {

final AndroidElement el = command.getElement();

final Rect rect = el.getBounds();

res.put("width", rect.width());

res.put("height", rect.height());

} catch (final UiObjectNotFoundException e) {

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,

e.getMessage());

} catch (final Exception e) { // handle NullPointerException

return getErrorResult("Unknown error");

}

return getSuccessResult(res);

} else {

return getErrorResult("Unable to get text without an element.");

}

}

}

获取控件的宽和高,调用的是UiObject的getBounds()。得到一个矩形,然后获得其宽和高。

GetLocation

package io.appium.android.bootstrap.handler;

import android.graphics.Rect;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

import org.json.JSONObject;

/**

* This handler is used to get the text of elements that support it.

*

*/

public class GetLocation extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @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()) {

return getErrorResult("Unable to get location without an element.");

}

try {

final JSONObject res = new JSONObject();

final AndroidElement el = command.getElement();

final Rect bounds = el.getBounds();

res.put("x", bounds.left);

res.put("y", bounds.top);

return getSuccessResult(res);

} catch (final Exception e) {

return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());

}

}

}

获取控件的起始点坐标。调用的也是getBounds,然后得到其起始点的x,y 坐标

GetDataDir

package io.appium.android.bootstrap.handler;

import android.os.Environment;

import io.appium.android.bootstrap.AndroidCommand;

import io.appium.android.bootstrap.AndroidCommandResult;

import io.appium.android.bootstrap.CommandHandler;

/**

* This handler is used to get the data dir.

*

*/

public class GetDataDir extends CommandHandler {

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @return {@link AndroidCommandResult}

*

* @throws JSONException

*

* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

* bootstrap.AndroidCommand)

*/

@Override

public AndroidCommandResult execute(final AndroidCommand command) {

return getSuccessResult(Environment.getDataDirectory());

}

}

获取data的根目录。调用的是android的api:Environment.getDataDirectory()

手机自动化测试:appium源码分析之bootstrap十一的更多相关文章

  1. 手机自动化测试:appium源码分析之bootstrap三

    手机自动化测试:appium源码分析之bootstrap三   研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...

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

    手机自动化测试:appium源码分析之bootstrap二   在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...

  3. 手机自动化测试:appium源码分析之bootstrap一

    手机自动化测试:appium源码分析之bootstrap一   前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...

  4. 手机自动化测试:appium源码分析之bootstrap十七

    手机自动化测试:appium源码分析之bootstrap十七   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  5. 手机自动化测试:appium源码分析之bootstrap十六

    手机自动化测试:appium源码分析之bootstrap十六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  6. 手机自动化测试:appium源码分析之bootstrap十五

    手机自动化测试:appium源码分析之bootstrap十五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  7. 手机自动化测试:appium源码分析之bootstrap十四

    手机自动化测试:appium源码分析之bootstrap十四   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

  8. 手机自动化测试:appium源码分析之bootstrap十三

    手机自动化测试:appium源码分析之bootstrap十三   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

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

    手机自动化测试:appium源码分析之bootstrap十二   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

随机推荐

  1. 使用国内docker镜像源

    在国内,通过Docker的pull和push命令访问hub.docker时,网络十分慢,而且会出现各种各样的网络连接问题.因此这里介绍下如何使用国内的镜像源,这里以DaoCloud为例. 注册DaoC ...

  2. HR从业者的下一个十年该怎么做?

    众所周知,目前我们所处的以互联网为代表的新经济时代,有两个重要的特征一是客户价值至上,二是人力资本主权.围绕这两个基本特征,我们需要重新架构我们的商业模式和创新人力资源管理思维.模式.工具和方法. 人 ...

  3. C语言常见错误中英文对照表

    C语言常见错误中英文对照表(网络搜索及经验积累不断更新中) 常见错误中英文对照表 fatal error C1003:  error count exceeds number; stopping co ...

  4. SQL SERVER的检查点checkpoint

    1 什么是检查点 数据修改操作 都是在 内存中的数据页进行修改,每次修改后并没有立即把这些页面写入磁盘,而是等到一定时期,数据库引擎对数据库发起 检查点命令,这时,该命令就会创建一个已知的正常点,把当 ...

  5. LINUX RHEL6.5字符界面安装图形化桌面

    安装RHEL 6.5 系统,也是一波三折.好不容易把系统装上去了,发现没装图形化界面.重装倒是学会了,不过觉得太麻烦,于是有了今天. 查了很多帖子,然后自己一一尝试,发现都是说简单,只要 yum gr ...

  6. linux 压缩zip包

    压缩: zip -r  名称.zip 被压缩文件 解压: unzip 名称.zip

  7. 关于Dapper.NET的相关论述

    年少时,为何不为自己的梦想去拼搏一次呢?纵使头破血流,也不悔有那年少轻狂.感慨很多,最近事情也很多,博客也很少更新了,毕竟每个人都需要为自己的生活去努力. 最近在一个群里遇到一个人说的话,在这里不再赘 ...

  8. AtomicInteger源码分析——基于CAS的乐观锁实

    1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时间片之间,需要进行cpu切换,也就是会发生进程的切换.切换涉及 ...

  9. HTML5周记(一)

    各位开发者朋友和技术大神大家好!博主刚开始学习html5 ,自本周开始会每周更新技术博客,与大家分享每周所学.鉴于博主水品有限,如发现有问题的地方欢迎大家指正,有更好的意见和建议可在评论下方发表,我会 ...

  10. 浅谈jquery插件开发模式

    首先根据<jQuery高级编程>的描述来看,jQuery插件开发方式主要有三种: 通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget ...