Skip to content

Commit 4cf9aeb

Browse files
committed
[rb] Support registering extra bridge commands
1 parent 3ec3cef commit 4cf9aeb

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

rb/lib/selenium/webdriver/remote/bridge.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class Bridge
2929
attr_accessor :http, :file_detector
3030
attr_reader :capabilities
3131

32+
class << self
33+
attr_reader :extra_commands
34+
35+
def add_command(name, verb, url, &block)
36+
@extra_commands ||= {}
37+
@extra_commands[name] = [verb, url]
38+
define_method(name, &block)
39+
end
40+
end
41+
3242
#
3343
# Initializes the bridge with the given server URL
3444
# @param [String, URI] url url for the remote server
@@ -612,7 +622,7 @@ def escaper
612622
end
613623

614624
def commands(command)
615-
command_list[command]
625+
command_list[command]|| Bridge.extra_commands[command]
616626
end
617627

618628
def unwrap_script_result(arg)

rb/spec/unit/selenium/webdriver/remote/bridge_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ module Selenium
2323
module WebDriver
2424
module Remote
2525
describe Bridge do
26+
describe '.add_command' do
27+
let(:http) { WebDriver::Remote::Http::Default.new }
28+
let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }
29+
30+
before do
31+
allow(http).to receive(:request)
32+
.with(any_args)
33+
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
34+
35+
bridge.create_session({})
36+
end
37+
38+
after do
39+
described_class.extra_commands.clear
40+
end
41+
42+
it 'adds new command' do
43+
described_class.add_command(:highlight, :get, 'session/:session_id/highlight/:id') do |element|
44+
execute :highlight, id: element
45+
end
46+
47+
bridge.highlight('bar')
48+
expect(http).to have_received(:request)
49+
.with(:get, URI('http://localhost/session/foo/highlight/bar'), any_args)
50+
end
51+
end
52+
2653
describe '#initialize' do
2754
it 'raises ArgumentError if passed invalid options' do
2855
expect { described_class.new(foo: 'bar') }.to raise_error(ArgumentError)

0 commit comments

Comments
 (0)