Skip to content

Commit e561c8d

Browse files
authored
feat: Add chrome devtools endpoints (#260)
* add commands for chrome from selenium/chrome * remove other commands * update changelog * add a comment * add docstring
1 parent 73cf85f commit e561c8d

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Read `release_notes.md` for commit level details.
77
### Enhancements
88
- Add `x-idempotency-key` header support (https://github.com/appium/appium-base-driver/pull/400)
99
- Can disable the header with `enable_idempotency_header: false` in `appium_lib` capability. Defaults to `true`.
10+
- Add chrome devtools entpoint which is available chrome module in Selenium Ruby binding
1011

1112
### Bug fixes
1213

lib/appium_lib_core/android/device.rb

+19
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@ module Device
285285
# @driver.finger_print 1
286286
#
287287

288+
# @!method execute_cdp(cmd, params)
289+
# Execute Chrome Devtools protocol commands
290+
# https://chromedevtools.github.io/devtools-protocol
291+
#
292+
# @param [String] cmd The name of command
293+
# @param [Hash] params The parameter for the command as hash.
294+
#
295+
# @example
296+
#
297+
# @driver.execute_cdp 'Page.captureScreenshot', { quality: 50, format: 'jpeg' }
298+
# @driver.execute_cdp 'Page.getResourceTree'
299+
#
300+
288301
####
289302
## class << self
290303
####
@@ -393,6 +406,12 @@ def end_coverage(path, intent)
393406
end
394407
end
395408

409+
::Appium::Core::Device.add_endpoint_method(:execute_cdp) do
410+
def execute_cdp(cmd, **params)
411+
execute :chrome_send_command, {}, { cmd: cmd, params: params }
412+
end
413+
end
414+
396415
Screen.add_methods
397416
Performance.add_methods
398417
Network.add_methods

lib/appium_lib_core/common/command/common.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ module Commands
9494
gsm_voice: [:post, 'session/:session_id/appium/device/gsm_voice'],
9595
set_network_speed: [:post, 'session/:session_id/appium/device/network_speed'],
9696
set_power_capacity: [:post, 'session/:session_id/appium/device/power_capacity'],
97-
set_power_ac: [:post, 'session/:session_id/appium/device/power_ac']
97+
set_power_ac: [:post, 'session/:session_id/appium/device/power_ac'],
98+
99+
# For chromium: https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/server/http_handler.cc
100+
chrome_send_command: [:post, 'session/:session_id/goog/cdp/execute']
98101
}.freeze
99102

100103
COMMAND_IOS = {

lib/appium_lib_core/device.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def extended(_mod)
4040
:stop_recording_screen, :stop_and_save_recording_screen,
4141
:shake, :device_time,
4242
:touch_actions, :multi_touch,
43-
:execute_driver
43+
:execute_driver, :execute_cdp
4444
].each(&method(:delegate_from_appium_driver))
4545
end
4646

test/unit/android/device/mjsonwp/definition_test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def test_with_arg_definitions
9292
:get_performance_data,
9393
:get_clipboard,
9494
:set_clipboard,
95-
:execute_driver])
95+
:execute_driver,
96+
:execute_cdp])
9697
end
9798
end
9899
end # module MJSONWP

test/unit/android/device/w3c/commands_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,28 @@ def test_search_element_child_element
480480
assert_requested(:post, "#{SESSION}/element", times: 1)
481481
assert_requested(:post, "#{SESSION}/element/element_id_parent/element", times: 1)
482482
end
483+
484+
def test_chromium_send_command
485+
stub_request(:post, "#{SESSION}/goog/cdp/execute")
486+
.with(body: { cmd: 'Page.captureScreenshot', params: { quality: 1, format: 'jpeg' } }.to_json)
487+
.to_return(headers: HEADER, status: 200, body: { value: { data: '/9j/4AAQSkZJRgABAQAAAQABAAD' } }.to_json)
488+
489+
r = @driver.execute_cdp 'Page.captureScreenshot', { quality: 1, format: 'jpeg' }
490+
491+
assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
492+
assert_equal '/9j/4AAQSkZJRgABAQAAAQABAAD', r['data']
493+
end
494+
495+
def test_chromium_send_command_no_param
496+
stub_request(:post, "#{SESSION}/goog/cdp/execute")
497+
.with(body: { cmd: 'Page.getResourceTree', params: {} }.to_json)
498+
.to_return(headers: HEADER, status: 200, body: { value: { frameTree: { childFrames: [] } } }.to_json)
499+
500+
r = @driver.execute_cdp 'Page.getResourceTree'
501+
502+
assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
503+
assert_equal({ 'childFrames' => [] }, r['frameTree'])
504+
end
483505
end # class CommandsTest
484506
end # module W3C
485507
end # module Device

test/unit/android/device/w3c/definition_test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def test_with_arg_definitions
9292
:get_performance_data,
9393
:get_clipboard,
9494
:set_clipboard,
95-
:execute_driver])
95+
:execute_driver,
96+
:execute_cdp])
9697
end
9798
end # class DefinitionTest
9899
end # module W3C

0 commit comments

Comments
 (0)