【问题标题】:How to Swipe and Scroll to the Last in Appium如何在 Appium 中滑动并滚动到最后一个
【发布时间】:2016-06-05 01:04:57
【问题描述】:

我从 API 获取一些图像,但我不知道数字,现在我想通过 Appium 在 Android 中测试 UI,我想向下滚动到最后一张图像。我该怎么做,而且我不知道 API 的标题是什么,所以我可以 ScrollTo("Title") 并且我无法滑动到最后。到底有没有?

【问题讨论】:

  • 您能否指定您正在处理的布局类型,如果您能在此处查看应用程序的屏幕截图或任何类似示例,您会很高兴。

标签: android listview scroll swipe appium


【解决方案1】:

没有办法确定您是否使用 appium 滚动到最后一个,因为滚动视图的边缘没有 UI 报告。

在不依赖开发人员的情况下知道您已到达滚动视图末尾的一种方法是每次滑动时比较滚动视图的所有子视图的列表。如果所有的孩子都完全一样,那么你已经走到了尽头。一个示例 xpath 看起来像//android.widget.View[@content-desc="Your scrollview]//*,它会抓取所有的孩子和后代。一旦有了要比较的列表,请检查所有子节点的内容。这只有在这些物品中有独特之处时才有效。如果所有项目都是完全通用的,那么就没有什么可比较的了,这也不可靠。如果可能,请开发人员为项目添加内容描述或可访问性数据。

另一种选择是让开发人员在滚动视图的顶部和底部嵌入一个唯一标识的不可见视图。这样,如果驾驶员可以找到它,您就知道您已经到达了视野的最边缘。如果你的滚动视图边缘已经有独特的元素,你可以使用它们。

最后,应用程序的开发人员确实可以帮助滚动过程,但希望比较当前滚动视图的子项的技巧可以帮助您。

【讨论】:

  • 可能由于每次滚动的 xml 都会发生变化,您可以散列 xml 并比较散列。
【解决方案2】:

您可以使用屏幕尺寸向下滚动:

 public void scrollDown() {
    Dimension size = driver.manage().window().getSize();
    int x = size.getWidth() / 2;
    int starty = (int) (size.getHeight() * 0.60);
    int endy = (int) (size.getHeight() * 0.10);
    driver.swipe(x, starty, x, endy, 2000);
}

【讨论】:

  • 解决方案没有确认到达终点,需要更多的验证和更多的元素细节。
  • scrollDown 多少次以确保您已到达显示的滚动视图的末尾?
  • 仍然无法解决问题。
【解决方案3】:

您可以使用可用的定位器将所有图像存储到一个列表中。然后使用driver.scrollToExact(list.get(list.size()).getAttribute("name"));

例子:

List<mobileElement> images = driver.findElementsByClass("<locator>");
driver.scrollToExact(images.get(images.size()).getAttribute("name")); 

driver.scrollToExact(images.get(images.size()).getText()); 

【讨论】:

    【解决方案4】:
     @Test
        public void testScroll()throws Exception
        {
            for(int i=0;i<4;i++)
            {
                Thread.sleep(2000);
                if (driver.findElement(By.name("end_item")).isDisplayed())
                {
                    driver.findElement(By.name("end_item")).click();
                    break;
                }
                else
                {
                    horizontalScroll();
                }
    
            }
        }
        public void verticalScroll()
        {
            size=driver.manage().window().getSize();
            int y_start=(int)(size.height*0.60);
            int y_end=(int)(size.height*0.30);
            int x=size.width/2;
            driver.swipe(x,y_start,x,y_end,4000);
        }
    

    以上示例适用于垂直滚动,它基于此博客中给出的水平滚动示例 http://qaautomated.blogspot.in/2016/02/how-to-do-horizontal-scroll-in-appium.html 我希望这对你有用。

    【讨论】:

      【解决方案5】:

      为此,您必须知道可滚动元素的 resource idcont-desc。您还需要知道可滚动元素的 className

      如果您在可滚动列表中有 cont-desc

      try {
          String scrollableList="your con-desc of scrollable List";
          String elementClassName="android.something.something";
          String anyText="any text";
      
          driver.findElement(MobileBy.AndroidUIAutomator(
                          "new UiScrollable(new UiSelector().description(\"" + scrollableList + "\")).getChildByText("
                                  + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
          }catch (Exception e){
                  System.out.println("Cannot scroll further");
      }
      

      如果您在可滚动列表中有资源 ID

      try {
          String scrollableList="your con-desc of scrollable List";
          String elementClassName="android.something.something";
          String anyText="any text";
      
          driver.findElement(MobileBy.AndroidUIAutomator(
                          "new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByText("
                                  + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
       }catch (Exception e){
                  System.out.println("Cannot scroll further");
      }
      

      如果屏幕不能进一步滚动,则会抛出错误,该错误将被 catch 块捕获。

      【讨论】:

        猜你喜欢
        • 2017-01-01
        • 1970-01-01
        • 2021-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多