如何在Appium中滑动并滚动到最后

时间:2016-02-23 09:18:23

标签: android listview scroll swipe appium

我从API获取了一些图片,我不知道这个数字,现在我想通过Appium在Android中测试UI,我想向下滚动到最后一张图片。我怎么能这样做,而且我不知道API的标题是什么,所以我可以ScrollTo(“标题”),我也无法刷到最后。反正有吗?

5 个答案:

答案 0 :(得分:3)

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

知道您在不依赖开发人员的情况下到达滚动视图结尾的一种方法是每次滑动时比较滚动视图的所有子项列表。如果所有孩子都完全一样,那么你已经到了最后。这个示例的xpath看起来像//android.widget.View[@content-desc="Your scrollview]//*,它会抓住所有子节点和后代。获得要比较的列表后,请检查所有子节点的内容。这只有在这些项目中有独特之处时才有效。如果所有项目都是完全一般的,那么就没有可比较的东西了,这不可靠。如果可能,请devs将内容描述或辅助功能数据添加到项目中。

另一个选择是让开发者在滚动视图的顶部和底部嵌入一个唯一的id&f不可见视图。这样,如果驾驶员可以找到它,您就知道您已经到达了视图的最边缘。如果您的滚动视图边缘已经存在唯一元素,则可以使用这些元素。

最后,应用程序的开发人员可以真正帮助滚动过程,但希望比较当前scrollview的孩子可以帮助你。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

 @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 我希望这适合你。

答案 4 :(得分:0)

为此,您必须知道可滚动元素的资源ID cont-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");
}

如果滚动列表中有resource-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块捕获。