Skip to content

Commit b7daaac

Browse files
emenKazuCocoa
authored andcommitted
fix: Fix Android scroll_to and scroll_to_exact (#638)
* fix: Fix Android scroll_to and scroll_to_exact * Remove unnecessary find_element call
1 parent 65b2c7a commit b7daaac

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

android_tests/lib/android/specs/android/element/generic.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def partial
3232

3333
# scroll_to is broken
3434
t 'scroll_to' do
35-
wait { find('Views').click }
35+
wait { scroll_to('Views').click }
3636
wait { scroll_to('scrollbars').text.must_equal 'ScrollBars' }
3737

3838
wait { find('ScrollBars').click }
@@ -45,7 +45,7 @@ def partial
4545
end
4646

4747
t 'scroll_to_exact' do
48-
wait { find('Views').click }
48+
wait { scroll_to('Views').click }
4949

5050
wait { scroll_to_exact('ScrollBars').text.must_equal 'ScrollBars' }
5151
wait { find('ScrollBars').click }

lib/appium_lib/android/element/generic.rb

+20-12
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ def scroll_uiselector(content, index = 0)
3939
# @return [Element] the element scrolled to
4040
def scroll_to(text, scrollable_index = 0)
4141
text = %("#{text}")
42-
43-
args = scroll_uiselector("new UiSelector().textContains(#{text})", scrollable_index) +
44-
scroll_uiselector("new UiSelector().descriptionContains(#{text})", scrollable_index) +
45-
scroll_uiselector(resource_id(text, "new UiSelector().resourceId(#{text});"), scrollable_index)
46-
47-
find_element :uiautomator, args
42+
rid = resource_id(text, "new UiSelector().resourceId(#{text});")
43+
args = rid.empty? ? ["new UiSelector().textContains(#{text})", "new UiSelector().descriptionContains(#{text})"] : [rid]
44+
args.each_with_index do |arg, index|
45+
begin
46+
elem = find_element :uiautomator, scroll_uiselector(arg, scrollable_index)
47+
return elem
48+
rescue => e
49+
raise e if index == args.size - 1
50+
end
51+
end
4852
end
4953

5054
# Scroll to the first element with the exact target text or description.
@@ -53,12 +57,16 @@ def scroll_to(text, scrollable_index = 0)
5357
# @return [Element] the element scrolled to
5458
def scroll_to_exact(text, scrollable_index = 0)
5559
text = %("#{text}")
56-
57-
args = scroll_uiselector("new UiSelector().text(#{text})", scrollable_index) +
58-
scroll_uiselector("new UiSelector().description(#{text})", scrollable_index) +
59-
scroll_uiselector(resource_id(text, "new UiSelector().resourceId(#{text});"), scrollable_index)
60-
61-
find_element :uiautomator, args
60+
rid = resource_id(text, "new UiSelector().resourceId(#{text});")
61+
args = rid.empty? ? ["new UiSelector().text(#{text})", "new UiSelector().description(#{text})"] : [rid]
62+
args.each_with_index do |arg, index|
63+
begin
64+
elem = find_element :uiautomator, scroll_uiselector(arg, scrollable_index)
65+
return elem
66+
rescue => e
67+
raise e if index == args.size - 1
68+
end
69+
end
6270
end
6371
end # module Android
6472
end # module Appium

0 commit comments

Comments
 (0)