Skip to content

Commit ae80832

Browse files
committed
Add ability to pass handlers to each different intercepted element
1 parent ad82e86 commit ae80832

File tree

7 files changed

+83
-24
lines changed

7 files changed

+83
-24
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def cancel_auth(request_id)
7676
)
7777
end
7878

79-
def continue_with_request(**args)
79+
def continue_request(**args)
8080
@bidi.send_cmd(
8181
'network.continueRequest',
82-
request: args[:request_id],
82+
request: args[:id],
8383
'body' => args[:body],
8484
'cookies' => args[:cookies],
8585
'headers' => args[:headers],
@@ -88,15 +88,22 @@ def continue_with_request(**args)
8888
)
8989
end
9090

91-
def continue_with_response(**args)
91+
def fail_request(request_id)
92+
@bidi.send_cmd(
93+
'network.failRequest',
94+
request: request_id
95+
)
96+
end
97+
98+
def continue_response(**args)
9299
@bidi.send_cmd(
93100
'network.continueResponse',
94-
request: args[:response_id],
95-
'body' => args[:body],
101+
request: args[:id],
96102
'cookies' => args[:cookies],
97103
'credentials' => args[:credentials],
98104
'headers' => args[:headers],
99-
'status' => args[:status]
105+
'reasonPhrase' => args[:reason],
106+
'statusCode' => args[:status]
100107
)
101108
end
102109

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,14 @@ class BiDi
2323
class InterceptedItem
2424
attr_reader :network, :request
2525

26-
def initialize(network, request)
27-
@network = network
28-
@request = request
26+
def initialize(**args)
27+
@network = args[:network]
28+
@request = args[:request]
2929
end
3030

3131
def id
3232
@id ||= @request['request']
3333
end
34-
35-
def headers
36-
request['headers']
37-
end
38-
39-
def headers=(new_headers)
40-
request['headers'] = new_headers
41-
end
4234
end
4335
end # BiDi
4436
end # WebDriver

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class InterceptedRequest < InterceptedItem
24+
attr_accessor :body, :cookies, :headers, :method, :url
25+
26+
def new(**args)
27+
super(args[:network], args[:request])
28+
@body = args[:body]
29+
@cookies = args[:cookies]
30+
@headers = args[:headers]
31+
@method = args[:method]
32+
@url = args[:url]
33+
end
34+
2435
def continue
25-
network.continue_with_request(request_id: id)
36+
network.continue_request(id:, body:, cookies:, headers:, method:, url:)
37+
end
38+
39+
def fail
40+
network.fail_request(id)
2641
end
2742
end
2843
end # BiDi

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class InterceptedResponse < InterceptedItem
24+
attr_accessor :cookies, :headers, :credentials, :reason
25+
26+
def new
27+
super(args[:network], args[:request])
28+
@cookies = args[:cookies]
29+
@headers = args[:headers]
30+
@credentials = args[:credentials]
31+
@reason = args[:reason]
32+
end
33+
2434
def continue
25-
network.continue_with_response(response_id: id)
35+
network.continue_response(id:, cookies:, headers:, credentials:, reason:)
2636
end
2737
end
2838
end # BiDi

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def add_handler(event_type, phase, intercept_type, &block)
6262
intercept = network.add_intercept(phases: [phase])
6363
callback_id = network.on(event_type) do |event|
6464
request = event['request']
65-
intercepted_item = intercept_type.new(network, request)
65+
intercepted_item = intercept_type.new(network: network, request: request)
6666
block.call(intercepted_item)
6767
end
6868

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,40 @@ class BiDi
8484
end
8585
end
8686

87-
it 'continues with request' do
87+
it 'continues request' do
8888
reset_driver!(web_socket_url: true) do |driver|
8989
network = described_class.new(driver.bidi)
9090
network.add_intercept(phases: [described_class::PHASES[:before_request]])
9191
network.on(:before_request) do |event|
9292
request_id = event['request']['request']
93-
network.continue_with_request(request_id: request_id)
93+
network.continue_request(id: request_id)
9494
end
9595

9696
driver.navigate.to url_for('formPage.html')
9797
expect(driver.find_element(name: 'login')).to be_displayed
9898
end
9999
end
100100

101-
it 'continues with response' do
101+
it 'fails request' do
102+
reset_driver!(web_socket_url: true) do |driver|
103+
network = described_class.new(driver.bidi)
104+
network.add_intercept(phases: [described_class::PHASES[:before_request]])
105+
network.on(:before_request) do |event|
106+
request_id = event['request']['request']
107+
network.fail_request(request_id)
108+
end
109+
110+
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)
111+
end
112+
end
113+
114+
it 'continues response' do
102115
reset_driver!(web_socket_url: true) do |driver|
103116
network = described_class.new(driver.bidi)
104117
network.add_intercept(phases: [described_class::PHASES[:response_started]])
105118
network.on(:response_started) do |event|
106119
request_id = event['request']['request']
107-
network.continue_with_response(request_id: request_id)
120+
network.continue_response(id: request_id)
108121
end
109122

110123
driver.navigate.to url_for('formPage.html')

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ module WebDriver
8989
end
9090
end
9191

92+
it 'adds a request handler with attributes' do
93+
reset_driver!(web_socket_url: true) do |driver|
94+
network = described_class.new(driver)
95+
network.add_request_handler do |request|
96+
request.method = 'GET'
97+
request.continue
98+
end
99+
driver.navigate.to url_for('formPage.html')
100+
expect(driver.find_element(name: 'login')).to be_displayed
101+
expect(network.callbacks.count).to be 1
102+
end
103+
end
104+
105+
it 'fails a request' do
106+
reset_driver!(web_socket_url: true) do |driver|
107+
network = described_class.new(driver)
108+
network.add_request_handler(&:fail)
109+
expect(network.callbacks.count).to be 1
110+
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)
111+
end
112+
end
113+
92114
it 'removes a request handler' do
93115
reset_driver!(web_socket_url: true) do |driver|
94116
network = described_class.new(driver)

0 commit comments

Comments
 (0)