From 2f2012ab93c5c86bc3f60ce84a15d32009033193 Mon Sep 17 00:00:00 2001 From: Augustin Pequeno Date: Wed, 18 Dec 2024 16:05:08 +0100 Subject: [PATCH] Update auth handlers and improve the :on method --- rb/lib/selenium/webdriver/bidi/network.rb | 29 +++++--------- rb/lib/selenium/webdriver/common/network.rb | 40 +++++++++---------- .../selenium/webdriver/bidi/network_spec.rb | 9 ++++- .../selenium/webdriver/network_spec.rb | 2 + 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/rb/lib/selenium/webdriver/bidi/network.rb b/rb/lib/selenium/webdriver/bidi/network.rb index 73000d749ee90..d3530653445fb 100644 --- a/rb/lib/selenium/webdriver/bidi/network.rb +++ b/rb/lib/selenium/webdriver/bidi/network.rb @@ -60,33 +60,26 @@ def continue_with_auth(request_id, username, password) ) end - def continue_with_request(**args) + def continue_with_auth_no_credentials(request_id) @bidi.send_cmd( - 'network.continueWithRequest', - request: args[:request_id], - 'body' => args[:body], - 'cookies' => args[:cookies], - 'headers' => args[:headers], - 'method' => args[:method], - 'url' => args[:url] + 'network.continueWithAuth', + 'request' => request_id, + 'action' => 'default' ) end - def continue_with_response(**args) + def cancel_auth(request_id) @bidi.send_cmd( - 'network.continueWithResponse', - response: args[:response_id], - 'body' => args[:body], - 'cookies' => args[:cookies], - 'credentials' => args[:credentials], - 'headers' => args[:headers], - 'status' => args[:status] + 'network.continueWithAuth', + 'request' => request_id, + 'action' => 'cancel' ) end - def on(event, &) + def on(event, &block) event = EVENTS[event] if event.is_a?(Symbol) - @bidi.add_callback(event, &) + @bidi.add_callback(event, &block) + @bidi.session.subscribe(event) end end # Network end # BiDi diff --git a/rb/lib/selenium/webdriver/common/network.rb b/rb/lib/selenium/webdriver/common/network.rb index 0a60c3d16f98a..5eb542bb6e1e0 100644 --- a/rb/lib/selenium/webdriver/common/network.rb +++ b/rb/lib/selenium/webdriver/common/network.rb @@ -20,7 +20,7 @@ module Selenium module WebDriver class Network - attr_reader :callbacks + attr_reader :callbacks, :network def initialize(bridge) @network = BiDi::Network.new(bridge.bidi) @@ -28,46 +28,46 @@ def initialize(bridge) end def remove_handler(id) - intercept = @callbacks[id] - @network.remove_intercept(intercept['intercept']) - @callbacks.delete(id) + intercept = callbacks[id] + network.remove_intercept(intercept['intercept']) + callbacks.delete(id) end def clear_handlers - @callbacks.each_key { |id| remove_handler(id) } + callbacks.each_key { |id| remove_handler(id) } end def add_authentication_handler(username, password) - intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:auth_required]]) - auth_id = @network.on(:auth_required) do |event| - request_id = event['requestId'] - @network.continue_with_auth(request_id, username, password) + intercept = network.add_intercept(phases: [Selenium::WebDriver::BiDi::Network::PHASES[:auth_required]]) + auth_id = network.on(:auth_required) do |event| + request_id = event['request']['request'] + network.continue_with_auth(request_id, username, password) end - @callbacks[auth_id] = intercept + callbacks[auth_id] = intercept auth_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) + intercept = network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]]) + request_id = network.on(:before_request) do |event| + request_id = event['request']['request'] + network.continue_with_request(request_id: request_id) end - @callbacks[request_id] = intercept + callbacks[request_id] = intercept request_id end def add_response_handler - intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:response_started]]) - response_id = @network.on(:response_started) do |event| - request_id = event['requestId'] - @network.continue_with_response(request_id: request_id) + intercept = network.add_intercept(phases: [BiDi::Network::PHASES[:response_started]]) + response_id = network.on(:response_started) do |event| + request_id = event['request']['request'] + network.continue_with_response(request_id: request_id) end - @callbacks[response_id] = intercept + callbacks[response_id] = intercept response_id end diff --git a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb index 6c6e7ec97c216..446a8e136e5cd 100644 --- a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb @@ -45,9 +45,10 @@ class BiDi password = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver.bidi) - network.add_intercept(phases: [described_class::PHASES[:auth_required]]) + phases = [Selenium::WebDriver::BiDi::Network::PHASES[:auth_required]] + network.add_intercept(phases: phases) network.on(:auth_required) do |event| - request_id = event['requestId'] + request_id = event['request']['request'] network.continue_with_auth(request_id, username, password) end @@ -56,16 +57,20 @@ class BiDi 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'] + pp 'cheese' network.continue_with_request(request_id: request_id) end + sleep 2 driver.navigate.to url_for('formPage.html') + sleep 2 expect(driver.find_element(name: 'login')).to be_displayed end end diff --git a/rb/spec/integration/selenium/webdriver/network_spec.rb b/rb/spec/integration/selenium/webdriver/network_spec.rb index 78f7a41b951de..be7d879a5f40d 100644 --- a/rb/spec/integration/selenium/webdriver/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/network_spec.rb @@ -30,6 +30,8 @@ module WebDriver reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) network.add_authentication_handler(username, password) + driver.navigate.to url_for('basicAuth') + expect(driver.find_element(tag_name: 'h1').text).to eq('authorized') expect(network.callbacks.count).to be 1 end end