如何滑动和滚动到 Appium 中的最后一个

How to Swipe and Scroll to the Last in Appium

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

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

 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);
}

您可以使用其可用定位器将所有图像存储到列表中。然后使用 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()); 
 @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 我希望这对你有用。

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

无需依赖开发人员即可知道您已到达滚动视图末尾的一种方法是,每次滑动时比较滚动视图的所有 children 列表。如果所有 children 都完全相同,则您已到达终点。一个示例 xpath 看起来像 //android.widget.View[@content-desc="Your scrollview]//*,它将获取所有 children 和后代。有了要比较的列表后,请检查所有 children 节点的内容。这仅在这些项目中有独特之处时才有效。如果所有项目都完全笼统,那就没有什么可比较的了,这就不可靠了。如果可能,要求开发人员向项目添加内容描述或可访问性数据。

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

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

为此,您必须知道可滚动元素的 资源 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 块捕获。