Skip to content

Commit d9e0ffc

Browse files
committed
Make auth handler more user friendly
1 parent 9c23adf commit d9e0ffc

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

rb/lib/selenium/webdriver/common/network.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ def clear_handlers
4444
callbacks.each_key { |id| remove_handler(id) }
4545
end
4646

47-
def add_authentication_handler(&)
48-
add_handler(:auth_required, BiDi::Network::PHASES[:auth_required], BiDi::InterceptedAuth, &)
47+
def add_authentication_handler(username = nil, password = nil, &block)
48+
selected_block =
49+
if username && password
50+
proc { |auth| auth.authenticate(username, password) }
51+
else
52+
block
53+
end
54+
55+
add_handler(:auth_required, BiDi::Network::PHASES[:auth_required], BiDi::InterceptedAuth, &selected_block)
4956
end
5057

5158
def add_request_handler(&)

rb/spec/integration/selenium/webdriver/network_spec.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ module WebDriver
2929
it 'adds an auth handler' do
3030
reset_driver!(web_socket_url: true) do |driver|
3131
network = described_class.new(driver)
32-
network.add_authentication_handler do |response|
33-
response.authenticate(username, password)
34-
end
32+
network.add_authentication_handler(username, password)
3533
driver.navigate.to url_for('basicAuth')
3634
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
3735
expect(network.callbacks.count).to be 1
@@ -41,9 +39,7 @@ module WebDriver
4139
it 'removes an auth handler' do
4240
reset_driver!(web_socket_url: true) do |driver|
4341
network = described_class.new(driver)
44-
id = network.add_authentication_handler do |response|
45-
response.authenticate(username, password)
46-
end
42+
id = network.add_authentication_handler(username, password)
4743
network.remove_handler(id)
4844
expect(network.callbacks.count).to be 0
4945
end
@@ -52,11 +48,7 @@ module WebDriver
5248
it 'clears all auth handlers' do
5349
reset_driver!(web_socket_url: true) do |driver|
5450
network = described_class.new(driver)
55-
2.times do
56-
network.add_authentication_handler do |response|
57-
response.authenticate(username, password)
58-
end
59-
end
51+
2.times { network.add_authentication_handler(username, password) }
6052
network.clear_handlers
6153
expect(network.callbacks.count).to be 0
6254
end

0 commit comments

Comments
 (0)