Skip to content

Commit 7e22239

Browse files
committed
Handle requests and responses in block
1 parent 25c201b commit 7e22239

File tree

8 files changed

+202
-35
lines changed

8 files changed

+202
-35
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class BiDi
2626
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
2727
autoload :Struct, 'selenium/webdriver/bidi/struct'
2828
autoload :Network, 'selenium/webdriver/bidi/network'
29+
autoload :InterceptedRequest, 'selenium/webdriver/bidi/network/intercepted_request'
30+
autoload :InterceptedResponse, 'selenium/webdriver/bidi/network/intercepted_response'
31+
autoload :InterceptedAuth, 'selenium/webdriver/bidi/network/intercepted_auth'
32+
autoload :InterceptedItem, 'selenium/webdriver/bidi/network/intercepted_item'
2933

3034
def initialize(url:)
3135
@ws = WebSocketConnection.new(url: url)

rb/lib/selenium/webdriver/bidi/network.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def continue_with_request(**args)
9191
def continue_with_response(**args)
9292
@bidi.send_cmd(
9393
'network.continueResponse',
94-
request: args[:request_id],
94+
request: args[:response_id],
9595
'body' => args[:body],
9696
'cookies' => args[:cookies],
9797
'credentials' => args[:credentials],
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class InterceptedAuth < InterceptedItem
24+
def authenticate(username, password)
25+
network.continue_with_auth(id, username, password)
26+
end
27+
28+
def skip
29+
network.continue_without_auth(id)
30+
end
31+
32+
def cancel
33+
network.cancel_auth(id)
34+
end
35+
end
36+
end # BiDi
37+
end # WebDriver
38+
end # Selenium
39+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class InterceptedItem
24+
attr_reader :network, :request
25+
26+
def initialize(network, request)
27+
@network = network
28+
@request = request
29+
end
30+
31+
def id
32+
@id ||= @request['request']
33+
end
34+
35+
def headers
36+
request['headers']
37+
end
38+
39+
def headers=(new_headers)
40+
request['headers'] = new_headers
41+
end
42+
end
43+
end # BiDi
44+
end # WebDriver
45+
end # Selenium
46+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class InterceptedRequest < InterceptedItem
24+
def continue
25+
network.continue_with_request(request_id: id)
26+
end
27+
end
28+
end # BiDi
29+
end # WebDriver
30+
end # Selenium
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class InterceptedResponse < InterceptedItem
24+
def continue
25+
network.continue_with_response(response_id: id)
26+
end
27+
end
28+
end # BiDi
29+
end # WebDriver
30+
end # Selenium
31+

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

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

47-
def add_authentication_handler(username, password)
48-
intercept = network.add_intercept(phases: [Selenium::WebDriver::BiDi::Network::PHASES[:auth_required]])
49-
auth_id = network.on(:auth_required) do |event|
50-
request_id = fetch_id(event)
51-
network.continue_with_auth(request_id, username, password)
52-
end
53-
54-
callbacks[auth_id] = intercept
55-
auth_id
47+
def add_authentication_handler(&)
48+
add_handler(:auth_required, BiDi::Network::PHASES[:auth_required], BiDi::InterceptedAuth, &)
5649
end
5750

5851
def add_request_handler(&)
59-
intercept = network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]])
60-
request_id = network.on(:before_request, &)
61-
62-
callbacks[request_id] = intercept
63-
64-
request_id
52+
add_handler(:before_request, BiDi::Network::PHASES[:before_request], BiDi::InterceptedRequest, &)
6553
end
6654

6755
def add_response_handler(&)
68-
intercept = network.add_intercept(phases: [BiDi::Network::PHASES[:response_started]])
69-
response_id = network.on(:response_started, &)
56+
add_handler(:response_started, BiDi::Network::PHASES[:response_started], BiDi::InterceptedResponse, &)
57+
end
7058

71-
callbacks[response_id] = intercept
59+
private
7260

73-
response_id
74-
end
61+
def add_handler(event_type, phase, intercept_type, &block)
62+
intercept = network.add_intercept(phases: [phase])
63+
callback_id = network.on(event_type) do |event|
64+
request = event['request']
65+
intercepted_item = intercept_type.new(network, request)
66+
block.call(intercepted_item)
67+
end
7568

76-
def fetch_id(event)
77-
event['request']['request']
69+
callbacks[callback_id] = intercept
70+
callback_id
7871
end
7972
end # Network
8073
end # WebDriver

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ 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(username, password)
32+
network.add_authentication_handler do |response|
33+
response.authenticate(username, password)
34+
end
3335
driver.navigate.to url_for('basicAuth')
3436
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
3537
expect(network.callbacks.count).to be 1
@@ -39,7 +41,9 @@ module WebDriver
3941
it 'removes an auth handler' do
4042
reset_driver!(web_socket_url: true) do |driver|
4143
network = described_class.new(driver)
42-
id = network.add_authentication_handler(username, password)
44+
id = network.add_authentication_handler do |response|
45+
response.authenticate(username, password)
46+
end
4347
network.remove_handler(id)
4448
expect(network.callbacks.count).to be 0
4549
end
@@ -48,17 +52,37 @@ module WebDriver
4852
it 'clears all auth handlers' do
4953
reset_driver!(web_socket_url: true) do |driver|
5054
network = described_class.new(driver)
51-
network.add_authentication_handler(username, password)
52-
network.add_authentication_handler(username, password)
55+
2.times do
56+
network.add_authentication_handler do |response|
57+
response.authenticate(username, password)
58+
end
59+
end
5360
network.clear_handlers
5461
expect(network.callbacks.count).to be 0
5562
end
5663
end
5764

65+
it 'continues without auth' do
66+
reset_driver!(web_socket_url: true) do |driver|
67+
network = described_class.new(driver)
68+
network.add_authentication_handler(&:skip)
69+
expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)
70+
end
71+
end
72+
73+
it 'cancels auth' do
74+
reset_driver!(web_socket_url: true) do |driver|
75+
network = described_class.new(driver)
76+
network.add_authentication_handler(&:cancel)
77+
driver.navigate.to url_for('basicAuth')
78+
expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')
79+
end
80+
end
81+
5882
it 'adds a request handler' do
5983
reset_driver!(web_socket_url: true) do |driver|
6084
network = described_class.new(driver)
61-
network.add_request_handler { |event| network.continue_with_request(request_id: network.fetch_id(event)) }
85+
network.add_request_handler(&:continue)
6286
driver.navigate.to url_for('formPage.html')
6387
expect(driver.find_element(name: 'login')).to be_displayed
6488
expect(network.callbacks.count).to be 1
@@ -68,7 +92,7 @@ module WebDriver
6892
it 'removes a request handler' do
6993
reset_driver!(web_socket_url: true) do |driver|
7094
network = described_class.new(driver)
71-
id = network.add_request_handler
95+
id = network.add_request_handler(&:continue)
7296
network.remove_handler(id)
7397
expect(network.callbacks.count).to be 0
7498
end
@@ -77,8 +101,7 @@ module WebDriver
77101
it 'clears all request handlers' do
78102
reset_driver!(web_socket_url: true) do |driver|
79103
network = described_class.new(driver)
80-
network.add_request_handler
81-
network.add_request_handler
104+
2.times { network.add_request_handler(&:continue) }
82105
network.clear_handlers
83106
expect(network.callbacks.count).to be 0
84107
end
@@ -87,7 +110,7 @@ module WebDriver
87110
it 'adds a response handler' do
88111
reset_driver!(web_socket_url: true) do |driver|
89112
network = described_class.new(driver)
90-
network.add_response_handler { |event| network.continue_with_response(request_id: network.fetch_id(event)) }
113+
network.add_response_handler(&:continue)
91114
driver.navigate.to url_for('formPage.html')
92115
expect(driver.find_element(name: 'login')).to be_displayed
93116
expect(network.callbacks.count).to be 1
@@ -97,17 +120,17 @@ module WebDriver
97120
it 'removes a response handler' do
98121
reset_driver!(web_socket_url: true) do |driver|
99122
network = described_class.new(driver)
100-
id = network.add_response_handler
123+
id = network.add_response_handler(&:continue)
101124
network.remove_handler(id)
125+
network.clear_handlers
102126
expect(network.callbacks.count).to be 0
103127
end
104128
end
105129

106130
it 'clears all response handlers' do
107131
reset_driver!(web_socket_url: true) do |driver|
108132
network = described_class.new(driver)
109-
network.add_response_handler
110-
network.add_response_handler
133+
2.times { network.add_response_handler(&:continue) }
111134
network.clear_handlers
112135
expect(network.callbacks.count).to be 0
113136
end

0 commit comments

Comments
 (0)