手机自动化测试: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的对象,判断其是否存在然后返回相应结果给客户端。

转载于:https://www.cnblogs.com/poptest/p/4950625.html

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

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

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

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

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

  3. 手机自动化测试:appium源码分析之bootstrap六 (下)

    代码的步骤和swipe类似,而且最终调用的也是UiDevice.swipe方法,那么我们来看看到底区别在什么地方.首先它也分控件和坐标,分别分析: 控件 首先将控件的中心点作为起始坐标,然后根据提供的 ...

  4. Netty 源码分析系列(十二)Netty 解码器

    前言 编码和解码:数据从一种特定协议格式到另一种格式的转换. 处理编码和解码的程序通常被称为编码器和解码器.Netty 提供了一些组件,利用它们可以很容易地为各种不同协议编写编解码器. 编解码概述 编 ...

  5. 手机自动化测试:Appium源码分析之跟踪代码分析四 1

    手机自动化测试:Appium源码分析之跟踪代码分析四 控制器模块 // Appium webserver controller methods // https://github.com/hugs/a ...

  6. CTS(11)---android自动化测试CTS源码分析之一

    android自动化测试CTS源码分析之一 1, 概述 CTS(Compatibility Test Suite)全名兼容性测试,主要目的就是让Android设备开发商能够开发出兼容性更好的andro ...

  7. Spring 源码分析(四) ——MVC(二)概述

    随时随地技术实战干货,获取项目源码.学习资料,请关注源代码社区公众号(ydmsq666) from:Spring 源码分析(四) --MVC(二)概述 - 水门-kay的个人页面 - OSCHINA ...

  8. Spring 源码分析衍生篇十 :Last-Modified 缓存机制

    文章目录 一.前言 二.Last-Modify 三.实现方案 1. 实现 org.springframework.web.servlet.mvc.LastModified接口 1.1. 简单演示 1. ...

  9. app自动化测试之Appium 源码分析

    Appium 是由 Node.js 来实现的 HTTP 服务,它并不是一套全新的框架,而是将现有的优秀的框架进行了集成,在 Selenium WebDriver 协议(JsonWireProtocol ...

最新文章

  1. javascript对象之window对象详解
  2. 英特尔将进行重大业务重组
  3. queue java 判断重复值_java集合类深入分析之Queue篇(Q,DQ)
  4. SAP Query达到select * where 的效果 2011-04-29
  5. surfaceView和View区别
  6. python与人工智能应用锁_linux应用锁的搜索结果-阿里云开发者社区
  7. linux 修改ldap密码,Linux-ldap密码修改程序,如何加密ssha
  8. oracle 实时负载查询,Oracle并行查询
  9. Linux ALSA驱动框架(一)--ALSA架构简介--声卡的创建
  10. linux 中rpc 服务器,实现Linux环境下编程RPC通信之个人经验总结(转)
  11. centos7安装lnmp
  12. JS indexOf 用法
  13. rstudio 保存_R: RStudio的中文读取、保存与显示
  14. linux上删除rime方案_超强的输入法:rime的配置(linux)
  15. 模仿类似美团手机版应用源码
  16. 单反相机的传奇—佳能单反50年辉煌之路(连载十五)
  17. TI DSP 6657 SRIO 简介
  18. RabbitMQ精讲7:与SpringBoot、Spring Cloud Stream整合实战
  19. java输出皮卡丘_使用CSS实现皮卡丘
  20. MTK MT6169 2G用户指南参考资料

热门文章

  1. Hadoop hdfs文件操作常用命令
  2. Python 之列表的常用方法
  3. Qt Creator 最实用的快捷操作
  4. java定时器的使用
  5. python(matplotlib5)——Contours 等高线图
  6. H. Fight Against Monsters
  7. 安装jdk配置环境、cmd命令行测试环境变量配置是否正确及运行java程序、安装IDEA编写代码测试
  8. webview 修改html,使用自定义CSS在WebView中呈现HTML
  9. pythontkinter控件单选框怎么判断是否被选中_Python GUI编程(Tkinter)Radiobutton单选框控件...
  10. 网络通信-1(InetAddress、UDP、TCP、DatagramPacket、DatagramSocket、UDP通信示例)