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的对象,推断其是否存在然后返回对应结果给client。

bootstrap之ScrollTo的更多相关文章

  1. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  2. angularjs requeirjs配置相关

    尝试了网上的yeoman generator 生成的脚手架项目不甚理想 npm install -g generator-angular-require yo angular-require 就不用那 ...

  3. Bootstrap结合BootstrapTable的使用,分为两种模试显示列表。 自适应表格

    引用的css: <link href="@Url.Content("~/Css/bootstrap.min.css")" rel="styles ...

  4. bootstrap之Click大事

    上一篇文章中谈到了bootstrap流程,本文开始把目光bootstrap它可以接受指令(从源代码视图的透视.因为appium该项目现在还处于不断更新,因此,一些指令已经实现.也许未来会实现一些.从视 ...

  5. Appium Android Bootstrap源码分析之命令解析执行

    通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...

  6. Appuim源码剖析(Bootstrap)

    Appuim源码剖析(Bootstrap) SkySeraph Jan. 26th 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www. ...

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

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

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

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

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

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

随机推荐

  1. jq相关操作

    1事件: <div class="ele">123</div> box.onclick = function(ev){ ev:系统传入的事件对象 ele.i ...

  2. One-to-one

    创建模型 在本例中,Place 和 Restaurant 为一对一关系 from django.db import models class Place(models.Model): name = m ...

  3. R语言基础-list matrix array

    列表可以包含多种类型,如数字/字符/向量/data.frame/list # 创建含一个向量元素的list list1 = list(c(1,2,3)) # list2有三个元素 list2 = li ...

  4. iOS使用Reveal分析他人app界面

    本文转自http://blog.csdn.net/cuibo1123/article/details/45694657 安装: 首先前往 http://revealapp.com/download/  ...

  5. ubuntu 终端查看图片(eog)

    远程登陆服务器的话,是没有办法直接查看图片的,这时我们需要进入图片所在目录,然后通过终端命令查看图片. 想要查看图片,需要通过ssh -X登陆,然后在终端输入命令: eog 图片名

  6. Excel读取导入数据库碰到的问题

    1.未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序. 下载并安装驱动:http://download.microsoft.com/download/7/0/3/703 ...

  7. 82. Spring Boot – 启动彩蛋【从零开始学Spring Boot】

    我们在[28. SpringBoot启动时的Banner设置 ] 这一小节介绍过设置Spring Boot的Banner,但是实际当中,我们希望做的更漂亮,所以也就有了这小节Spring Boot-启 ...

  8. 1.ruby基本格式

    1.ruby对于空格是敏感的,很像shell 如:a + b 解释成 a+b(这是两个局部变量) a +b 解释成 a(+b) (这是一个方法调用) 2.在ruby中一行怎么才算结束? Ruby解释分 ...

  9. 【Android】android:ellipsize的使用以及一个点解决方法

    EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...

  10. 解开Future的神秘面纱之任务执行

    此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 任务提交到执行的流程 前文我们已经了解到一些Future的实现细节,这里我们来梳理一 ...