Skip to content

Commit

Permalink
[rb][BiDi] Add support for provide response command (#15080)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aguspe and pujagani authored Feb 5, 2025
1 parent 2357514 commit 73607ee
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
12 changes: 12 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def continue_response(**args)
)
end

def provide_response(**args)
@bidi.send_cmd(
'network.provideResponse',
request: args[:id],
body: args[:body],
cookies: args[:cookies],
headers: args[:headers],
reasonPhrase: args[:reason],
statusCode: args[:status]
)
end

def on(event, &)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback(event, &)
Expand Down
26 changes: 24 additions & 2 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ module Selenium
module WebDriver
class BiDi
class InterceptedResponse < InterceptedItem
attr_accessor :reason
attr_accessor :reason, :status
attr_reader :body

def initialize(network, request)
super
@reason = nil
@status = nil
@body = nil
end

def continue
Expand All @@ -38,7 +41,19 @@ def continue
cookies: cookies.as_json,
headers: headers.as_json,
credentials: credentials.as_json,
reason: reason
reason: reason,
status: status
)
end

def provide_response
network.provide_response(
id: id,
cookies: cookies.as_json,
headers: headers.as_json,
body: body,
reason: reason,
status: status
)
end

Expand All @@ -53,6 +68,13 @@ def headers
def cookies(cookies = {})
@cookies ||= Cookies.new(cookies)
end

def body=(value)
@body = {
type: 'string',
value: value.to_json
}
end
end
end # BiDi
end # WebDriver
Expand Down
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/bidi/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module Selenium

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

def provide_response: -> Hash[nil, nil]

def on: (Symbol event) { (?) -> untyped } -> Hash[nil, nil]
end
end
Expand Down
2 changes: 0 additions & 2 deletions rb/sig/lib/selenium/webdriver/bidi/network/cookies.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Selenium
module WebDriver
class BiDi
class Cookies
KNOWN_KEYS: Array[Symbol]

def initialize: (Hash[Symbol, String | Integer | bool] cookies) -> void

def as_json: () -> Array[Hash[Symbol, untyped]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Selenium
module WebDriver
class BiDi
class InterceptedResponse < InterceptedItem
@body: untyped
@cookies: Hash[Symbol, String | Integer]?

@reason: String
Expand All @@ -10,17 +11,26 @@ module Selenium

@headers: untyped

attr_reader body: untyped
attr_accessor reason: String

attr_accessor status: Integer?

def initialize: (untyped network, untyped request) -> void

def body=: -> untyped

def continue: () -> untyped

def cookies:(?Hash[Symbol, String | Integer]? set_cookie_headers) -> untyped

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

def headers: () -> untyped

def provide_response: -> untyped

def set_cookie_headers: (?untyped? set_cookie_headers) -> untyped
end
end
end
Expand Down
11 changes: 0 additions & 11 deletions rb/sig/lib/selenium/webdriver/bidi/network/set_cookie_headers.rbs

This file was deleted.

32 changes: 32 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ class BiDi
expect(driver.find_element(name: 'login')).to be_displayed
end
end

it 'provides response' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver.bidi)
network.add_intercept(phases: [described_class::PHASES[:response_started]])
network.on(:response_started) do |event|
request_id = event['request']['request']
network.provide_response(
id: request_id,
status: 200,
headers: [
{
name: 'foo',
value: {
type: 'string',
value: 'bar'
}
}
],
body: {
type: 'string',
value: '<html><head><title>Hello World!</title></head><body/></html>'
}
)
end

driver.navigate.to url_for('formPage.html')
source = driver.page_source
expect(source).not_to include('There should be a form here:')
expect(source).to include('Hello World!')
end
end
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions rb/spec/integration/selenium/webdriver/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ module WebDriver
end
end

it 'adds a response handler that provides a response' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_response_handler do |response|
response.status = 200
response.headers['foo'] = 'bar'
response.body = '<html><head><title>Hello World!</title></head><body/></html>'
response.provide_response
end
driver.navigate.to url_for('formPage.html')
source = driver.page_source
expect(source).not_to include('There should be a form here:')
expect(source).to include('Hello World!')
end
end

it 'removes a response handler' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
Expand Down

0 comments on commit 73607ee

Please sign in to comment.