手机自动化测试: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. 自定义checkbox, radio样式

    17.2.25.nimil 今天开始做百度前端学院的任务,第一个是自定义checkbox, radio样式. checkbox和radio两个标签是不可以改变样式的,background-color. ...

  2. 如何利用docker快速构建MySQL主从复制环境

    在学习MySQL的过程中,常常会测试各种参数的作用.这时候,就需要快速构建出MySQL实例,甚至主从. 考虑如下场景: 譬如我想测试mysqldump在指定--single-transaction参数 ...

  3. 网站Web业务架构从小到大演变

    有一天,我突发奇想创建了一个站点,基于 LNMP 架构,起初只有我自己访问,后来因为我点儿正,访问量越来越大,所以最终导致下面的架构演变. 1.单台机器 单台机器因为只是一个小站,访问量一天也没有多少 ...

  4. JS中的作用域链

    在js中数据的声明方式有两种: 1.用var声明,例如:var num = 10: 2.直接声明,例如:num = 10: 两种声明方式在某些情况下是有区别的: var data = 10; func ...

  5. [前言] 实现一个Android电子书阅读APP

    大家好,我是小方,我将在接下来的几篇文章中从零实现一个网络小说阅读器,从安卓编程最基础的部分讲起,直至成功完成我们的应用,从新建一个项目开始,不断添加新的代码,添加新的界面,循序渐进,涵盖所有我们需要 ...

  6. 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response>

    徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 接上一篇,说到接受了请求,接下来就是解析请求构建Request对象,以及创建Response对象返回. 多有纰漏还请指出.省略 ...

  7. x86主机搭建家庭智能路由系统 ---- Proxmox虚拟化实现一机多用

    Proxmox VE简介 Proxmox VE(Proxmox Virtual Environment) 是一款完全开源虚拟化管理平台,可以管理QEMU/KVM虚拟机和LXC容器.事实上它只是一个前端 ...

  8. JS一周游~(基础、运算符、条件语句)

    一.基础篇 JavaScript 基于浏览器(客户端).基于(面向)对象{没有继承}.事件驱动(要有对象).脚本语言(灵活多变) 1.作用 表单的验证,减轻服务端的压力 添加页面动画效果 动态更改页面 ...

  9. C++类对象大小的计算

    (一)常规类大小计算 C++类对象计算需要考虑很多东西,如成员变量大小,内存对齐,是否有虚函数,是否有虚继承等.接下来,我将对此举例说明. 以下内存测试环境为Win7+VS2012,操作系统为32位 ...

  10. kali linux qq 2013

    按照网上的教程折腾了好几个小时,都没有搞定的qq for linux 在意外的尝试中成功了 文章有参考网友教程的部分:http://xiao106347.blog.163.com/blog/stati ...