@@ -22,6 +22,8 @@ module WebDriver
22
22
module Remote
23
23
class Bridge
24
24
autoload :COMMANDS , 'selenium/webdriver/remote/bridge/commands'
25
+ autoload :LocatorConverter , 'selenium/webdriver/remote/bridge/locator_converter'
26
+
25
27
include Atoms
26
28
27
29
PORT = 4444
@@ -31,12 +33,17 @@ class Bridge
31
33
32
34
class << self
33
35
attr_accessor :extra_commands
36
+ attr_writer :locator_converter
34
37
35
38
def add_command ( name , verb , url , &block )
36
39
@extra_commands ||= { }
37
40
@extra_commands [ name ] = [ verb , url ]
38
41
define_method ( name , &block )
39
42
end
43
+
44
+ def locator_converter
45
+ @locator_converter ||= LocatorConverter . new
46
+ end
40
47
end
41
48
42
49
#
@@ -53,6 +60,8 @@ def initialize(url:, http_client: nil)
53
60
@http = http_client || Http ::Default . new
54
61
@http . server_url = uri
55
62
@file_detector = nil
63
+
64
+ @locator_converter = self . class . locator_converter
56
65
end
57
66
58
67
#
@@ -533,7 +542,7 @@ def active_element
533
542
alias switch_to_active_element active_element
534
543
535
544
def find_element_by ( how , what , parent_ref = [ ] )
536
- how , what = convert_locator ( how , what )
545
+ how , what = @locator_converter . convert ( how , what )
537
546
538
547
return execute_atom ( :findElements , Support ::RelativeLocator . new ( what ) . as_json ) . first if how == 'relative'
539
548
@@ -551,7 +560,7 @@ def find_element_by(how, what, parent_ref = [])
551
560
end
552
561
553
562
def find_elements_by ( how , what , parent_ref = [ ] )
554
- how , what = convert_locator ( how , what )
563
+ how , what = @locator_converter . convert ( how , what )
555
564
556
565
return execute_atom :findElements , Support ::RelativeLocator . new ( what ) . as_json if how == 'relative'
557
566
@@ -668,42 +677,6 @@ def prepare_capabilities_payload(capabilities)
668
677
{ capabilities : capabilities }
669
678
end
670
679
671
- def convert_locator ( how , what )
672
- how = SearchContext ::FINDERS [ how . to_sym ] || how
673
-
674
- case how
675
- when 'class name'
676
- how = 'css selector'
677
- what = ".#{ escape_css ( what . to_s ) } "
678
- when 'id'
679
- how = 'css selector'
680
- what = "##{ escape_css ( what . to_s ) } "
681
- when 'name'
682
- how = 'css selector'
683
- what = "*[name='#{ escape_css ( what . to_s ) } ']"
684
- end
685
-
686
- if what . is_a? ( Hash )
687
- what = what . each_with_object ( { } ) do |( h , w ) , hash |
688
- h , w = convert_locator ( h . to_s , w )
689
- hash [ h ] = w
690
- end
691
- end
692
-
693
- [ how , what ]
694
- end
695
-
696
- ESCAPE_CSS_REGEXP = /(['"\\ #.:;,!?+<>=~*^$|%&@`{}\- \[ \] ()])/
697
- UNICODE_CODE_POINT = 30
698
-
699
- # Escapes invalid characters in CSS selector.
700
- # @see https://mathiasbynens.be/notes/css-escapes
701
- def escape_css ( string )
702
- string = string . gsub ( ESCAPE_CSS_REGEXP ) { |match | "\\ #{ match } " }
703
- string = "\\ #{ UNICODE_CODE_POINT + Integer ( string [ 0 ] ) } #{ string [ 1 ..] } " if string [ 0 ] &.match? ( /[[:digit:]]/ )
704
-
705
- string
706
- end
707
680
end # Bridge
708
681
end # Remote
709
682
end # WebDriver
0 commit comments