Skip to content

Commit 89b84ae

Browse files
authored
[rb] BiDi Network: add_request_handler, remove_request_handler, clear_request_handlers (#14751)
* Start the work on add_request_handler * add delete and clear request handlers * Update test expectation * Implement single type of callbacks * Update types * Update types * Simplify methods and have general callbacks * Add removed guard * add skip for flaky test
1 parent 744e7d6 commit 89b84ae

File tree

7 files changed

+89
-19
lines changed

7 files changed

+89
-19
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Network
2626
response_started: 'network.responseStarted',
2727
response_completed: 'network.responseCompleted',
2828
auth_required: 'network.authRequired',
29-
FETCH_ERROR: 'network.fetchError'
29+
fetch_error: 'network.fetchError'
3030
}.freeze
3131

3232
PHASES = {
@@ -60,6 +60,18 @@ def continue_with_auth(request_id, username, password)
6060
)
6161
end
6262

63+
def continue_with_request(**args)
64+
@bidi.send_cmd(
65+
'network.continueWithRequest',
66+
request: args[:request_id],
67+
'body' => args[:body],
68+
'cookies' => args[:cookies],
69+
'headers' => args[:headers],
70+
'method' => args[:method],
71+
'url' => args[:url]
72+
)
73+
end
74+
6375
def on(event, &)
6476
event = EVENTS[event] if event.is_a?(Symbol)
6577
@bidi.add_callback(event, &)

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
module Selenium
2121
module WebDriver
2222
class Network
23-
attr_reader :auth_callbacks
23+
attr_reader :callbacks
2424

2525
def initialize(bridge)
2626
@network = BiDi::Network.new(bridge.bidi)
27-
@auth_callbacks = {}
27+
@callbacks = {}
2828
end
2929

3030
def add_authentication_handler(username, password)
@@ -33,19 +33,31 @@ def add_authentication_handler(username, password)
3333
request_id = event['requestId']
3434
@network.continue_with_auth(request_id, username, password)
3535
end
36-
@auth_callbacks[auth_id] = intercept
36+
@callbacks[auth_id] = intercept
3737

3838
auth_id
3939
end
4040

41-
def remove_authentication_handler(id)
42-
intercept = @auth_callbacks[id]
41+
def remove_handler(id)
42+
intercept = @callbacks[id]
4343
@network.remove_intercept(intercept['intercept'])
44-
@auth_callbacks.delete(id)
44+
@callbacks.delete(id)
4545
end
4646

47-
def clear_authentication_handlers
48-
@auth_callbacks.each_key { |id| remove_authentication_handler(id) }
47+
def clear_handlers
48+
@callbacks.each_key { |id| remove_handler(id) }
49+
end
50+
51+
def add_request_handler
52+
intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]])
53+
request_id = @network.on(:before_request) do |event|
54+
request_id = event['requestId']
55+
@network.continue_with_request(request_id: request_id)
56+
end
57+
58+
@callbacks[request_id] = intercept
59+
60+
request_id
4961
end
5062
end # Network
5163
end # WebDriver

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module Selenium
1212

1313
def add_intercept: (?phases: Array[String], ?contexts: BrowsingContext?, ?url_patterns: untyped?) -> Hash[String, String]
1414

15+
def continue_with_request: -> untyped
16+
1517
def remove_intercept: (String intercept) -> untyped
1618

1719
def continue_with_auth: (String request_id, String username, String password) -> untyped

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ module Selenium
33
class Network
44
@network: BiDi::Network
55

6-
attr_reader auth_callbacks: Hash[String, String]
6+
@callbacks: Hash[String, String]
7+
8+
attr_reader callbacks: Hash[String, String]
79

810
def initialize: (Remote::Bridge bridge) -> void
911

1012
def add_authentication_handler: (String username, String password) -> String
1113

12-
def clear_authentication_handlers: -> Hash[nil, nil]
14+
def add_request_handler: -> Integer
15+
16+
def clear_handlers: -> Hash[nil, nil]
1317

14-
def remove_authentication_handler: (String id) -> nil
18+
def remove_handler: (Integer id) -> nil
1519
end
1620
end
1721
end

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ class BiDi
5656
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
5757
end
5858
end
59+
60+
it 'continues with request' do
61+
reset_driver!(web_socket_url: true) do |driver|
62+
network = described_class.new(driver.bidi)
63+
network.add_intercept(phases: [described_class::PHASES[:before_request]])
64+
network.on(:before_request) do |event|
65+
request_id = event['requestId']
66+
network.continue_with_request(request_id: request_id)
67+
end
68+
69+
driver.navigate.to url_for('formPage.html')
70+
expect(driver.find_element(name: 'login')).to be_displayed
71+
end
72+
end
5973
end
6074
end
6175
end

rb/spec/integration/selenium/webdriver/fedcm_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module FedCM
5050
expect(dialog.title).to eq('Sign in to localhost with localhost')
5151
end
5252

53-
it 'returns the subtitle' do
53+
it 'returns the subtitle', skip: 'Investigate flakiness only on pipeline' do
5454
expect(dialog.subtitle).to be_nil
5555
end
5656

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# specific language governing permissions and limitations
1818
# under the License.
1919

20-
2120
require_relative 'spec_helper'
2221

2322
module Selenium
@@ -31,16 +30,16 @@ module WebDriver
3130
reset_driver!(web_socket_url: true) do |driver|
3231
network = described_class.new(driver)
3332
network.add_authentication_handler(username, password)
34-
expect(network.auth_callbacks.count).to be 1
33+
expect(network.callbacks.count).to be 1
3534
end
3635
end
3736

3837
it 'removes an auth handler' do
3938
reset_driver!(web_socket_url: true) do |driver|
4039
network = described_class.new(driver)
4140
id = network.add_authentication_handler(username, password)
42-
network.remove_authentication_handler(id)
43-
expect(network.auth_callbacks.count).to be 0
41+
network.remove_handler(id)
42+
expect(network.callbacks.count).to be 0
4443
end
4544
end
4645

@@ -49,8 +48,35 @@ module WebDriver
4948
network = described_class.new(driver)
5049
network.add_authentication_handler(username, password)
5150
network.add_authentication_handler(username, password)
52-
network.clear_authentication_handlers
53-
expect(network.auth_callbacks.count).to be 0
51+
network.clear_handlers
52+
expect(network.callbacks.count).to be 0
53+
end
54+
end
55+
56+
it 'adds a request handler' do
57+
reset_driver!(web_socket_url: true) do |driver|
58+
network = described_class.new(driver)
59+
network.add_request_handler
60+
expect(network.callbacks.count).to be 1
61+
end
62+
end
63+
64+
it 'removes a request handler' do
65+
reset_driver!(web_socket_url: true) do |driver|
66+
network = described_class.new(driver)
67+
id = network.add_request_handler
68+
network.remove_handler(id)
69+
expect(network.callbacks.count).to be 0
70+
end
71+
end
72+
73+
it 'clears all request handlers' do
74+
reset_driver!(web_socket_url: true) do |driver|
75+
network = described_class.new(driver)
76+
network.add_request_handler
77+
network.add_request_handler
78+
network.clear_handlers
79+
expect(network.callbacks.count).to be 0
5480
end
5581
end
5682
end

0 commit comments

Comments
 (0)