Module: Calabash::Android::TouchHelpers
Instance Method Summary collapse
- #double_tap(query_string, options = {}) ⇒ Object
- #drag(*args) ⇒ Object
- #execute_gesture(multi_touch_gesture) ⇒ Object
- #find_coordinate(uiquery, options = {}) ⇒ Object
- #flick(query_string, direction, options = {}) ⇒ Object
- #flick_down(options = {}) ⇒ Object
- #flick_left(options = {}) ⇒ Object
- #flick_right(options = {}) ⇒ Object
- #flick_up(options = {}) ⇒ Object
- #long_press(query_string, options = {}) ⇒ Object
- #long_press_when_element_exists(query_string, options = {}) ⇒ Object
- #pan(query_string, direction, options = {}) ⇒ Object
- #pan_down(options = {}) ⇒ Object
- #pan_left(options = {}) ⇒ Object
- #pan_right(options = {}) ⇒ Object
- #pan_up(options = {}) ⇒ Object
- #pinch(query_string, direction, options = {}) ⇒ Object
- #pinch_in(options = {}) ⇒ Object
- #pinch_out(options = {}) ⇒ Object
- #query_result?(uiquery) ⇒ Boolean
- #tap(mark, *args) ⇒ Object
- #tap_mark(mark, *args) ⇒ Object
- #tap_when_element_exists(query_string, options = {}) ⇒ Object
- #touch(query_string, options = {}) ⇒ Object
Instance Method Details
#double_tap(query_string, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/calabash-android/touch_helpers.rb', line 39 def double_tap(query_string, ={}) if query_result?(query_string) center_x, center_y = find_coordinate(query_string, ) perform_action("double_tap_coordinate", center_x, center_y) else execute_gesture(Gesture.with_parameters(Gesture.double_tap(), {query_string: query_string}.merge())) end end |
#drag(*args) ⇒ Object
69 70 71 |
# File 'lib/calabash-android/touch_helpers.rb', line 69 def drag(*args) pan(*args) end |
#execute_gesture(multi_touch_gesture) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/calabash-android/touch_helpers.rb', line 6 def execute_gesture(multi_touch_gesture) result = JSON.parse(http("/gesture", JSON.parse(multi_touch_gesture.to_json), read_timeout: multi_touch_gesture.timeout+10)) if result['outcome'] != 'SUCCESS' raise "Failed to perform gesture. #{result['reason']}" end nil end |
#find_coordinate(uiquery, options = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/calabash-android/touch_helpers.rb', line 125 def find_coordinate(uiquery, ={}) raise "Cannot find nil" unless uiquery element = execute_uiquery(uiquery) raise "No elements found. Query: #{uiquery}" if element.nil? x = element["rect"]["center_x"] y = element["rect"]["center_y"] if [:offset] x += [:offset][:x] || 0 y += [:offset][:y] || 0 end [x, y] end |
#flick(query_string, direction, options = {}) ⇒ Object
109 110 111 |
# File 'lib/calabash-android/touch_helpers.rb', line 109 def flick(query_string, direction, ={}) execute_gesture(Gesture.with_parameters(Gesture.swipe(direction, {flick: true}.merge()), {query_string: query_string}.merge())) end |
#flick_down(options = {}) ⇒ Object
105 106 107 |
# File 'lib/calabash-android/touch_helpers.rb', line 105 def flick_down(={}) flick("* id:'content'", :down, ) end |
#flick_left(options = {}) ⇒ Object
93 94 95 |
# File 'lib/calabash-android/touch_helpers.rb', line 93 def flick_left(={}) flick("DecorView", :left, ) end |
#flick_right(options = {}) ⇒ Object
97 98 99 |
# File 'lib/calabash-android/touch_helpers.rb', line 97 def flick_right(={}) flick("DecorView", :right, ) end |
#flick_up(options = {}) ⇒ Object
101 102 103 |
# File 'lib/calabash-android/touch_helpers.rb', line 101 def flick_up(={}) flick("* id:'content'", :up, ) end |
#long_press(query_string, options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/calabash-android/touch_helpers.rb', line 49 def long_press(query_string, ={}) if query_result?(query_string) center_x, center_y = find_coordinate(query_string, ) length = [:length] perform_action("long_press_coordinate", center_x, center_y, *(length unless length.nil?)) else length = [:length] if length puts "Using the length key is deprecated. Use 'time' (in seconds) instead." [:time] = length/1000.0 end [:time] ||= 1 touch(query_string, ) end end |
#long_press_when_element_exists(query_string, options = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/calabash-android/touch_helpers.rb', line 153 def long_press_when_element_exists(query_string, ={}) .merge!({action: lambda {|q| long_press(q, )}}) if [:scroll] == true scroll_to(query_string, ) else when_element_exists(query_string, ) end end |
#pan(query_string, direction, options = {}) ⇒ Object
89 90 91 |
# File 'lib/calabash-android/touch_helpers.rb', line 89 def pan(query_string, direction, ={}) execute_gesture(Gesture.with_parameters(Gesture.swipe(direction, ), {query_string: query_string}.merge())) end |
#pan_down(options = {}) ⇒ Object
85 86 87 |
# File 'lib/calabash-android/touch_helpers.rb', line 85 def pan_down(={}) pan("* id:'content'", :down, ) end |
#pan_left(options = {}) ⇒ Object
73 74 75 |
# File 'lib/calabash-android/touch_helpers.rb', line 73 def pan_left(={}) pan("DecorView", :left, ) end |
#pan_right(options = {}) ⇒ Object
77 78 79 |
# File 'lib/calabash-android/touch_helpers.rb', line 77 def pan_right(={}) pan("DecorView", :right, ) end |
#pan_up(options = {}) ⇒ Object
81 82 83 |
# File 'lib/calabash-android/touch_helpers.rb', line 81 def pan_up(={}) pan("* id:'content'", :up, ) end |
#pinch(query_string, direction, options = {}) ⇒ Object
121 122 123 |
# File 'lib/calabash-android/touch_helpers.rb', line 121 def pinch(query_string, direction, ={}) execute_gesture(Gesture.with_parameters(Gesture.pinch(direction, ), {query_string: query_string}.merge())) end |
#pinch_in(options = {}) ⇒ Object
117 118 119 |
# File 'lib/calabash-android/touch_helpers.rb', line 117 def pinch_in(={}) pinch("* id:'content'", :in, ) end |
#pinch_out(options = {}) ⇒ Object
113 114 115 |
# File 'lib/calabash-android/touch_helpers.rb', line 113 def pinch_out(={}) pinch("* id:'content'", :out, ) end |
#query_result?(uiquery) ⇒ Boolean
163 164 165 166 167 168 169 170 171 |
# File 'lib/calabash-android/touch_helpers.rb', line 163 def query_result?(uiquery) element = if uiquery.is_a?(Array) uiquery.first else uiquery end element.is_a?(Hash) && element.has_key?('rect') && element['rect'].has_key?('center_x') && element['rect'].has_key?('center_y') end |
#tap(mark, *args) ⇒ Object
16 17 18 19 20 |
# File 'lib/calabash-android/touch_helpers.rb', line 16 def tap(mark, *args) puts "Warning: The method tap is deprecated. Use tap_mark instead. In later Calabash versions we will change the semantics of `tap` to take a general query." tap_mark(mark, *args) end |
#tap_mark(mark, *args) ⇒ Object
22 23 24 |
# File 'lib/calabash-android/touch_helpers.rb', line 22 def tap_mark(mark, *args) touch("* marked:'#{mark}'", *args) end |
#tap_when_element_exists(query_string, options = {}) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/calabash-android/touch_helpers.rb', line 143 def tap_when_element_exists(query_string, ={}) .merge!({action: lambda {|q| touch(q, )}}) if [:scroll] == true scroll_to(query_string, ) else when_element_exists(query_string, ) end end |
#touch(query_string, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/calabash-android/touch_helpers.rb', line 26 def touch(query_string, ={}) if query_result?(query_string) center_x, center_y = find_coordinate(query_string, ) perform_action("touch_coordinate", center_x, center_y) else if query_string.nil? && (.nil? || .empty?) raise "Can't touch nil" end execute_gesture(Gesture.with_parameters(Gesture.tap(), {query_string: query_string}.merge())) end end |