@@ -31,14 +31,14 @@ def initialize(page)
3131 end
3232
3333 def down ( key )
34- key = normalize_keys ( Array ( key ) )
34+ key = normalize_keys ( Array ( key ) ) . first
3535 type = key [ :text ] ? "keyDown" : "rawKeyDown"
3636 @page . command ( "Input.dispatchKeyEvent" , slowmoable : true , type : type , **key )
3737 self
3838 end
3939
4040 def up ( key )
41- key = normalize_keys ( Array ( key ) )
41+ key = normalize_keys ( Array ( key ) ) . first
4242 @page . command ( "Input.dispatchKeyEvent" , slowmoable : true , type : "keyUp" , **key )
4343 self
4444 end
@@ -61,9 +61,14 @@ def modifiers(keys)
6161
6262 private
6363
64+ # TODO: Refactor it, and try to simplify complexity
65+ # rubocop:disable Metrics/PerceivedComplexity
66+ # rubocop:disable Metrics/CyclomaticComplexity
6467 def normalize_keys ( keys , pressed_keys = [ ] , memo = [ ] )
6568 case keys
6669 when Array
70+ raise ArgumentError , "empty keys passed" if keys . empty?
71+
6772 pressed_keys . push ( [ ] )
6873 memo += combine_strings ( keys ) . map do |key |
6974 normalize_keys ( key , pressed_keys , memo )
@@ -82,6 +87,8 @@ def normalize_keys(keys, pressed_keys = [], memo = [])
8287 to_options ( key )
8388 end
8489 when String
90+ raise ArgumentError , "empty keys passed" if keys . empty?
91+
8592 pressed = pressed_keys . flatten
8693 keys . each_char . map do |char |
8794 key = KEYS [ char ] || { }
@@ -102,8 +109,12 @@ def normalize_keys(keys, pressed_keys = [], memo = [])
102109 modifiers + [ to_options ( key ) ]
103110 end . flatten
104111 end
112+ else
113+ raise ArgumentError , "unexpected argument"
105114 end
106115 end
116+ # rubocop:enable Metrics/PerceivedComplexity
117+ # rubocop:enable Metrics/CyclomaticComplexity
107118
108119 def combine_strings ( keys )
109120 keys
0 commit comments