Skip to content

Commit 73607ee

Browse files
aguspepujagani
andauthored
[rb][BiDi] Add support for provide response command (#15080)
* Add response handler * Update auth handlers and improve the :on method * Request and response working as expected * Add test for continue without auth and cancel auth * Finish implementation * Correct rubocop offenses * Add alias for user to do network.bidi instead of network.network * Fix rust file causing formatting error * Handle requests and responses in block * Add ability to pass handlers to each different intercepted element * Headers working * All tests passing and signatures simplified * remove unnecessary changes * remove unnecessary changes * Added credentials and set cookie header to intercepted response * Fix rubocop issues * Make auth handler more user friendly * Add filtering an url pattern support * Fix formatting issues * Modify tests to avoid element related failures on firefox * Fix styling of comments * Improve types and hash consistency * Update all the places that use 9.4.8.0 * Serialize request values * Refactor cookies, headers and set cookie headers * Simplify serialization * Add extra type support * remove unnecessary comment * Improve typing * add provide response support * Correct rubocop offenses * Remove set cookie headers * Remove set cookie headers * Fix failing tests * Add negative scenario --------- Co-authored-by: Puja Jagani <puja.jagani93@gmail.com>
1 parent 2357514 commit 73607ee

File tree

8 files changed

+96
-15
lines changed

8 files changed

+96
-15
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: cookies.as_json,
3942
headers: headers.as_json,
4043
credentials: credentials.as_json,
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: cookies.as_json,
53+
headers: headers.as_json,
54+
body: body,
55+
reason: reason,
56+
status: status
4257
)
4358
end
4459

@@ -53,6 +68,13 @@ def headers
5368
def cookies(cookies = {})
5469
@cookies ||= Cookies.new(cookies)
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
@@ -26,6 +26,8 @@ module Selenium
2626

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

29+
def provide_response: -> Hash[nil, nil]
30+
2931
def on: (Symbol event) { (?) -> untyped } -> Hash[nil, nil]
3032
end
3133
end

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class Cookies
5-
KNOWN_KEYS: Array[Symbol]
6-
75
def initialize: (Hash[Symbol, String | Integer | bool] cookies) -> void
86

97
def as_json: () -> Array[Hash[Symbol, untyped]]

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

Lines changed: 10 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
@cookies: Hash[Symbol, String | Integer]?
67

78
@reason: String
@@ -10,17 +11,26 @@ module Selenium
1011

1112
@headers: untyped
1213

14+
attr_reader body: untyped
1315
attr_accessor reason: String
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 cookies:(?Hash[Symbol, String | Integer]? set_cookie_headers) -> untyped
2026

2127
def credentials: (?username: untyped?, ?password: untyped?) -> untyped
2228

2329
def headers: () -> untyped
30+
31+
def provide_response: -> untyped
32+
33+
def set_cookie_headers: (?untyped? set_cookie_headers) -> untyped
2434
end
2535
end
2636
end

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,38 @@ 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(
155+
id: request_id,
156+
status: 200,
157+
headers: [
158+
{
159+
name: 'foo',
160+
value: {
161+
type: 'string',
162+
value: 'bar'
163+
}
164+
}
165+
],
166+
body: {
167+
type: 'string',
168+
value: '<html><head><title>Hello World!</title></head><body/></html>'
169+
}
170+
)
171+
end
172+
173+
driver.navigate.to url_for('formPage.html')
174+
source = driver.page_source
175+
expect(source).not_to include('There should be a form here:')
176+
expect(source).to include('Hello World!')
177+
end
178+
end
147179
end
148180
end
149181
end

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ module WebDriver
262262
end
263263
end
264264

265+
it 'adds a response handler that provides a response' do
266+
reset_driver!(web_socket_url: true) do |driver|
267+
network = described_class.new(driver)
268+
network.add_response_handler do |response|
269+
response.status = 200
270+
response.headers['foo'] = 'bar'
271+
response.body = '<html><head><title>Hello World!</title></head><body/></html>'
272+
response.provide_response
273+
end
274+
driver.navigate.to url_for('formPage.html')
275+
source = driver.page_source
276+
expect(source).not_to include('There should be a form here:')
277+
expect(source).to include('Hello World!')
278+
end
279+
end
280+
265281
it 'removes a response handler' do
266282
reset_driver!(web_socket_url: true) do |driver|
267283
network = described_class.new(driver)

0 commit comments

Comments
 (0)