Skip to content

Commit fcb64b7

Browse files
authored
add keyevent (#27)
* add keyevent * fix rubocop * update changelog
1 parent a54b315 commit fcb64b7

File tree

7 files changed

+83
-20
lines changed

7 files changed

+83
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [Unreleased]
55
### Enhancements
66
- Add guidelines in `.github`
7+
- session/:session_id/appium/device/keyevent [#21](https://github.com/appium/ruby_lib_core/issues/21)
78

89
### Bug fixes
910

lib/appium_lib_core/common/command.rb

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module Commands
4343
hide_keyboard: [:post, 'session/:session_id/appium/device/hide_keyboard'.freeze],
4444
press_keycode: [:post, 'session/:session_id/appium/device/press_keycode'.freeze],
4545
long_press_keycode: [:post, 'session/:session_id/appium/device/long_press_keycode'.freeze],
46+
# keyevent is only for Selendroid
47+
keyevent: [:post, 'session/:session_id/appium/device/keyevent'.freeze],
4648
set_immediate_value: [:post, 'session/:session_id/appium/element/:id/value'.freeze],
4749
replace_value: [:post, 'session/:session_id/appium/element/:id/replace_value'.freeze],
4850
push_file: [:post, 'session/:session_id/appium/device/push_file'.freeze],

lib/appium_lib_core/common/device.rb

+39-16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ module Device
138138
# @driver.hide_keyboard(nil, :tapOutside) # Close a keyboard with tapping out side of keyboard
139139
#
140140

141+
# @!method keyevent(key, metastate = nil)
142+
# Send keyevent on the device.(Only for Selendroid)
143+
# http://developer.android.com/reference/android/view/KeyEvent.html
144+
# @param [integer] key The key to press.
145+
# @param [String] metastate The state the metakeys should be in when pressing the key.
146+
#
147+
# @example
148+
#
149+
# @driver.keyevent 82
150+
#
151+
141152
# @!method press_keycode(key, metastate = nil)
142153
# Press keycode on the device.
143154
# http://developer.android.com/reference/android/view/KeyEvent.html
@@ -391,22 +402,6 @@ def hide_keyboard(close_key = nil, strategy = nil)
391402
end
392403
end
393404

394-
add_endpoint_method(:press_keycode) do
395-
def press_keycode(key, metastate = nil)
396-
args = { keycode: key }
397-
args[:metastate] = metastate if metastate
398-
execute :press_keycode, {}, args
399-
end
400-
end
401-
402-
add_endpoint_method(:long_press_keycode) do
403-
def long_press_keycode(key, metastate = nil)
404-
args = { keycode: key }
405-
args[:metastate] = metastate if metastate
406-
execute :long_press_keycode, {}, args
407-
end
408-
end
409-
410405
add_endpoint_method(:set_immediate_value) do
411406
def set_immediate_value(element, *value)
412407
keys = ::Selenium::WebDriver::Keys.encode(value)
@@ -454,6 +449,7 @@ def update_settings(settings)
454449
end
455450
end
456451

452+
add_keyevent
457453
add_touch_actions
458454
add_ime_actions
459455
add_handling_context
@@ -498,6 +494,33 @@ def create_bridge_command(method)
498494
end
499495
end
500496

497+
def add_keyevent
498+
# Only for Selendroid
499+
add_endpoint_method(:keyevent) do
500+
def keyevent(key, metastate = nil)
501+
args = { keycode: key }
502+
args[:metastate] = metastate if metastate
503+
execute :keyevent, {}, args
504+
end
505+
end
506+
507+
add_endpoint_method(:press_keycode) do
508+
def press_keycode(key, metastate = nil)
509+
args = { keycode: key }
510+
args[:metastate] = metastate if metastate
511+
execute :press_keycode, {}, args
512+
end
513+
end
514+
515+
add_endpoint_method(:long_press_keycode) do
516+
def long_press_keycode(key, metastate = nil)
517+
args = { keycode: key }
518+
args[:metastate] = metastate if metastate
519+
execute :long_press_keycode, {}, args
520+
end
521+
end
522+
end
523+
501524
def add_touch_actions
502525
add_endpoint_method(:touch_actions) do
503526
def touch_actions(actions)

test/functional/android/android/device_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ def test_get_display_density
206206
assert(@@driver.get_display_density > 0)
207207
end
208208

209+
def test_keyevent
210+
skip('Because only for Selendroid')
211+
# http://developer.android.com/reference/android/view/KeyEvent.html
212+
assert @@driver.keyevent(176)
213+
end
214+
209215
def test_press_keycode
210216
# http://developer.android.com/reference/android/view/KeyEvent.html
211217
assert @@driver.press_keycode(176)

test/unit/android/device_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_with_arg_definitions
4848
:app_installed?,
4949
:background_app,
5050
:hide_keyboard,
51+
:keyevent,
5152
:press_keycode,
5253
:long_press_keycode,
5354
:set_immediate_value,
@@ -321,6 +322,17 @@ def test_hide_keyboard
321322
assert_requested(:post, "#{SESSION}/appium/device/hide_keyboard", times: 1)
322323
end
323324

325+
def test_keyevent
326+
skip('Because only for Selendroid')
327+
328+
stub_request(:post, "#{SESSION}/appium/device/keyevent")
329+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
330+
331+
@driver.keyevent 86
332+
333+
assert_requested(:post, "#{SESSION}/appium/device/keyevent", times: 1)
334+
end
335+
324336
def test_press_keycode
325337
stub_request(:post, "#{SESSION}/appium/device/press_keycode")
326338
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)

test/unit/ios/device_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_with_arg_definitions
3939
:app_installed?,
4040
:background_app,
4141
:hide_keyboard,
42+
:keyevent,
4243
:press_keycode,
4344
:long_press_keycode,
4445
:set_immediate_value,

test/unit/script/commands_test.rb

+22-4
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,43 @@ def test_get_all_command_path
2121
end
2222

2323
# depends on webdriver-version... (number of commands)
24-
def test_appium_commands
25-
assert_equal 127, @c.implemented_mjsonwp_commands.length
24+
def test_implemented_mjsonwp_commands
25+
assert_equal 128, @c.implemented_mjsonwp_commands.length
2626
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_mjsonwp_commands.first
27+
28+
# pick up an arbitrary command
2729
assert_equal %i(get post), @c.implemented_mjsonwp_commands['session/:session_id/alert_text']
30+
end
2831

29-
assert_equal 88, @c.implemented_w3c_commands.length
32+
def test_implemented_w3c_commands
33+
assert_equal 89, @c.implemented_w3c_commands.length
3034
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_w3c_commands.first
35+
36+
# pick up an arbitrary command
3137
assert_equal %i(get post), @c.implemented_w3c_commands['session/:session_id/alert/text']
38+
end
3239

33-
assert_equal 42, @c.implemented_core_commands.length
40+
def test_implemented_core_commands
41+
assert_equal 43, @c.implemented_core_commands.length
3442
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_core_commands.first
43+
44+
# pick up an arbitrary command
3545
assert_equal [:post], @c.implemented_core_commands['session/:session_id/appium/device/pull_folder']
46+
end
3647

48+
def test_webdriver_oss_commands
3749
assert_equal 86, @c.webdriver_oss_commands.length
3850
assert_equal ['session/:session_id', %i(get delete)], @c.webdriver_oss_commands.first
51+
52+
# pick up an arbitrary command
3953
assert_equal %i(get post), @c.webdriver_oss_commands['session/:session_id/alert_text']
54+
end
4055

56+
def test_webdriver_w3c_commands
4157
assert_equal 46, @c.webdriver_w3c_commands.length
4258
assert_equal ['session', [:post]], @c.webdriver_w3c_commands.first
59+
60+
# pick up an arbitrary command
4361
assert_equal %i(get post), @c.webdriver_w3c_commands['session/:session_id/alert/text']
4462
end
4563

0 commit comments

Comments
 (0)