Skip to content

Commit

Permalink
Update auth handlers and improve the :on method
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspe committed Dec 18, 2024
1 parent 7ef9d83 commit 2f2012a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
29 changes: 11 additions & 18 deletions rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions rb/lib/selenium/webdriver/common/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,54 @@
module Selenium
module WebDriver
class Network
attr_reader :callbacks
attr_reader :callbacks, :network

def initialize(bridge)
@network = BiDi::Network.new(bridge.bidi)
@callbacks = {}
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
Expand Down
9 changes: 7 additions & 2 deletions rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rb/spec/integration/selenium/webdriver/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f2012a

Please sign in to comment.