Appium: A Conditional Scrolling Workaround

I had to write some more or less platform independent tests for a mobile application using Appium. It's a nice framework, but I had troubles, because the scrolling actions did't work like expected. So I used the following workaround.

Altough scrolling is supported, it does not work well. And there are a lot of bug reports regarding scrolling (e.g. 6140, 6023, 5723, 5629).

I tried the following options (without success):

scrollTo("Element")
scroll_to("Element")
execute_script "mobile: scroll", :direction => 'down', :element => ele
execute_script "mobile: scrollTo", :element => ele

So I used the following workaround for android:

q = xpath_exists? "Element"
while !q
    7.times do
        $driver.press_keycode 20
    end

    sleep 1
    q = xpath_exists? "Element"
end

First it checks if the element is already visible or not. If it is visible no scrolling action is needed, otherwise it presses the "down"-key 7 times and then tries to find the element again. For iOS I used a different approach. The swipe action works so I swipe down instead of pressing the "down"-button several times:

q = xpath_exists? "Element"
while !q
    swipe(start_x: 0, start_y: 450, end_x: 0, end_y: 250, duration: 2000)

    sleep 1
    q = xpath_exists? "Element"
end

Links