Skip to content

Commit

Permalink
[rb] implement toggle for BiDi and Classic implementations (#14092)
Browse files Browse the repository at this point in the history
[rb] use BiDiBridge subclass when web_socket_url is true
  • Loading branch information
titusfortner authored Jun 6, 2024
1 parent 8e268cf commit 3597ecd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ def ref
attr_reader :bridge

def create_bridge(caps:, url:, http_client: nil)
Remote::Bridge.new(http_client: http_client, url: url).tap do |bridge|
klass = caps['webSocketUrl'] ? Remote::BiDiBridge : Remote::Bridge
klass.new(http_client: http_client, url: url).tap do |bridge|
bridge.create_session(caps)
end
end
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module WebDriver
module Remote
autoload :Features, 'selenium/webdriver/remote/features'
autoload :Bridge, 'selenium/webdriver/remote/bridge'
autoload :BiDiBridge, 'selenium/webdriver/remote/bidi_bridge'
autoload :Driver, 'selenium/webdriver/remote/driver'
autoload :Response, 'selenium/webdriver/remote/response'
autoload :Capabilities, 'selenium/webdriver/remote/capabilities'
Expand Down
44 changes: 44 additions & 0 deletions rb/lib/selenium/webdriver/remote/bidi_bridge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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 Remote
class BiDiBridge < Bridge
attr_reader :bidi

def create_session(capabilities)
super(capabilities)
socket_url = @capabilities[:web_socket_url]
@bidi = Selenium::WebDriver::BiDi.new(url: socket_url)
end

def quit
super
ensure
bidi.close
end

def close
execute(:close_window).tap { |handles| bidi.close if handles.empty? }
end
end # BiDiBridge
end # Remote
end # WebDriver
end # Selenium
10 changes: 3 additions & 7 deletions rb/lib/selenium/webdriver/remote/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,10 @@ def quit
http.close
rescue *QUIT_ERRORS
nil
ensure
@bidi&.close
end

def close
execute(:close_window).tap { |handles| @bidi&.close if handles.empty? }
execute :close_window
end

def refresh
Expand Down Expand Up @@ -605,10 +603,8 @@ def user_verified(verified, authenticator_id)
end

def bidi
msg = 'this operation requires enabling BiDi by setting #web_socket_url to true in options class'
raise(WebDriver::Error::WebDriverError, msg) unless capabilities.web_socket_url

@bidi ||= Selenium::WebDriver::BiDi.new(url: capabilities[:web_socket_url])
msg = 'BiDi must be enabled by setting #web_socket_url to true in options class'
raise(WebDriver::Error::WebDriverError, msg)
end

def command_list
Expand Down
5 changes: 2 additions & 3 deletions rb/spec/integration/selenium/webdriver/bidi/script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ module WebDriver

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

Expand Down
6 changes: 5 additions & 1 deletion rb/spec/tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ def rb_integration_test(name, srcs, deps = [], data = [], browsers = BROWSERS.ke
env = BROWSERS[browser]["env"] | {"WEBDRIVER_BIDI": "true"},
main = "@bundle//bin:rspec",
tags = COMMON_TAGS + BROWSERS[browser]["tags"] + tags + ["{}-bidi".format(browser)],
deps = ["//rb/spec/integration/selenium/webdriver:spec_helper"] + BROWSERS[browser]["deps"] + deps,
deps = depset(
["//rb/spec/integration/selenium/webdriver:spec_helper", "//rb/lib/selenium/webdriver:bidi"] +
BROWSERS[browser]["deps"] +
deps,
),
visibility = ["//rb:__subpackages__"],
target_compatible_with = BROWSERS[browser]["target_compatible_with"],
)
Expand Down

0 comments on commit 3597ecd

Please sign in to comment.