Just for my memo
When we scroll views using Appium, then we have two ways to achieve it.
1: Use UiScrollable
The below method is implemented in Ruby client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # @private | |
| def scroll_uiselector(content, index = 0) | |
| "new UiScrollable(new UiSelector().scrollable(true).instance(#{index})).scrollIntoView(#{content}.instance(0));" | |
| end | |
| # Scroll to the first element containing target text or description. | |
| # @param text [String] the text or resourceId to search for in the text value and content description | |
| # @param scrollable_index [Integer] the index for scrollable views. | |
| # @return [Element] the element scrolled to | |
| def scroll_to(text, scrollable_index = 0) | |
| text = %("#{text}") | |
| rid = resource_id(text, "new UiSelector().resourceId(#{text});") | |
| args = rid.empty? ? ["new UiSelector().textContains(#{text})", "new UiSelector().descriptionContains(#{text})"] : [rid] | |
| args.each_with_index do |arg, index| | |
| begin | |
| elem = find_element :uiautomator, scroll_uiselector(arg, scrollable_index) | |
| return elem | |
| rescue => e | |
| raise e if index == args.size – 1 | |
| end | |
| end | |
| end |
2: Use TouchAction or related actions.
The below is methods I used to implement as helper methods in my framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DURATION = 0.5 | |
| def scroll_to_uiselector_until(element:, start_x:, start_y:, end_x:, end_y:) | |
| times = 30 | |
| set_wait(0.5) | |
| ele = check_uiselector(element) | |
| return ele unless ele.nil? | |
| (0…times).each do | |
| Appium::TouchAction.new.swipe(start_x: start_x, start_y: start_y, | |
| end_x: end_x, end_y: end_y, | |
| duration: DURATION).perform | |
| ele = check_uiselector(element) | |
| return ele unless ele.nil? | |
| end | |
| set_wait # default | |
| raise ::Selenium::WebDriver::Error::NoSuchElementError, "no element to scroll" | |
| end | |
| private | |
| def check_uiselector(element_name) | |
| element = find_uiselector(element_name) | |
| if element.displayed? | |
| set_wait # default | |
| return element | |
| end | |
| rescue ::Selenium::WebDriver::Error::NoSuchElementError => e | |
| Appium::Logger.info e | |
| nil # no element | |
| end |
In my experience, 2 is almost stable than 1 since 1‘s behaviour depends on OS side and it also depends on devices,(and OSs).
I also tried scrollForward to make scroll fast, for example, but stability was less than 2 one.