0

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.

Erik Schmidt
  • 85
  • 1
  • 8

1 Answers1

0

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.

Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • First of all, thank you a lot for the quick response!! I did as suggested and it works, but it scrolls several times up and down before finding and clicking the desired element. Anyway I would rather like to do more of the logic in the test project (Kotlin) than performing such long task on the server side ( with that I mean the long java command we pass as String parameter). I guess therefore I had to import some sort of UIAutomator2 library to have access to UiSelector class etc... Is there such a library out there? – Erik Schmidt Feb 10 '20 at 11:13
  • @ErikSchmidt, This is UiAutomator2 native feature. As we are not telling uiAutomator2 engine which direct element could be (Up and down) so it will make it scroll Up and down for searching an element. As I am using it I can say it most stable function in Appium library for scrolling. we have option scroll with touch action class but then we haveto loop it for scrolling and use if condition for element's visiblity for each scroll and would not be feasible solution as it increasing complexity. – Muzzamil Feb 10 '20 at 11:18
  • I think you're right. I'll use your implementation and hope that UIAnimator2 will be mirrored in the appium java-client on day, so that I can call the functions directly instead of sending a String that is later executed as a line of java code. Thank you a lot again! – Erik Schmidt Feb 10 '20 at 12:05