Module: Calabash::Cucumber::UIA

Included in:
Core, IOS7Operations, InstrumentsActions
Defined in:
lib/calabash-cucumber/uia.rb

Instance Method Summary collapse

Instance Method Details

#escape_uia_string(string) ⇒ Object



222
223
224
225
# File 'lib/calabash-cucumber/uia.rb', line 222

def escape_uia_string(string)
  #TODO escape '\n in query
  escape_quotes string
end

#send_uia_command(opts = {}) ⇒ Object

DEPRECATED: Use uia("...javascript..", options) instead. deprecated because the method signature is poor



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/calabash-cucumber/uia.rb', line 229

def send_uia_command(opts ={})

  # TODO formally deprecate send_uia_command with _deprecated function
  #cmd = opts[:command]
  #new_opts = cmd.select{|x| x != :command}
  #_deprecated('0.9.163',
  #            "use 'uia(#{cmd}, #{new_opts})' instead",
  #            :warn)

  uia(opts[:command], opts)
end

#uia(command, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/calabash-cucumber/uia.rb', line 8

def uia(command,options={})
  res = http({:method => :post, :path => 'uia'}, {command:command}.merge(options))
  res = JSON.parse(res)
  if res['outcome'] != 'SUCCESS'
    raise "uia action failed because: #{res['reason']}\n#{res['details']}"
  end
  res['results'].first
end

#uia_call(args_arr, *opts) ⇒ Object



152
153
154
# File 'lib/calabash-cucumber/uia.rb', line 152

def uia_call(args_arr, *opts)
  uia_call_method(:queryEl, args_arr, *opts)
end

#uia_call_method(cmd, args_arr, *opts) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/calabash-cucumber/uia.rb', line 156

def uia_call_method(cmd, args_arr, *opts)
  if opts.empty?
    return uia_handle_command(cmd, args_arr)
  end
  js_cmd = uia_serialize_command(cmd, args_arr)

  js_args = []
  opts.each do |invocation|
    js_args << case invocation
                 when Symbol
                   "#{invocation}()"
                 when Hash
                   m = invocation.keys.first
                   args = invocation[m]

                   if args.is_a?(Array)
                     serialized_args = (args.map &:to_json).join(',')
                   else
                     serialized_args = args.to_json
                   end


                   "#{m}(#{serialized_args})"
                 else
                   raise "Invalid invocation spec #{invocation}"              
    end
  end
  command = "#{js_cmd}.#{js_args.join('.')}"
  if ENV['DEBUG'] == '1'
    puts 'Sending UIA command'
    puts command
  end

  uia_result(uia(command))

end

#uia_double_tap(*queryparts) ⇒ Object



49
50
51
# File 'lib/calabash-cucumber/uia.rb', line 49

def uia_double_tap(*queryparts)
  uia_handle_command(:doubleTap, queryparts)
end

#uia_double_tap_mark(mark) ⇒ Object



53
54
55
# File 'lib/calabash-cucumber/uia.rb', line 53

def uia_double_tap_mark(mark)
  uia_double_tap(:view, marked:mark)
end

#uia_double_tap_offset(offset) ⇒ Object



57
58
59
# File 'lib/calabash-cucumber/uia.rb', line 57

def uia_double_tap_offset(offset)
  uia_handle_command(:doubleTapOffset, offset)
end

#uia_element_does_not_exist?(*queryparts) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/calabash-cucumber/uia.rb', line 113

def uia_element_does_not_exist?(*queryparts)
  uia_handle_command(:elementDoesNotExist, queryparts)
end

#uia_element_exists?(*queryparts) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/calabash-cucumber/uia.rb', line 109

def uia_element_exists?(*queryparts)
  uia_handle_command(:elementExists, queryparts)
end

#uia_enterObject



130
131
132
# File 'lib/calabash-cucumber/uia.rb', line 130

def uia_enter
  uia_handle_command(:enter)
end

#uia_flick_offset(from, to) ⇒ Object



69
70
71
# File 'lib/calabash-cucumber/uia.rb', line 69

def uia_flick_offset(from, to)
  uia_handle_command(:flickOffset, from, to)
end

#uia_handle_command(cmd, *query_args) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/calabash-cucumber/uia.rb', line 193

def uia_handle_command(cmd, *query_args)
  command = uia_serialize_command(cmd, *query_args)
  if ENV['DEBUG'] == '1'
    puts 'Sending UIA command'
    puts command
  end
  s = uia(command)
  uia_result(s)
end

#uia_names(*queryparts) ⇒ Object



32
33
34
35
# File 'lib/calabash-cucumber/uia.rb', line 32

def uia_names(*queryparts)
  #TODO escape '\n etc in query
  uia_handle_command(:names, queryparts)
end

#uia_pan(from_q, to_q) ⇒ Object



81
82
83
# File 'lib/calabash-cucumber/uia.rb', line 81

def uia_pan(from_q, to_q)
  uia_handle_command(:pan, from_q, to_q)
end

#uia_pan_offset(from, to, options) ⇒ Object



85
86
87
# File 'lib/calabash-cucumber/uia.rb', line 85

def uia_pan_offset(from, to, options)
  uia_handle_command(:panOffset, from, to, options)
end

#uia_pinch(*queryparts) ⇒ Object



97
98
99
# File 'lib/calabash-cucumber/uia.rb', line 97

def uia_pinch(*queryparts)
  uia_handle_command(:pinch, queryparts)
end

#uia_pinch_offset(in_or_out, offset, duration) ⇒ Object



101
102
103
# File 'lib/calabash-cucumber/uia.rb', line 101

def uia_pinch_offset(in_or_out, offset, duration)
  uia_handle_command(:pinchOffset, in_or_out, offset, duration)
end

#uia_query(*queryparts) ⇒ Object



17
18
19
20
# File 'lib/calabash-cucumber/uia.rb', line 17

def uia_query(*queryparts)
  #TODO escape '\n etc in query
  uia_handle_command(:query, queryparts)
end

#uia_query_el(*queryparts) ⇒ Object



22
23
24
25
# File 'lib/calabash-cucumber/uia.rb', line 22

def uia_query_el(*queryparts)
  #TODO escape '\n etc in query
  uia_handle_command(:queryEl, queryparts)
end

#uia_query_windows(*queryparts) ⇒ Object



27
28
29
30
# File 'lib/calabash-cucumber/uia.rb', line 27

def uia_query_windows(*queryparts)
  #TODO escape '\n etc in query
  uia_handle_command(:queryWindows, queryparts)
end

#uia_screenshot(name) ⇒ Object



117
118
119
# File 'lib/calabash-cucumber/uia.rb', line 117

def uia_screenshot(name)
  uia_handle_command(:screenshot, name)
end

#uia_scroll_to(*queryparts) ⇒ Object



105
106
107
# File 'lib/calabash-cucumber/uia.rb', line 105

def uia_scroll_to(*queryparts)
  uia_handle_command(:scrollTo, queryparts)
end

#uia_send_app_to_background(secs) ⇒ Object



148
149
150
# File 'lib/calabash-cucumber/uia.rb', line 148

def uia_send_app_to_background(secs)
  uia_handle_command(:deactivate, secs)
end

#uia_serialize_argument(part) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/calabash-cucumber/uia.rb', line 214

def uia_serialize_argument(part)
  if part.is_a?(String)
    "'#{escape_uia_string(part)}'"
  else
    "'#{escape_uia_string(part.to_edn)}'"
  end
end

#uia_serialize_arguments(args) ⇒ Object



208
209
210
211
212
# File 'lib/calabash-cucumber/uia.rb', line 208

def uia_serialize_arguments(args)
  args.map do |part|
    uia_serialize_argument(part)
  end
end

#uia_serialize_command(cmd, *query_args) ⇒ Object



203
204
205
206
# File 'lib/calabash-cucumber/uia.rb', line 203

def uia_serialize_command(cmd, *query_args)
  args = uia_serialize_arguments(query_args)
  %Q[uia.#{cmd}(#{args.join(', ')})]
end

#uia_set_location(options) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/calabash-cucumber/uia.rb', line 134

def uia_set_location(options)
  validate_hash_is_location!(options)
  if options[:place]
    place = options[:place]
    search_results = Geocoder.search(place)
    raise "Got no results for #{place}" if search_results.empty?
    loc = search_results.first
    loc_data = {'latitude'=>loc.latitude, 'longitude'=>loc.longitude}
  elsif options.is_a?(Hash)
    loc_data = options
  end
  uia_handle_command(:setLocation, loc_data)
end

#uia_swipe(*queryparts) ⇒ Object



89
90
91
# File 'lib/calabash-cucumber/uia.rb', line 89

def uia_swipe(*queryparts)
  uia_handle_command(:swipe, queryparts)
end

#uia_swipe_offset(offset, options) ⇒ Object



93
94
95
# File 'lib/calabash-cucumber/uia.rb', line 93

def uia_swipe_offset(offset, options)
  uia_handle_command(:swipeOffset, offset, options)
end

#uia_tap(*queryparts) ⇒ Object



37
38
39
# File 'lib/calabash-cucumber/uia.rb', line 37

def uia_tap(*queryparts)
  uia_handle_command(:tap, queryparts)
end

#uia_tap_mark(mark) ⇒ Object



41
42
43
# File 'lib/calabash-cucumber/uia.rb', line 41

def uia_tap_mark(mark)
  uia_handle_command(:tapMark, mark)
end

#uia_tap_offset(offset) ⇒ Object



45
46
47
# File 'lib/calabash-cucumber/uia.rb', line 45

def uia_tap_offset(offset)
  uia_handle_command(:tapOffset, offset)
end

#uia_touch_hold(duration, *queryparts) ⇒ Object



73
74
75
# File 'lib/calabash-cucumber/uia.rb', line 73

def uia_touch_hold(duration, *queryparts)
  uia_handle_command(:touchHold, duration, queryparts)
end

#uia_touch_hold_offset(duration, offset) ⇒ Object



77
78
79
# File 'lib/calabash-cucumber/uia.rb', line 77

def uia_touch_hold_offset(duration, offset)
  uia_handle_command(:touchHoldOffset, duration, offset)
end

#uia_two_finger_tap(*queryparts) ⇒ Object



61
62
63
# File 'lib/calabash-cucumber/uia.rb', line 61

def uia_two_finger_tap(*queryparts)
  uia_handle_command(:twoFingerTap, queryparts)
end

#uia_two_finger_tap_offset(offset) ⇒ Object



65
66
67
# File 'lib/calabash-cucumber/uia.rb', line 65

def uia_two_finger_tap_offset(offset)
  uia_handle_command(:twoFingerTapOffset, offset)
end

#uia_type_string(string) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/calabash-cucumber/uia.rb', line 121

def uia_type_string(string)
  res = uia_handle_command(:typeString, string)
  status = res['status']
  if status.eql?('error')
    value = res['value']
    raise "could not type '#{string}' - '#{value}'"
  end
end