Skip to content

Commit 3597ecd

Browse files
authored
[rb] implement toggle for BiDi and Classic implementations (#14092)
[rb] use BiDiBridge subclass when web_socket_url is true
1 parent 8e268cf commit 3597ecd

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ def ref
318318
attr_reader :bridge
319319

320320
def create_bridge(caps:, url:, http_client: nil)
321-
Remote::Bridge.new(http_client: http_client, url: url).tap do |bridge|
321+
klass = caps['webSocketUrl'] ? Remote::BiDiBridge : Remote::Bridge
322+
klass.new(http_client: http_client, url: url).tap do |bridge|
322323
bridge.create_session(caps)
323324
end
324325
end

rb/lib/selenium/webdriver/remote.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module WebDriver
2525
module Remote
2626
autoload :Features, 'selenium/webdriver/remote/features'
2727
autoload :Bridge, 'selenium/webdriver/remote/bridge'
28+
autoload :BiDiBridge, 'selenium/webdriver/remote/bidi_bridge'
2829
autoload :Driver, 'selenium/webdriver/remote/driver'
2930
autoload :Response, 'selenium/webdriver/remote/response'
3031
autoload :Capabilities, 'selenium/webdriver/remote/capabilities'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
module Remote
23+
class BiDiBridge < Bridge
24+
attr_reader :bidi
25+
26+
def create_session(capabilities)
27+
super(capabilities)
28+
socket_url = @capabilities[:web_socket_url]
29+
@bidi = Selenium::WebDriver::BiDi.new(url: socket_url)
30+
end
31+
32+
def quit
33+
super
34+
ensure
35+
bidi.close
36+
end
37+
38+
def close
39+
execute(:close_window).tap { |handles| bidi.close if handles.empty? }
40+
end
41+
end # BiDiBridge
42+
end # Remote
43+
end # WebDriver
44+
end # Selenium

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,10 @@ def quit
213213
http.close
214214
rescue *QUIT_ERRORS
215215
nil
216-
ensure
217-
@bidi&.close
218216
end
219217

220218
def close
221-
execute(:close_window).tap { |handles| @bidi&.close if handles.empty? }
219+
execute :close_window
222220
end
223221

224222
def refresh
@@ -605,10 +603,8 @@ def user_verified(verified, authenticator_id)
605603
end
606604

607605
def bidi
608-
msg = 'this operation requires enabling BiDi by setting #web_socket_url to true in options class'
609-
raise(WebDriver::Error::WebDriverError, msg) unless capabilities.web_socket_url
610-
611-
@bidi ||= Selenium::WebDriver::BiDi.new(url: capabilities[:web_socket_url])
606+
msg = 'BiDi must be enabled by setting #web_socket_url to true in options class'
607+
raise(WebDriver::Error::WebDriverError, msg)
612608
end
613609

614610
def command_list

rb/spec/integration/selenium/webdriver/bidi/script_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ module WebDriver
2727

2828
it 'errors when bidi not enabled' do
2929
reset_driver!(web_socket_url: false) do |driver|
30-
expect {
31-
driver.script
32-
}.to raise_error(WebDriver::Error::WebDriverError, /this operation requires enabling BiDi/)
30+
msg = /BiDi must be enabled by setting #web_socket_url to true in options class/
31+
expect { driver.script }.to raise_error(WebDriver::Error::WebDriverError, msg)
3332
end
3433
end
3534

rb/spec/tests.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ def rb_integration_test(name, srcs, deps = [], data = [], browsers = BROWSERS.ke
207207
env = BROWSERS[browser]["env"] | {"WEBDRIVER_BIDI": "true"},
208208
main = "@bundle//bin:rspec",
209209
tags = COMMON_TAGS + BROWSERS[browser]["tags"] + tags + ["{}-bidi".format(browser)],
210-
deps = ["//rb/spec/integration/selenium/webdriver:spec_helper"] + BROWSERS[browser]["deps"] + deps,
210+
deps = depset(
211+
["//rb/spec/integration/selenium/webdriver:spec_helper", "//rb/lib/selenium/webdriver:bidi"] +
212+
BROWSERS[browser]["deps"] +
213+
deps,
214+
),
211215
visibility = ["//rb:__subpackages__"],
212216
target_compatible_with = BROWSERS[browser]["target_compatible_with"],
213217
)

0 commit comments

Comments
 (0)