ScrollTo

  1. package io.appium.android.bootstrap.handler;
  2.  
  3. import com.android.uiautomator.core.UiObject;
  4. import com.android.uiautomator.core.UiObjectNotFoundException;
  5. import com.android.uiautomator.core.UiScrollable;
  6. import com.android.uiautomator.core.UiSelector;
  7. import io.appium.android.bootstrap.*;
  8. import org.json.JSONException;
  9.  
  10. import java.util.Hashtable;
  11.  
  12. /**
  13. * This handler is used to scroll to elements in the Android UI.
  14. *
  15. * Based on the element Id of the scrollable, scroll to the object with the
  16. * text.
  17. *
  18. */
  19. public class ScrollTo extends CommandHandler {
  20.  
  21. /*
  22. * @param command The {@link AndroidCommand}
  23. *
  24. * @return {@link AndroidCommandResult}
  25. *
  26. * @throws JSONException
  27. *
  28. * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
  29. * bootstrap.AndroidCommand)
  30. */
  31. @Override
  32. public AndroidCommandResult execute(final AndroidCommand command)
  33. throws JSONException {
  34. if (!command.isElementCommand()) {
  35. return getErrorResult("A scrollable view is required for this command.");
  36. }
  37.  
  38. try {
  39. Boolean result;
  40. final Hashtable<String, Object> params = command.params();
  41. final String text = params.get("text").toString();
  42. final String direction = params.get("direction").toString();
  43.  
  44. final AndroidElement el = command.getElement();
  45.  
  46. if (!el.getUiObject().isScrollable()) {
  47. return getErrorResult("The provided view is not scrollable.");
  48. }
  49.  
  50. final UiScrollable view = new UiScrollable(el.getUiObject().getSelector());
  51.  
  52. if (direction.toLowerCase().contentEquals("horizontal")
  53. || view.getClassName().contentEquals(
  54. "android.widget.HorizontalScrollView")) {
  55. view.setAsHorizontalList();
  56. }
  57. view.scrollToBeginning(100);
  58. view.setMaxSearchSwipes(100);
  59. result = view.scrollTextIntoView(text);
  60. view.waitForExists(5000);
  61.  
  62. // make sure we can get to the item
  63. UiObject listViewItem = view.getChildByInstance(
  64. new UiSelector().text(text), 0);
  65.  
  66. // We need to make sure that the item exists (visible)
  67. if (!(result && listViewItem.exists())) {
  68. return getErrorResult("Could not scroll element into view: " + text);
  69. }
  70. return getSuccessResult(result);
  71. } catch (final UiObjectNotFoundException e) {
  72. return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
  73. } catch (final NullPointerException e) { // el is null
  74. return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
  75. } catch (final Exception e) {
  76. return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
  77. }
  78. }
  79. }

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

首先推断控件是否是能够滚动的,然后创建UiScrollable对象,由于默认的滚动方式是垂直方向的,假设须要的是水平方向的的话。还要设置方向为水平。

  1. view.setAsHorizontalList();

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

然后開始调用

  1. view.scrollTextIntoView(text);

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

  1. view.waitForExists(5000);

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

  1. UiObject listViewItem = view.getChildByInstance(
  2. 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. 小数据池 is 和 ==的区别

    小数据池 一.小数据池 1)代码块 python程序是由代码块构成的,一个代码块的文本作为pythont程序执行的单元 官方文档: A Python program is constructed fr ...

  2. Repo command reference

    Repo command reference In this document init sync upload diff download forall prune start status Rep ...

  3. gg mirror

    https://gufen.ga/ (无广告,原guso.ml,ggso.ga,guge.ga) https://a.aiguso.tk (无广告,体验良好) https://a.freedo.gq/ ...

  4. 00031_ArrayList集合中常用的方法

    1.ArrayList集合提供的一些常用方法 import java.util.ArrayList; public class ArrayListDemo01 { public static void ...

  5. Java-转换Unicode和utf-8

    package com.tj; import java.io.UnsupportedEncodingException; public class MyClass implements Cloneab ...

  6. python算法-快速排序

    快速排序: 学习快速排序,要先复习下递归: 递归的2个条件: 1. 函数自己调用自己 2.有一个退出的条件 练习:基于递归下一个函数,计算n!并且求出当n等于10的值. n!=n * n-1*…..* ...

  7. Leetcode 373.查找和最小的k对数字

    查找和最小的k对数字 给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k. 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2. 找到和最小的 ...

  8. VS2010SP1修复补丁&Microsoft Visual Studio 2010 Service Pack 1

    网上比较难找,官网找也容易找错,现在贴出来 补丁包下载地址:链接:https://pan.baidu.com/s/1_tFzXL6PaHiWk3JeRBw0ww 密码:z38k

  9. [Kubernetes]容器健康检查和恢复机制

    在Kubernetes中,可以为Pod里的容器定义一个健康检查探针(Probe),这样Kubernetes会根据这个Probe的返回值决定这个容器的状态,而不是直接以容器是否允许(来自Docker返回 ...

  10. tab栏切换效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...