How to work with 'scroll_to' method in appium using Ruby?


I want to scroll to a particular text in android app and click on it.
Currently, it is scrolling up/down but it is unable to find the given text. I am working with ‘appium_lib’ for Ruby, Android (Yelp application). Below is the code snippet:

it ‘Should launch’ do
find_element(:name, ‘More Categories’).click
el = scroll_to(‘Food’)
el.find_element(:xpath, “//android.widget.CheckedTextView[@text=‘Food’ and @index=‘0’]”).click
rescue Exception => error
p error.message

Got it from this post:

def scroll_to text
# This is a workaround for Appium issue 4311,
# It can and should be replaced by the following when the issue is resolved
# appium.driver.scroll_to text
text = %Q(“#{text}”)
args = Appium::Android::scroll_uiselector(“new UiSelector().text(#{text})”)
find_element :uiautomator, args

Again, this scrolls to the element, but again scrolls back to the top of the page and then checks for the element again, and it fails.

How to make it stop once the element is identified and then click on it?