Skip to content

Commit

Permalink
feat: Add chrome devtools endpoints (#260)
Browse files Browse the repository at this point in the history
* add commands for chrome from selenium/chrome

* remove other commands

* update changelog

* add a comment

* add docstring
  • Loading branch information
KazuCocoa authored Apr 18, 2020
1 parent 73cf85f commit e561c8d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Read `release_notes.md` for commit level details.
### Enhancements
- Add `x-idempotency-key` header support (https://github.com/appium/appium-base-driver/pull/400)
- Can disable the header with `enable_idempotency_header: false` in `appium_lib` capability. Defaults to `true`.
- Add chrome devtools entpoint which is available chrome module in Selenium Ruby binding

### Bug fixes

Expand Down
19 changes: 19 additions & 0 deletions lib/appium_lib_core/android/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,19 @@ module Device
# @driver.finger_print 1
#

# @!method execute_cdp(cmd, params)
# Execute Chrome Devtools protocol commands
# https://chromedevtools.github.io/devtools-protocol
#
# @param [String] cmd The name of command
# @param [Hash] params The parameter for the command as hash.
#
# @example
#
# @driver.execute_cdp 'Page.captureScreenshot', { quality: 50, format: 'jpeg' }
# @driver.execute_cdp 'Page.getResourceTree'
#

####
## class << self
####
Expand Down Expand Up @@ -393,6 +406,12 @@ def end_coverage(path, intent)
end
end

::Appium::Core::Device.add_endpoint_method(:execute_cdp) do
def execute_cdp(cmd, **params)
execute :chrome_send_command, {}, { cmd: cmd, params: params }
end
end

Screen.add_methods
Performance.add_methods
Network.add_methods
Expand Down
5 changes: 4 additions & 1 deletion lib/appium_lib_core/common/command/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ module Commands
gsm_voice: [:post, 'session/:session_id/appium/device/gsm_voice'],
set_network_speed: [:post, 'session/:session_id/appium/device/network_speed'],
set_power_capacity: [:post, 'session/:session_id/appium/device/power_capacity'],
set_power_ac: [:post, 'session/:session_id/appium/device/power_ac']
set_power_ac: [:post, 'session/:session_id/appium/device/power_ac'],

# For chromium: https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/server/http_handler.cc
chrome_send_command: [:post, 'session/:session_id/goog/cdp/execute']
}.freeze

COMMAND_IOS = {
Expand Down
2 changes: 1 addition & 1 deletion lib/appium_lib_core/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def extended(_mod)
:stop_recording_screen, :stop_and_save_recording_screen,
:shake, :device_time,
:touch_actions, :multi_touch,
:execute_driver
:execute_driver, :execute_cdp
].each(&method(:delegate_from_appium_driver))
end

Expand Down
3 changes: 2 additions & 1 deletion test/unit/android/device/mjsonwp/definition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def test_with_arg_definitions
:get_performance_data,
:get_clipboard,
:set_clipboard,
:execute_driver])
:execute_driver,
:execute_cdp])
end
end
end # module MJSONWP
Expand Down
22 changes: 22 additions & 0 deletions test/unit/android/device/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,28 @@ def test_search_element_child_element
assert_requested(:post, "#{SESSION}/element", times: 1)
assert_requested(:post, "#{SESSION}/element/element_id_parent/element", times: 1)
end

def test_chromium_send_command
stub_request(:post, "#{SESSION}/goog/cdp/execute")
.with(body: { cmd: 'Page.captureScreenshot', params: { quality: 1, format: 'jpeg' } }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { data: '/9j/4AAQSkZJRgABAQAAAQABAAD' } }.to_json)

r = @driver.execute_cdp 'Page.captureScreenshot', { quality: 1, format: 'jpeg' }

assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
assert_equal '/9j/4AAQSkZJRgABAQAAAQABAAD', r['data']
end

def test_chromium_send_command_no_param
stub_request(:post, "#{SESSION}/goog/cdp/execute")
.with(body: { cmd: 'Page.getResourceTree', params: {} }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { frameTree: { childFrames: [] } } }.to_json)

r = @driver.execute_cdp 'Page.getResourceTree'

assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
assert_equal({ 'childFrames' => [] }, r['frameTree'])
end
end # class CommandsTest
end # module W3C
end # module Device
Expand Down
3 changes: 2 additions & 1 deletion test/unit/android/device/w3c/definition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def test_with_arg_definitions
:get_performance_data,
:get_clipboard,
:set_clipboard,
:execute_driver])
:execute_driver,
:execute_cdp])
end
end # class DefinitionTest
end # module W3C
Expand Down

0 comments on commit e561c8d

Please sign in to comment.