appium-androidandroid-scrollappium-desktop

Appium can't scroll in Android App when starting on clickable element


My situation

I want to write UI-tests for an Android App and therefore I need to scroll in some of the App's fragments. The tests are written in Kotlin, Appium version is v1.15.1 .

My problem

I use the standard approach for scrolling(see below) and it works fine, as long as the coordinates of my starting point don't fall onto a clickable element. I also observed this behaviour when navigating through the App with the Appium Desktop Inspector.

My current approach

PlatformTouchAction(driver as AppiumDriver)
            .press(PointOption.point(100, 500))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
            .moveTo(PointOption.point(100, 100))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
            .release()
            .perform()

As mentioned before this works if the starting point (100,500) is not on a clickable element.
If for example a button happens to be at (100,500) the scroll/swipe is not performed, but in fact on scroll listeners are still called.


Solution

  • You can scroll with help of resource id of element. This can be achieved with UiAutomator2 as automation engine. You need to use automation name as UiAutomator2 in desires capabilities.

    Add in desired capability UiAutomator2 if you are using appium as automation engine.

    capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
    

    Now use below functions if you have element's resource id, and index as 0 if there is one element on page.

    public void scrollByID(String Id, int index) {
    
            try {
    
                 driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\""+Id+"\").instance("+index+"));")); 
    
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    

    This is dynamic approach it will scroll till element is not visible.