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

 

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

ScrollTo

package io.appium.android.bootstrap.handler;

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

import com.android.uiautomator.core.UiSelector;

import io.appium.android.bootstrap.*;

import org.json.JSONException;

import java.util.Hashtable;

/**

* This handler is used to scroll to elements in the Android UI.

*

* Based on the element Id of the scrollable, scroll to the object with the

* text.

*

*/

public class ScrollTo 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()) {

return getErrorResult("A scrollable view is required for this command.");

}

try {

Boolean result;

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

final String text = params.get("text").toString();

final String direction = params.get("direction").toString();

final AndroidElement el = command.getElement();

if (!el.getUiObject().isScrollable()) {

return getErrorResult("The provided view is not scrollable.");

}

final UiScrollable view = new UiScrollable(el.getUiObject().getSelector());

if (direction.toLowerCase().contentEquals("horizontal")

|| view.getClassName().contentEquals(

"android.widget.HorizontalScrollView")) {

view.setAsHorizontalList();

}

view.scrollToBeginning(100);

view.setMaxSearchSwipes(100);

result = view.scrollTextIntoView(text);

view.waitForExists(5000);

// make sure we can get to the item

UiObject listViewItem = view.getChildByInstance(

new UiSelector().text(text), 0);

// We need to make sure that the item exists (visible)

if (!(result && listViewItem.exists())) {

return getErrorResult("Could not scroll element into view: " + text);

}

return getSuccessResult(result);

} catch (final UiObjectNotFoundException e) {

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

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

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

} catch (final Exception e) {

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

}

}

}

在uiautomator中有时候需要在一个滚动的list中找到某一个item,而这个item的位置又不定,这个时候我们可以通过UiScrollable的scrollTo来找到特定text的控件。而bootstrap的这个ScrollTo就是封装这样一种需求的。

首先判断控件是否是可以滚动的,然后创建UiScrollable对象,因为默认的滚动方式是垂直方向的,如果需要的是水平方向的的话,还要设置方向为水平。

view.setAsHorizontalList();

然后将滚动控件滚到最开始的地方,然后设置最大的搜索范围为100次,因为不可能永远搜索下去。然后开始调用

view.scrollTextIntoView(text);

开始滚动,最后确认是否滚动到制定目标:

view.waitForExists(5000);

因为有可能有刷新到时间,所以调用方法到时候传入了时间5秒钟。

UiObject listViewItem = view.getChildByInstance(

new UiSelector().text(text), 0);

最后检查结果,获取制定text的对象,判断其是否存在然后返回相应结果给客户端。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. mysql远程连接权限

    环境:mysql6.0 .Navicat Premium 用户名:root 密码:123456  本地连接无问题 远程连接mysql的时候应该碰到Navicat Premium 报错. 错误代码是11 ...

  2. 【图像浏览】FastStone Image Viewer——快速、小巧、功能强大

    FastStone Image Viewer 是一款免费(非商业用途)且小巧的看图软件. 其在在appinn.com的我最喜爱的<图片/照片浏览查看工具>调查结果中排第6名(总提名 140 ...

  3. 简学Python第四章__装饰器、迭代器、列表生成式

    Python第四章__装饰器.迭代器 欢迎加入Linux_Python学习群  群号:478616847 目录: 列表生成式 生成器 迭代器 单层装饰器(无参) 多层装饰器(有参) 冒泡算法 代码开发 ...

  4. Kettle 5.0源码编译

    下载源码请参考上一篇博文Kettle4.4.2源码分析 Kettle 5.0以前的库文件通过ant管理,5.0+的库文件通过ant+ivy管理.Eclipse一般都是安装ant插件,不安装ivy插件, ...

  5. HTML文档及标签介绍

    HTML标签 HTML 标记标签通常被称为 HTML 标签 (HTML tag). HTML标签是由尖括号包含的关键词,比如<html> HTML标签通常是成对出现的,比如<body ...

  6. 关于ASP.NET Web Api的HelpPage文档注释问题

    关于ASP.NET Web Api的HelpPage文档注释问题 以前我用微软的HelpPage来自动生成的webAPI帮助文档.在使用了一段时间后发现只能显示Controller上面写的注释文档内容 ...

  7. Java设计模式之《组合模式》及应用场景

    摘要: 原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6489827.html 组合模式,就是在一个对象中包含其他对象,这些被包含的对象可能是 ...

  8. hadoop2.7.2完全分布式环境搭建

      1.先使用groupadd hadoop 建立hadoop用户组 2.新建用户,useradd -d /usr/hadoop -g hadoop -m hadoop (新建用户hadoop指定用户 ...

  9. spring提供的线程池

    SPRING中的线程池ThreadPoolTaskExecutor 分类: JAVA Spring2013-07-12 10:36 14896人阅读 评论(9) 收藏 举报 Spring线程池多线程 ...

  10. grpc-gateway:grpc对外提供http服务的解决方案

    我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful.于是就想到了google的grpc. 使用grpc ...