Unable to scroll backward using UiAutomator2 in android

Hi team,

I am trying to swipe from bottom to top using the below code:

new UiScrollable(new UiSelector().className(“android.widget.ScrollView”)).scrollBackward()

for which i am getting error as “org.openqa.selenium.InvalidSelectorException: Could not parse expression new UiScrollable(new UiSelector().className("android.widget.ScrollView")).scrollBackward(): Last method called on a UiScrollable object must return a UiObject object

Kindly help me on this.

1 Like

use it in a way:

      try {
            driver.findElements(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector()).setAsVerticalList().flingToEnd(10)"));
        } catch (Exception e) {
            // ignore
        }

try-catch as far as “findElements” expecting element but “scrollBackward” returns boolean instead.

PS and try to remove className(“android.widget.ScrollView”) just not needed.

2 Likes

Thank you @Aleksei, Tried the same but getting the below error

Could not parse expression new UiScrollable(new UiSelector()).setAsVerticalList().flingToEnd(5): Last method called on a UiScrollable object must return a UiObject object

Write your code

It is looks like more error in code

This is my code…

getTLDriver().findElements(MobileBy.AndroidUIAutomator(“new UiScrollable(new UiSelector()).setAsVerticalList().flingToEnd(5)”));

Thanks for looking into it

add “;” ->

try {
            driver.findElements(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector()).setAsVerticalList().flingToEnd(10);"));
        } catch (Exception e) {
            // ignore
        }

PS i will check code later also…

Thank you @Aleksei , But not working. Kindly check the screenshot

hi. double checked. code is working. error as i said EXPECTED! just ignore it. “;” is no matter.
e.g. i tested with horizontal scrollView with mine app:

        sleep(5000);
        try {
            driverThreadLocal().findElements(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector()).setAsHorizontalList().flingToEnd(10)"));
        } catch (Exception e) {
            // ignore
        }
        log("scrollBackward done");
        try {
            driverThreadLocal().findElements(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector()).setAsHorizontalList().flingToBeginning(10)"));
        } catch (Exception e) {
            // ignore
        }
        log("scrollForward done");

Thank you @Aleksei Its working as you said but its throwing exception when its getting into catch block so i ignored in printing the error

Here is my code which works fine. Try this if you want to scroll down till the end of screen in android.

try {

Thread.sleep(5000);
StepDefs.driver.findElement(MobileBy.AndroidUIAutomator(“new UiScrollable(new UiSelector().scrollable(true))”+".scrollToEnd(55);"));

}
catch (Exception e)
{

System.out.println(e.getMessage());

}

Note: The Int value 55 is the number of maxSwipes you want to perform on the screen.

https://developer.android.com/reference/android/support/test/uiautomator/UiScrollable#scrolltoend