Skip to content

Commit

Permalink
Prepare infrastructure for CDP in Ruby
Browse files Browse the repository at this point in the history
Adds WebSocket support to allow using CDP from Ruby bindings. The
implementation is naive: it simply processes handshake and then writes
to the connection socket and reads from it.

   driver = Selenium::WebDriver.for(:chrome)
   driver.devtools.send('Page.navigate', {url: 'https://google.com'})

There is no built-in CDP commands/types/events support. These will be
added later.

The code relies on websocket gem as it's the only dependency-free
implementation.
  • Loading branch information
p0deje committed Dec 23, 2019
1 parent 7ab5f1b commit d61cecb
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rb/lib/selenium/webdriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'date'
require 'json'
require 'set'
require 'websocket'

require 'selenium/webdriver/common'
require 'selenium/webdriver/atoms'
Expand All @@ -36,6 +37,7 @@ module WebDriver
Location = Struct.new(:latitude, :longitude, :altitude)

autoload :Chrome, 'selenium/webdriver/chrome'
autoload :DevTools, 'selenium/webdriver/devtools'
autoload :Edge, 'selenium/webdriver/edge'
autoload :EdgeHtml, 'selenium/webdriver/edge'
autoload :EdgeChrome, 'selenium/webdriver/edge'
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::HasLocation
include DriverExtensions::TakesScreenshot
include DriverExtensions::DownloadsFiles
include DriverExtensions::HasDevTools

def browser
:chrome
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
require 'selenium/webdriver/common/driver_extensions/has_debugger'
require 'selenium/webdriver/common/driver_extensions/uploads_files'
require 'selenium/webdriver/common/driver_extensions/has_addons'
require 'selenium/webdriver/common/driver_extensions/has_devtools'
require 'selenium/webdriver/common/keys'
require 'selenium/webdriver/common/profile_helper'
require 'selenium/webdriver/common/options'
Expand Down
38 changes: 38 additions & 0 deletions rb/lib/selenium/webdriver/common/driver_extensions/has_devtools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
module DriverExtensions
module HasDevTools

#
# Retrieves connection to DevTools.
#
# @return [DevTools]
#

def devtools
@devtools ||= DevTools.new(capabilities['goog:chromeOptions']['debuggerAddress'])
end

end # HasDevTools
end # DriverExtensions
end # WebDriver
end # Selenium
70 changes: 70 additions & 0 deletions rb/lib/selenium/webdriver/devtools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class DevTools

def initialize(url)
@uri = URI("http://#{url}")
process_handshake
end

def send(method, **params)
data = JSON.generate(id: next_id, method: method, params: params)

out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text')
socket.write(out_frame.to_s)

in_frame = WebSocket::Frame::Incoming::Client.new(version: ws.version)
in_frame << socket.readpartial(4096)
JSON.parse(in_frame.next.to_s)
end

private

def next_id
@id ||= 0
@id += 1
end

def process_handshake
socket.write(ws.to_s)
ws << socket.readpartial(1024)
end

def socket
@socket ||= TCPSocket.new(ws.host, ws.port)
end

def ws
@ws ||= WebSocket::Handshake::Client.new(url: ws_url)
end

def ws_url
@ws_url ||= begin
urls = JSON.parse(Net::HTTP.get(@uri.hostname, '/json', @uri.port))
page = urls.find { |u| u['type'] == 'page' }
page['webSocketDebuggerUrl']
end
end

end # DevTools
end # WebDriver
end # Selenium
1 change: 1 addition & 0 deletions rb/selenium-webdriver.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'childprocess', ['>= 0.5', '< 4.0']
s.add_runtime_dependency 'rubyzip', ['>= 1.2.2']
s.add_runtime_dependency 'websocket', ['~> 1.0']

# childprocess requires ffi on windows but doesn't declare it in its dependencies
s.add_development_dependency 'ffi'
Expand Down
Binary file added third_party/rb/vendor/cache/websocket-1.2.8.gem
Binary file not shown.

0 comments on commit d61cecb

Please sign in to comment.