Skip to content

Commit 3557bfb

Browse files
committed
add provide response support
1 parent 26253b8 commit 3557bfb

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ def continue_response(**args)
112112
)
113113
end
114114

115+
def provide_response(**args)
116+
@bidi.send_cmd(
117+
'network.provideResponse',
118+
request: args[:id],
119+
body: args[:body],
120+
cookies: args[:cookies],
121+
headers: args[:headers],
122+
reasonPhrase: args[:reason],
123+
statusCode: args[:status]
124+
)
125+
end
126+
115127
def on(event, &)
116128
event = EVENTS[event] if event.is_a?(Symbol)
117129
@bidi.add_callback(event, &)

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ module Selenium
2525
module WebDriver
2626
class BiDi
2727
class InterceptedResponse < InterceptedItem
28-
attr_accessor :reason
28+
attr_accessor :reason, :status
29+
attr_reader :body
2930

3031
def initialize(network, request)
3132
super
3233
@reason = nil
34+
@status = nil
35+
@body = nil
3336
end
3437

3538
def continue
@@ -38,7 +41,19 @@ def continue
3841
cookies: set_cookie_headers.serialize,
3942
headers: headers.serialize,
4043
credentials: credentials.serialize,
41-
reason: reason
44+
reason: reason,
45+
status: status
46+
)
47+
end
48+
49+
def provide_response
50+
network.provide_response(
51+
id: id,
52+
cookies: set_cookie_headers.serialize,
53+
headers: headers.serialize,
54+
body: body,
55+
reason: reason,
56+
status: status
4257
)
4358
end
4459

@@ -53,6 +68,13 @@ def headers
5368
def set_cookie_headers(set_cookie_headers = {})
5469
@set_cookie_headers ||= SetCookieHeaders.new(set_cookie_headers)
5570
end
71+
72+
def body=(value)
73+
@body = {
74+
type: 'string',
75+
value: value.to_json
76+
}
77+
end
5678
end
5779
end # BiDi
5880
end # WebDriver

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

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

2323
def fail_request: -> Hash[nil, nil]
2424

25+
def provide_response: -> Hash[nil, nil]
26+
2527
def remove_intercept: (String intercept) -> Hash[nil, nil]
2628

2729
def continue_with_auth: (String request_id, String username, String password) -> Hash[nil, nil]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class InterceptedResponse < InterceptedItem
5+
@body: untyped
56
@reason: untyped
67

78
@credentials: untyped
@@ -10,16 +11,23 @@ module Selenium
1011

1112
@set_cookie_headers: untyped
1213

14+
attr_reader body: untyped
1315
attr_accessor reason: untyped
1416

17+
attr_accessor status: Integer?
18+
1519
def initialize: (untyped network, untyped request) -> void
1620

21+
def body=: -> untyped
22+
1723
def continue: () -> untyped
1824

1925
def credentials: (?username: untyped?, ?password: untyped?) -> untyped
2026

2127
def headers: () -> untyped
2228

29+
def provide_response: -> untyped
30+
2331
def set_cookie_headers: (?untyped? set_cookie_headers) -> untyped
2432
end
2533
end

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ class BiDi
144144
expect(driver.find_element(name: 'login')).to be_displayed
145145
end
146146
end
147+
148+
it 'provides response' do
149+
reset_driver!(web_socket_url: true) do |driver|
150+
network = described_class.new(driver.bidi)
151+
network.add_intercept(phases: [described_class::PHASES[:response_started]])
152+
network.on(:response_started) do |event|
153+
request_id = event['request']['request']
154+
network.provide_response(id: request_id,
155+
status: 200,
156+
headers: [
157+
{
158+
name: 'foo',
159+
value: {
160+
type: 'string',
161+
value: 'bar'
162+
}
163+
}
164+
],
165+
body: {
166+
type: 'string',
167+
value: '<html><head><title>Hello World!</title></head><body/></html>'
168+
})
169+
end
170+
171+
driver.navigate.to url_for('formPage.html')
172+
expect(driver.page_source).to include('Hello World!')
173+
end
174+
end
147175
end
148176
end
149177
end

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module WebDriver
150150
request.headers['foo'] = 'bar'
151151
request.headers['baz'] = 'qux'
152152
request.cookies['foo'] = 'bar'
153-
request.body = ({test: 'example'})
153+
request.body = ({ test: 'example' })
154154
request.continue
155155
end
156156
driver.navigate.to url_for('formPage.html')
@@ -274,6 +274,20 @@ module WebDriver
274274
end
275275
end
276276

277+
it 'adds a response handler that provides a response' do
278+
reset_driver!(web_socket_url: true) do |driver|
279+
network = described_class.new(driver)
280+
network.add_response_handler do |response|
281+
response.status = 200
282+
response.headers['foo'] = 'bar'
283+
response.body = '<html><head><title>Hello World!</title></head><body/></html>'
284+
response.provide_response
285+
end
286+
driver.navigate.to url_for('formPage.html')
287+
expect(driver.page_source).to include('Hello World!')
288+
end
289+
end
290+
277291
it 'removes a response handler' do
278292
reset_driver!(web_socket_url: true) do |driver|
279293
network = described_class.new(driver)

0 commit comments

Comments
 (0)