Skip to content

Commit 6b5ceb2

Browse files
committed
Start the work on add_request_handler
1 parent 9af4e15 commit 6b5ceb2

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,27 @@ 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, &)
6678
end
67-
end # Network
68-
end # BiDi
79+
end
80+
81+
# Network
82+
end
83+
84+
# BiDi
6985
end # WebDriver
7086
end # Selenium

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
module Selenium
2121
module WebDriver
2222
class Network
23-
attr_reader :auth_callbacks
23+
attr_reader :auth_callbacks, :request_callbacks
2424

2525
def initialize(bridge)
2626
@network = BiDi::Network.new(bridge.bidi)
2727
@auth_callbacks = {}
28+
@request_callbacks = {}
2829
end
2930

3031
def add_authentication_handler(username, password)
@@ -47,6 +48,18 @@ def remove_authentication_handler(id)
4748
def clear_authentication_handlers
4849
@auth_callbacks.each_key { |id| remove_authentication_handler(id) }
4950
end
51+
52+
def add_request_handler
53+
intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]])
54+
request_id = @network.on(:before_request) do |event|
55+
request_id = event['requestId']
56+
@network.continue_with_request(request_id: request_id)
57+
end
58+
59+
@request_callbacks[request_id] = intercept
60+
61+
request_id
62+
end
5063
end # Network
5164
end # WebDriver
5265
end # Selenium

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ module Selenium
33
class Network
44
@network: BiDi::Network
55

6+
@request_callbacks: untyped
7+
68
attr_reader auth_callbacks: Hash[String, String]
79

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

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

14+
def add_request_handler: -> untyped
15+
1216
def clear_authentication_handlers: -> Hash[nil, nil]
1317

1418
def remove_authentication_handler: (String id) -> nil

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('simple_page')
70+
expect(driver.find_element(tag_name: 'h1').text).to eq('Simple Page')
71+
end
72+
end
5973
end
6074
end
6175
end

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ module WebDriver
5353
expect(network.auth_callbacks.count).to be 0
5454
end
5555
end
56+
57+
it 'adds a request handler' do
58+
reset_driver!(web_socket_url: true) do |driver|
59+
network = described_class.new(driver)
60+
network.add_request_handler
61+
expect(network.request_callbacks.count).to be 1
62+
end
63+
end
5664
end
5765
end
5866
end

0 commit comments

Comments
 (0)