手机自动化测试: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. BZOJ 1096: [ZJOI2007]仓库建设(动态规划+斜率优化)

    第一次写斜率优化,发现其实也没啥难的,没打过就随便找了一份代码借(chao)鉴(xi)下,不要介意= = 题解实在是懒得写了,贴代码吧= = CODE: #include<cstdio># ...

  2. 前端总结·基础篇·CSS(二)视觉

    前端总结系列 前端总结·基础篇·CSS(一)布局 前端总结·基础篇·CSS(二)视觉 前端总结·基础篇·CSS(三)补充 前端总结·基础篇·CSS(四)兼容 目录 一.动画(animation)(IE ...

  3. bootstrap select2 参数详解

    Select2使用示例地址: https://select2.github.io/examples.html Select2参数文档说明: https://select2.github.io/opti ...

  4. loadrunner测试结果分析

    LR性能测试结果样例分析 测试结果分析 LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源 ...

  5. Node.js~在linux上的部署

    我们以centOS为例来说说如何部署node.js环境 一 打开centos,然后开始下载node.js包 curl --silent --location https://rpm.nodesourc ...

  6. Memcached 内存管理详解

    Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用. 首先需要我们先了解两 ...

  7. UI设计学习路线图

    文章转载自「开发者圆桌」一个关于开发者入门.进阶.踩坑的微信公众号 这里整理的UI设计学习路线图包含初中高三个部分,你可以通过百度云盘下载观看对应的视频 链接: http://pan.baidu.co ...

  8. Laptop Ubuntu16.04/14.04 安装Nvidia显卡驱动

    笔记本型号 机械革命(MECHREVO)深海泰坦X6Ti-S(黑曜金)15.6英寸 CPU型号 i5-7300HQ 内存 8G 硬盘容量 128SSD+1T机械硬盘 显卡 GeForce GTX 10 ...

  9. C#:判断100--999之前的水仙花数

    //判断100--999之前的水仙花数.水仙花数举例:153=13+53+33. using System;public class Program  {    public static void ...

  10. redux-form的学习笔记二--实现表单的同步验证

    (注:这篇博客参考自redux-form的官方英文文档)左转http://redux-form.com/6.5.0/examples/syncValidation/ 在这篇博客里,我将用redux-f ...