How to Swipe and Scroll to the Last in Appium
Asked Answered
M

5

7

I am getting some Images from the API and i don't know the number and now i want to test the UI in Android through Appium and i want to scroll down to the Last image. How can I do this and also I don't know the what the title from the API is coming so that I can ScrollTo("Title") and also i am not able to swipe to the last. Is there anyway?

Malone answered 23/2, 2016 at 9:18 Comment(1)
could you specify what kind of layouts are you dealing with and would be great to see a screenshot of the application or any similar examples in here.Anywise
S
4

There is no way to know for sure whether you've scrolled to the last using appium because there is no UI reporting for the edges of a scrollview.

One way to know that you've reached the end of a scroll view without relying on devs is to compare the list of all children of the scrollview every time you swipe. If all children are exactly the same, you've reached the end. An example xpath for this would look like //android.widget.View[@content-desc="Your scrollview]//*, which would grab all children and descendants. Once you have a list to compare, check the content of all children nodes. This will only work if there's something unique in these items. If all items are completely general, there will be nothing to compare and this won't be reliable. Ask devs to add content descriptions or accessibility data to the items if possible.

Another option would be to have the devs embed a uniquely id'd invisible view at the top and bottom of the scrollview. That way, if it can be found by the driver, you know that you've reached the very edge of the view. If there are already unique elements at the edges of your scrollview, you can use those.

In the end, the devs of the application can really help out the process of scrolling, but hopefully the trick of comparing the current scrollview's children can help you.

Sande answered 13/12, 2016 at 21:17 Comment(1)
Possibly since the xml changes for every scroll you do you could hash the xml and compare the hash.Dickerson
H
3

You can use the screen dimensions to scroll down:

 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);
}
Hardness answered 23/2, 2016 at 15:29 Comment(3)
the solution does not confirm of reaching to the end, few more validations would be required with a li'l more element details.Anywise
How many times do you scrollDown to ensure you have reached the end of the scroll view displayed?Anywise
Still not upto what the concern is.Anywise
C
0

You can store all the images into a list using its available locator. Then use driver.scrollToExact(list.get(list.size()).getAttribute("name"));

Example:

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

or

driver.scrollToExact(images.get(images.size()).getText()); 
Closeknit answered 24/2, 2016 at 8:25 Comment(0)
G
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);
    }

The above example works with vertical scroll and it is based on the example given at this blog for horizontal scroll http://qaautomated.blogspot.in/2016/02/how-to-do-horizontal-scroll-in-appium.html I hope this works for you.

Gastronome answered 1/3, 2016 at 9:28 Comment(0)
J
0

To do this you must know resource id or cont-desc of scrollable element. You also need to know className of your scrollable element.

If you have cont-desc in scrollable list

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

If you have resource-id in scrollable list

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

If the screen cannot be scroll further it will throw error which will be catched by catch block.

Jowett answered 20/8, 2018 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.