diff --git a/rb/lib/selenium/webdriver/bidi/network.rb b/rb/lib/selenium/webdriver/bidi/network.rb index a6cdef3c344f6..ae55f9200efe4 100644 --- a/rb/lib/selenium/webdriver/bidi/network.rb +++ b/rb/lib/selenium/webdriver/bidi/network.rb @@ -26,7 +26,7 @@ class Network response_started: 'network.responseStarted', response_completed: 'network.responseCompleted', auth_required: 'network.authRequired', - FETCH_ERROR: 'network.fetchError' + fetch_error: 'network.fetchError' }.freeze PHASES = { @@ -60,6 +60,18 @@ def continue_with_auth(request_id, username, password) ) end + def continue_with_request(**args) + @bidi.send_cmd( + 'network.continueWithRequest', + request: args[:request_id], + 'body' => args[:body], + 'cookies' => args[:cookies], + 'headers' => args[:headers], + 'method' => args[:method], + 'url' => args[:url] + ) + end + def on(event, &) event = EVENTS[event] if event.is_a?(Symbol) @bidi.add_callback(event, &) diff --git a/rb/lib/selenium/webdriver/common/network.rb b/rb/lib/selenium/webdriver/common/network.rb index c78980b954bff..91bc0525c8340 100644 --- a/rb/lib/selenium/webdriver/common/network.rb +++ b/rb/lib/selenium/webdriver/common/network.rb @@ -20,11 +20,11 @@ module Selenium module WebDriver class Network - attr_reader :auth_callbacks + attr_reader :callbacks def initialize(bridge) @network = BiDi::Network.new(bridge.bidi) - @auth_callbacks = {} + @callbacks = {} end def add_authentication_handler(username, password) @@ -33,19 +33,31 @@ def add_authentication_handler(username, password) request_id = event['requestId'] @network.continue_with_auth(request_id, username, password) end - @auth_callbacks[auth_id] = intercept + @callbacks[auth_id] = intercept auth_id end - def remove_authentication_handler(id) - intercept = @auth_callbacks[id] + def remove_handler(id) + intercept = @callbacks[id] @network.remove_intercept(intercept['intercept']) - @auth_callbacks.delete(id) + @callbacks.delete(id) end - def clear_authentication_handlers - @auth_callbacks.each_key { |id| remove_authentication_handler(id) } + def clear_handlers + @callbacks.each_key { |id| remove_handler(id) } + end + + def add_request_handler + intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]]) + request_id = @network.on(:before_request) do |event| + request_id = event['requestId'] + @network.continue_with_request(request_id: request_id) + end + + @callbacks[request_id] = intercept + + request_id end end # Network end # WebDriver diff --git a/rb/sig/lib/selenium/webdriver/bidi/network.rbs b/rb/sig/lib/selenium/webdriver/bidi/network.rbs index 96dcdc47d89ed..44645cf05d250 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network.rbs @@ -12,6 +12,8 @@ module Selenium def add_intercept: (?phases: Array[String], ?contexts: BrowsingContext?, ?url_patterns: untyped?) -> Hash[String, String] + def continue_with_request: -> untyped + def remove_intercept: (String intercept) -> untyped def continue_with_auth: (String request_id, String username, String password) -> untyped diff --git a/rb/sig/lib/selenium/webdriver/common/network.rbs b/rb/sig/lib/selenium/webdriver/common/network.rbs index edd306ee3990a..2ad03a748167e 100644 --- a/rb/sig/lib/selenium/webdriver/common/network.rbs +++ b/rb/sig/lib/selenium/webdriver/common/network.rbs @@ -3,15 +3,19 @@ module Selenium class Network @network: BiDi::Network - attr_reader auth_callbacks: Hash[String, String] + @callbacks: Hash[String, String] + + attr_reader callbacks: Hash[String, String] def initialize: (Remote::Bridge bridge) -> void def add_authentication_handler: (String username, String password) -> String - def clear_authentication_handlers: -> Hash[nil, nil] + def add_request_handler: -> Integer + + def clear_handlers: -> Hash[nil, nil] - def remove_authentication_handler: (String id) -> nil + def remove_handler: (Integer id) -> nil end end end diff --git a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb index 7aef0ba9856aa..80aad4de78e31 100644 --- a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb @@ -56,6 +56,20 @@ class BiDi expect(driver.find_element(tag_name: 'h1').text).to eq('authorized') end end + + it 'continues with request' do + reset_driver!(web_socket_url: true) do |driver| + network = described_class.new(driver.bidi) + network.add_intercept(phases: [described_class::PHASES[:before_request]]) + network.on(:before_request) do |event| + request_id = event['requestId'] + network.continue_with_request(request_id: request_id) + end + + driver.navigate.to url_for('formPage.html') + expect(driver.find_element(name: 'login')).to be_displayed + end + end end end end diff --git a/rb/spec/integration/selenium/webdriver/fedcm_spec.rb b/rb/spec/integration/selenium/webdriver/fedcm_spec.rb index 608f3a4361294..aeee262e9a41a 100644 --- a/rb/spec/integration/selenium/webdriver/fedcm_spec.rb +++ b/rb/spec/integration/selenium/webdriver/fedcm_spec.rb @@ -50,7 +50,7 @@ module FedCM expect(dialog.title).to eq('Sign in to localhost with localhost') end - it 'returns the subtitle' do + it 'returns the subtitle', skip: 'Investigate flakiness only on pipeline' do expect(dialog.subtitle).to be_nil end diff --git a/rb/spec/integration/selenium/webdriver/network_spec.rb b/rb/spec/integration/selenium/webdriver/network_spec.rb index 11776a4e2c73f..2ad4bc9f5365c 100644 --- a/rb/spec/integration/selenium/webdriver/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/network_spec.rb @@ -17,7 +17,6 @@ # specific language governing permissions and limitations # under the License. - require_relative 'spec_helper' module Selenium @@ -31,7 +30,7 @@ module WebDriver reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) network.add_authentication_handler(username, password) - expect(network.auth_callbacks.count).to be 1 + expect(network.callbacks.count).to be 1 end end @@ -39,8 +38,8 @@ module WebDriver reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) id = network.add_authentication_handler(username, password) - network.remove_authentication_handler(id) - expect(network.auth_callbacks.count).to be 0 + network.remove_handler(id) + expect(network.callbacks.count).to be 0 end end @@ -49,8 +48,35 @@ module WebDriver network = described_class.new(driver) network.add_authentication_handler(username, password) network.add_authentication_handler(username, password) - network.clear_authentication_handlers - expect(network.auth_callbacks.count).to be 0 + network.clear_handlers + expect(network.callbacks.count).to be 0 + end + end + + it 'adds a request handler' do + reset_driver!(web_socket_url: true) do |driver| + network = described_class.new(driver) + network.add_request_handler + expect(network.callbacks.count).to be 1 + end + end + + it 'removes a request handler' do + reset_driver!(web_socket_url: true) do |driver| + network = described_class.new(driver) + id = network.add_request_handler + network.remove_handler(id) + expect(network.callbacks.count).to be 0 + end + end + + it 'clears all request handlers' do + reset_driver!(web_socket_url: true) do |driver| + network = described_class.new(driver) + network.add_request_handler + network.add_request_handler + network.clear_handlers + expect(network.callbacks.count).to be 0 end end end