diff --git a/rb/lib/selenium/webdriver/bidi/network.rb b/rb/lib/selenium/webdriver/bidi/network.rb index f3074fb6173b7..5cebd8967f2e1 100644 --- a/rb/lib/selenium/webdriver/bidi/network.rb +++ b/rb/lib/selenium/webdriver/bidi/network.rb @@ -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, &) diff --git a/rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb b/rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb index 08c67d9745f65..08e4b9c84a715 100644 --- a/rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb +++ b/rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb @@ -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 @@ -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 @@ -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 diff --git a/rb/sig/lib/selenium/webdriver/bidi/network.rbs b/rb/sig/lib/selenium/webdriver/bidi/network.rbs index 71d2588c65750..de3bbd1994d56 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network.rbs @@ -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 diff --git a/rb/sig/lib/selenium/webdriver/bidi/network/cookies.rbs b/rb/sig/lib/selenium/webdriver/bidi/network/cookies.rbs index bc5ceb7f1b680..5688a4e9b63cc 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network/cookies.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network/cookies.rbs @@ -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]] diff --git a/rb/sig/lib/selenium/webdriver/bidi/network/intercepted_response.rbs b/rb/sig/lib/selenium/webdriver/bidi/network/intercepted_response.rbs index ecba7f2bac96f..22263c22165b3 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network/intercepted_response.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network/intercepted_response.rbs @@ -2,6 +2,7 @@ module Selenium module WebDriver class BiDi class InterceptedResponse < InterceptedItem + @body: untyped @cookies: Hash[Symbol, String | Integer]? @reason: String @@ -10,10 +11,15 @@ 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 @@ -21,6 +27,10 @@ module Selenium 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 diff --git a/rb/sig/lib/selenium/webdriver/bidi/network/set_cookie_headers.rbs b/rb/sig/lib/selenium/webdriver/bidi/network/set_cookie_headers.rbs deleted file mode 100644 index 7d7ce657d33ac..0000000000000 --- a/rb/sig/lib/selenium/webdriver/bidi/network/set_cookie_headers.rbs +++ /dev/null @@ -1,11 +0,0 @@ -module Selenium - module WebDriver - class BiDi - class SetCookieHeaders - def initialize: (Hash[Symbol, untyped] set_cookie_headers) -> void - - def as_json: () -> Array[Hash[Symbol, untyped]] - end - end - end -end diff --git a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb index c02eee189450d..7c2b63f4770dc 100644 --- a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb @@ -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: 'Hello World!' + } + ) + 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 diff --git a/rb/spec/integration/selenium/webdriver/network_spec.rb b/rb/spec/integration/selenium/webdriver/network_spec.rb index 3e74c678f76d9..210e2d2e9d229 100644 --- a/rb/spec/integration/selenium/webdriver/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/network_spec.rb @@ -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 = 'Hello World!' + 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)