Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rb] BiDi Network: add_request_handler, remove_request_handler, clear_request_handlers #14751

Merged
merged 16 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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, &)
Expand Down
28 changes: 20 additions & 8 deletions rb/lib/selenium/webdriver/common/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/bidi/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions rb/sig/lib/selenium/webdriver/common/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rb/spec/integration/selenium/webdriver/fedcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 32 additions & 6 deletions rb/spec/integration/selenium/webdriver/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# specific language governing permissions and limitations
# under the License.


require_relative 'spec_helper'

module Selenium
Expand All @@ -31,16 +30,16 @@ 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

it 'removes an auth handler' do
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

Expand All @@ -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
Expand Down
Loading