手机自动化测试: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. Loadrunner 在controller中运行socket脚本时报错:Abnormal termination, caused by mdrv process termination 的原因和解决方法

    原因: 网上给出的可能的原因大致有两个: 1.  压力负载机器的资源不足(CPU,内存) 2.  分配内存和释放内存的语句不匹配. 并给出了一些解决方案,最开始我以为是加了IP地址的原因,不断尝试增加 ...

  2. 学习笔记——Java核心技术之接口、继承与多态练习题

    1.创建一个抽象类,验证它是否可以实例化对象. package com.lzw; public abstract class UseCase3 { abstract void doit(); publ ...

  3. 浅解.Net分布式锁的实现

    序言 我晚上有在公司多呆会儿的习惯,所以很多晚上我都是最后一个离开公司的.当然也有一些同事,跟我一样喜欢在公司多搞会儿.这篇文章就要从,去年年末一个多搞会的晚上说起,那是一个夜黑风高的晚上,公司应该没 ...

  4. Linux的capability深入分析

    Linux的capability深入分析详见:http://blog.csdn.net/u014338577/article/details/48791953 lxd中对容器能力的限制: 普通用户不能 ...

  5. 延时循环数组 DelayLoops

    在操作mongodb数据库时,查询了数据得到一个数组,然后还得遍历这个数组,再对每个数组的值进行数据库相关的增删改查, 如果单纯的遍历会出错.所以才写了这个简单的类. var a = [1,2,3,4 ...

  6. 中软卓越IT培训:给IT程序员的18个忠告

    1 .想清楚,写清楚,说清楚,才是真正的清楚! 2 .多花点时间沟通清楚需求,才能把握正确方向! 3 .修复需求错误的成本是代码错误的几十倍! 4 .程序员最大的坏习惯就是:急于动手写代码! 5 .提 ...

  7. Java并发之需要了解但不能太依赖的东东

    <Java 编程思想>在并发一章中提到了Sleep休眠.优先级.后台线程,提醒读者需要了解但又不能太依赖他们.就让我们一起看看吧. 休眠Sleep import java.util.con ...

  8. 用GDB调试程序

    转自:http://blog.csdn.net/haoel/article/details/2879 是一篇从基础讲gdb的博文 用GDB调试程序 GDB概述---- GDB是GNU开源组织发布的一个 ...

  9. 3DES 加密 解密

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p2 { margin: 0.0px 0. ...

  10. Yii2.0修改默认控制器

    设置默认控制器有两种方法 1.在/vendor/yiisoft/yii2/web/Application.PHP的第28行左右 public $defaultRoute = 'site';      ...