Module: Calabash::Android::TouchHelpers

Includes:
Gestures
Included in:
Operations
Defined in:
lib/calabash-android/touch_helpers.rb

Instance Method Summary collapse

Instance Method Details

#double_tap(query_string, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/calabash-android/touch_helpers.rb', line 36

def double_tap(query_string, options={})
  if query_result?(query_string)
    center_x, center_y = find_coordinate(query_string, options)

    perform_action("double_tap_coordinate", center_x, center_y)
  else
    execute_gesture(Gesture.with_parameters(Gesture.double_tap(options), {query_string: query_string}.merge(options)))
  end
end

#drag(*args) ⇒ Object



66
67
68
# File 'lib/calabash-android/touch_helpers.rb', line 66

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/calabash-android/touch_helpers.rb', line 122

def find_coordinate(uiquery, options={})
  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 options[:offset]
    x += options[:offset][:x] || 0
    y += options[:offset][:y] || 0
  end

  [x, y]
end

#flick(query_string, direction, options = {}) ⇒ Object



106
107
108
# File 'lib/calabash-android/touch_helpers.rb', line 106

def flick(query_string, direction, options={})
  execute_gesture(Gesture.with_parameters(Gesture.swipe(direction, {flick: true}.merge(options)), {query_string: query_string}.merge(options)))
end

#flick_down(options = {}) ⇒ Object



102
103
104
# File 'lib/calabash-android/touch_helpers.rb', line 102

def flick_down(options={})
  flick("* id:'content'", :down, options)
end

#flick_left(options = {}) ⇒ Object



90
91
92
# File 'lib/calabash-android/touch_helpers.rb', line 90

def flick_left(options={})
  flick("DecorView", :left, options)
end

#flick_right(options = {}) ⇒ Object



94
95
96
# File 'lib/calabash-android/touch_helpers.rb', line 94

def flick_right(options={})
  flick("DecorView", :right, options)
end

#flick_up(options = {}) ⇒ Object



98
99
100
# File 'lib/calabash-android/touch_helpers.rb', line 98

def flick_up(options={})
  flick("* id:'content'", :up, options)
end

#long_press(query_string, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/calabash-android/touch_helpers.rb', line 46

def long_press(query_string, options={})
  if query_result?(query_string)
    center_x, center_y = find_coordinate(query_string, options)
    length = options[:length]

    perform_action("long_press_coordinate", center_x, center_y, *(length unless length.nil?))
  else
    length = options[:length]

    if length
      puts "Using the length key is deprecated. Use 'time' (in seconds) instead."
      options[:time] = length/1000.0
    end

    options[:time] ||= 1

    touch(query_string, options)
  end
end

#long_press_when_element_exists(query_string, options = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/calabash-android/touch_helpers.rb', line 150

def long_press_when_element_exists(query_string, options={})
  options.merge!({action: lambda {|q| long_press(q, options)}})

  if options[:scroll] == true
    scroll_to(query_string, options)
  else
    when_element_exists(query_string, options)
  end
end

#pan(query_string, direction, options = {}) ⇒ Object



86
87
88
# File 'lib/calabash-android/touch_helpers.rb', line 86

def pan(query_string, direction, options={})
  execute_gesture(Gesture.with_parameters(Gesture.swipe(direction, options), {query_string: query_string}.merge(options)))
end

#pan_down(options = {}) ⇒ Object



82
83
84
# File 'lib/calabash-android/touch_helpers.rb', line 82

def pan_down(options={})
  pan("* id:'content'", :down, options)
end

#pan_left(options = {}) ⇒ Object



70
71
72
# File 'lib/calabash-android/touch_helpers.rb', line 70

def pan_left(options={})
  pan("DecorView", :left, options)
end

#pan_right(options = {}) ⇒ Object



74
75
76
# File 'lib/calabash-android/touch_helpers.rb', line 74

def pan_right(options={})
  pan("DecorView", :right, options)
end

#pan_up(options = {}) ⇒ Object



78
79
80
# File 'lib/calabash-android/touch_helpers.rb', line 78

def pan_up(options={})
  pan("* id:'content'", :up, options)
end

#pinch(query_string, direction, options = {}) ⇒ Object



118
119
120
# File 'lib/calabash-android/touch_helpers.rb', line 118

def pinch(query_string, direction, options={})
  execute_gesture(Gesture.with_parameters(Gesture.pinch(direction, options), {query_string: query_string}.merge(options)))
end

#pinch_in(options = {}) ⇒ Object



114
115
116
# File 'lib/calabash-android/touch_helpers.rb', line 114

def pinch_in(options={})
  pinch("* id:'content'", :in, options)
end

#pinch_out(options = {}) ⇒ Object



110
111
112
# File 'lib/calabash-android/touch_helpers.rb', line 110

def pinch_out(options={})
  pinch("* id:'content'", :out, options)
end

#query_result?(uiquery) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
# File 'lib/calabash-android/touch_helpers.rb', line 160

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



140
141
142
143
144
145
146
147
148
# File 'lib/calabash-android/touch_helpers.rb', line 140

def tap_when_element_exists(query_string, options={})
  options.merge!({action: lambda {|q| touch(q, options)}})

  if options[:scroll] == true
    scroll_to(query_string, options)
  else
    when_element_exists(query_string, options)
  end
end

#touch(query_string, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/calabash-android/touch_helpers.rb', line 26

def touch(query_string, options={})
  if query_result?(query_string)
    center_x, center_y = find_coordinate(query_string, options)

    perform_action("touch_coordinate", center_x, center_y)
  else
    execute_gesture(Gesture.with_parameters(Gesture.tap(options), {query_string: query_string}.merge(options)))
  end
end