15
15
module Appium
16
16
module Core
17
17
class Base
18
+ class LocatorConverter
19
+ def convert ( how , what )
20
+ [ how , what ]
21
+ end
22
+ end # LocatorConverter
23
+
18
24
class Bridge < ::Selenium ::WebDriver ::Remote ::Bridge
19
25
include Device ::DeviceLock
20
26
include Device ::Keyboard
@@ -31,6 +37,8 @@ class Bridge < ::Selenium::WebDriver::Remote::Bridge
31
37
include Device ::ExecuteDriver
32
38
include Device ::Orientation
33
39
40
+ Bridge . locator_converter = LocatorConverter . new
41
+
34
42
# Prefix for extra capability defined by W3C
35
43
APPIUM_PREFIX = 'appium:'
36
44
@@ -153,6 +161,18 @@ def json_create(value)
153
161
public
154
162
155
163
# command for Appium 2.0.
164
+
165
+ # Example:
166
+ # driver.add_command(name: :available_contexts, method: :get, url: 'session/:session_id/contexts') do
167
+ # execute(:available_contexts, {}) || []
168
+ # end
169
+ # Then,
170
+ # driver.available_contexts #=> ["NATIVE_APP"]
171
+
172
+ # def add_command(method:, url:, name:, &block)
173
+ # Bridge.add_command name, method, url, &block
174
+ # end
175
+
156
176
def add_command ( method :, url :, name :, &block )
157
177
::Appium ::Logger . info "Overriding the method '#{ name } ' for '#{ url } '" if @available_commands . key? name
158
178
@@ -162,7 +182,7 @@ def add_command(method:, url:, name:, &block)
162
182
end
163
183
164
184
def commands ( command )
165
- @available_commands [ command ]
185
+ @available_commands [ command ] || Bridge . extra_commands [ command ]
166
186
end
167
187
168
188
def status
@@ -216,52 +236,8 @@ def element_attribute(element, name)
216
236
end
217
237
218
238
# For Appium
219
- # override
220
- def active_element
221
- ::Appium ::Core ::Element . new self , element_id_from ( execute ( :get_active_element ) )
222
- end
223
239
alias switch_to_active_element active_element
224
240
225
- # For Appium
226
- # override
227
- def find_element_by ( how , what , parent_ref = [ ] )
228
- how , what = convert_locator ( how , what )
229
-
230
- return execute_atom ( :findElements , Support ::RelativeLocator . new ( what ) . as_json ) . first if how == 'relative'
231
-
232
- parent_type , parent_id = parent_ref
233
- id = case parent_type
234
- when :element
235
- execute :find_child_element , { id : parent_id } , { using : how , value : what . to_s }
236
- when :shadow_root
237
- execute :find_shadow_child_element , { id : parent_id } , { using : how , value : what . to_s }
238
- else
239
- execute :find_element , { } , { using : how , value : what . to_s }
240
- end
241
-
242
- ::Appium ::Core ::Element . new self , element_id_from ( id )
243
- end
244
-
245
- # For Appium
246
- # override
247
- def find_elements_by ( how , what , parent_ref = [ ] )
248
- how , what = convert_locator ( how , what )
249
-
250
- return execute_atom :findElements , Support ::RelativeLocator . new ( what ) . as_json if how == 'relative'
251
-
252
- parent_type , parent_id = parent_ref
253
- ids = case parent_type
254
- when :element
255
- execute :find_child_elements , { id : parent_id } , { using : how , value : what . to_s }
256
- when :shadow_root
257
- execute :find_shadow_child_elements , { id : parent_id } , { using : how , value : what . to_s }
258
- else
259
- execute :find_elements , { } , { using : how , value : what . to_s }
260
- end
261
-
262
- ids . map { |id | ::Appium ::Core ::Element . new self , element_id_from ( id ) }
263
- end
264
-
265
241
# For Appium
266
242
# @param [Hash] id The id which can get as a response from server
267
243
# @return [::Appium::Core::Element]
@@ -370,36 +346,6 @@ def unwrap_script_result(arg)
370
346
arg
371
347
end
372
348
end
373
-
374
- def element_id_from ( id )
375
- id [ 'ELEMENT' ] || id [ 'element-6066-11e4-a52e-4f735466cecf' ]
376
- end
377
-
378
- # Don't convert locators for Appium in native context
379
- def convert_locator ( how , what )
380
- # case how
381
- # when 'class name'
382
- # how = 'css selector'
383
- # what = ".#{escape_css(what)}"
384
- # when 'id'
385
- # how = 'css selector'
386
- # what = "##{escape_css(what)}"
387
- # when 'name'
388
- # how = 'css selector'
389
- # what = "*[name='#{escape_css(what)}']"
390
- # when 'tag name'
391
- # how = 'css selector'
392
- # end
393
- #
394
- # if what.is_a?(Hash)
395
- # what = what.each_with_object({}) do |(h, w), hash|
396
- # h, w = convert_locator(h.to_s, w)
397
- # hash[h] = w
398
- # end
399
- # end
400
-
401
- [ how , what ]
402
- end
403
349
end # class Bridge
404
350
end # class Base
405
351
end # module Core
0 commit comments