Unable to Scroll Elements inside ListView with Appium Selenium Android

Viewed 1525

For a few days I try to find a simple solution for scroll inside a list view

Since that Appium Inspector not provide the all elements of the list (only by manualy swipe and refresh the inspector then the new elements with the new instance loads.

After research i find out the distance between list item is as follow:

Item Location= (15, 828)
Item Location= (15, 1209)
Item Location= (15, 1590)

So i guess that every click need to scroll 381 pixels down, after so many tries I cannot find solution for this part of code and will be happy if someone can help with that:

List<WebElement> list = driver.findElements(By.id("net.balink.diplomat.qa:id/btnAddToCart"));
System.out.println("List Size is= " + list.size());

for (int j = 0; j < list.size(); j++) {
    WebElement listItem = list.get(j);
    Point location = listItem.getLocation();
    System.out.println("Location= " + location);
    listItem.click();

    //to do here: swipe 381 px down after any click, then re-initialize 
    //the listItem in order to get a new index - and the loop when 
    //theres no more list items

    Thread.sleep(2000);
}
2 Answers

The more reliable approach is to use native UiAutomator way:

So first of all find parent view of your list elements with scrollable: true attribute in place. That definitely should be at least one.

Then use inspector to build & test your UiAutomator locator

MobileElement elementScrollTo = driver
            .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
            + ".resourceId(\"android:id/list\")).scrollIntoView("
            + "new UiSelector().text(\"Some text\"));");

UiSelector has pretty rich API , check it and find what suits you best.

This approach is better than TouchActions as you are not dependent on device screen resolution/coordinates.

I am using the following method for scrolling screen:

public static MobileElement scrollElementByContentDesc(String scrollableList, String uiSelector, String textToSearchInList) {
        return driver.findElement(MobileBy.AndroidUIAutomator(
                "new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByDescription("
                        + "new UiSelector().className(\"" + uiSelector + "\"), \"" + textToSearchInList+ "\")"));

    }

scrollableList is id of your scrollable List element,

uiSelector is the className of the item of list,

textToSearchInList can be any text that you need to search in the list. It can be any random text if your purpose is just to scroll till the end of the list.

Call this method from any method:

public void someMethod(){
//other code
try{
    MobileElement element= scrollElementByContentDesc("your scrollableList id",
                "uiSelector classname", "any text you need to find in the list);
}catch(Exception e){
    //do nothing
}
}

If you want to swipe by co0rdinate, you can do like this.

import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import java.util.concurrent.TimeUnit;
import static java.time.Duration.ofSeconds;

TouchAction action = new TouchAction(driver); 
action.press(PointOption.point(115, 650)).waitAction(WaitOptions.waitOptions(ofSeconds(1)))
                .moveTo(PointOption.point(115, 350)).release().perform();

Make sure to import correct library.

Related