Skip to content

Commit

Permalink
[rb] Support registering extra bridge commands
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed May 15, 2024
1 parent 3ec3cef commit 4cf9aeb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rb/lib/selenium/webdriver/remote/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class Bridge
attr_accessor :http, :file_detector
attr_reader :capabilities

class << self
attr_reader :extra_commands

def add_command(name, verb, url, &block)
@extra_commands ||= {}
@extra_commands[name] = [verb, url]
define_method(name, &block)
end
end

#
# Initializes the bridge with the given server URL
# @param [String, URI] url url for the remote server
Expand Down Expand Up @@ -612,7 +622,7 @@ def escaper
end

def commands(command)
command_list[command]
command_list[command]|| Bridge.extra_commands[command]
end

def unwrap_script_result(arg)
Expand Down
27 changes: 27 additions & 0 deletions rb/spec/unit/selenium/webdriver/remote/bridge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ module Selenium
module WebDriver
module Remote
describe Bridge do
describe '.add_command' do
let(:http) { WebDriver::Remote::Http::Default.new }
let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }

before do
allow(http).to receive(:request)
.with(any_args)
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})

bridge.create_session({})
end

after do
described_class.extra_commands.clear
end

it 'adds new command' do
described_class.add_command(:highlight, :get, 'session/:session_id/highlight/:id') do |element|
execute :highlight, id: element
end

bridge.highlight('bar')
expect(http).to have_received(:request)
.with(:get, URI('http://localhost/session/foo/highlight/bar'), any_args)
end
end

describe '#initialize' do
it 'raises ArgumentError if passed invalid options' do
expect { described_class.new(foo: 'bar') }.to raise_error(ArgumentError)
Expand Down

0 comments on commit 4cf9aeb

Please sign in to comment.