手机自动化测试: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. php查询,多条件查询

    单条件查询: 1.先要有一张表,显示出表中的数据: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  2. 解决项目中找不到Maven Dependencies

    项目中找不到Maven Dependencies 正常的Maven项目应该是这样的 自己的项目中却没有Maven Dependencies,自己百度了, 发现解决不了,最后发现在.classpath和 ...

  3. 【转】Java通过IText导出word和pdf

    原帖地址:http://blog.csdn.net/zwx19921215/article/details/34439851 最近做的项目中需要用到把Highcharts图表导出word和pdf的功能 ...

  4. 什么是Git?

    其实我在写这篇随笔的时候连Git是什么都不知道,只是听说过,也注册了一个GitHub的账号,但并不会玩. 我也是查看了半天的网页才明白一个大概,但我觉得以后肯定会经常用到它. 简单的来说, Git 是 ...

  5. jprofiler安装图解 ( 7_1 )

    环境: 1.sun jdk1.6.0 2.jprofiler_windows_6_0_2.exe 安装 1. jdk, 安装略... 2. jprofiler安装 一路next 到Enter lice ...

  6. angular drag and drop (marceljuenemann) 笔记

    这是原文 http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple 看起来很多功能,所以我只记入我需要的部分 ...

  7. Git的简单安装

    git 的安装及源代码上传 首先去官网把git下载好,这里给大家提供个官网下载地址 https://git-scm.com/downloads 安装完成后,在开始菜单里找到“Git”->“Git ...

  8. 关于IAR开发STM32配置

    因为自己要学Msp430还有ZigBee发现IAR真的挺好用,,,所以以后想着就用IAR写单片机程序,, 这次配置我不会把程序的配置弄得有条理,分开文件夹存放,,,我要把那些文件全都放到一块,,弄得乱 ...

  9. python之SQLAlchemy ORM 上

    前言: SQLAlchmey是暑假学的,当时学完后也没及时写博客整理下.这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.下篇博客整理写篇关于Web框架和django基础~ ...

  10. C#基础——数组(冒泡排序)

    数组 所谓数组,就是相同数据类型的元素按一定的顺序的集合,就是把有限个类型相同的变量用一个名字来命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标.组成数组的各个变量称为数组的分量 ...