Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rb] Add Bidi Network Response Handler #14900

Merged
merged 63 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
7ef9d83
Add response handler
aguspe Dec 14, 2024
2f2012a
Update auth handlers and improve the :on method
aguspe Dec 18, 2024
487a6e3
Merge branch 'trunk' into add_response_handler
aguspe Dec 18, 2024
28cf363
Request and response working as expected
aguspe Dec 18, 2024
314b458
Add test for continue without auth and cancel auth
aguspe Dec 18, 2024
305135e
Finish implementation
aguspe Dec 18, 2024
ff2ba0d
Correct rubocop offenses
aguspe Dec 18, 2024
0b41266
Add alias for user to do network.bidi instead of network.network
aguspe Dec 18, 2024
25c201b
Fix rust file causing formatting error
aguspe Dec 18, 2024
92ea3ef
Merge branch 'trunk' into add_response_handler
aguspe Dec 19, 2024
7e22239
Handle requests and responses in block
aguspe Dec 22, 2024
ad82e86
Merge remote-tracking branch 'origin/add_response_handler' into add_r…
aguspe Dec 22, 2024
b2cea1d
Merge branch 'trunk' into add_response_handler
pujagani Dec 23, 2024
ae80832
Add ability to pass handlers to each different intercepted element
aguspe Dec 23, 2024
b4a4fe7
Merge remote-tracking branch 'origin/add_response_handler' into add_r…
aguspe Dec 23, 2024
13a0912
Merge branch 'trunk' into add_response_handler
aguspe Dec 23, 2024
c21f643
Headers working
aguspe Jan 1, 2025
f390699
Merge branch 'trunk' into add_response_handler
aguspe Jan 1, 2025
69b5690
All tests passing and signatures simplified
aguspe Jan 1, 2025
bb1ece5
remove unnecessary changes
aguspe Jan 1, 2025
996c527
remove unnecessary changes
aguspe Jan 1, 2025
f8f3f7f
Merge remote-tracking branch 'origin/add_response_handler' into add_r…
aguspe Jan 1, 2025
f2f9e01
Added credentials and set cookie header to intercepted response
aguspe Jan 2, 2025
38d0b9a
Merge branch 'trunk' into add_response_handler
aguspe Jan 2, 2025
71f5fa6
Fix rubocop issues
aguspe Jan 2, 2025
9c23adf
Merge branch 'trunk' into add_response_handler
aguspe Jan 3, 2025
d9e0ffc
Make auth handler more user friendly
aguspe Jan 3, 2025
87dc1e6
Add filtering an url pattern support
aguspe Jan 4, 2025
9612939
Merge branch 'trunk' into add_response_handler
aguspe Jan 4, 2025
7c49b93
Fix formatting issues
aguspe Jan 4, 2025
b1e14b6
Merge remote-tracking branch 'origin/add_response_handler' into add_r…
aguspe Jan 4, 2025
3772844
Modify tests to avoid element related failures on firefox
aguspe Jan 4, 2025
0515131
Fix styling of comments
aguspe Jan 4, 2025
eb3f378
Improve types and hash consistency
aguspe Jan 5, 2025
ac43ca2
Merge branch 'trunk' into add_response_handler
aguspe Jan 6, 2025
abb697d
Merge branch 'trunk' into add_response_handler
aguspe Jan 8, 2025
edc5508
Merge branch 'trunk' into add_response_handler
aguspe Jan 10, 2025
296105e
Update all the places that use 9.4.8.0
aguspe Jan 10, 2025
51e8f84
Merge branch 'trunk' into add_response_handler
aguspe Jan 11, 2025
c7552aa
Serialize request values
aguspe Jan 11, 2025
ab0f4c2
Refactor cookies, headers and set cookie headers
aguspe Jan 11, 2025
fa0c87a
Simplify serialization
aguspe Jan 11, 2025
bbd7bcc
Add extra type support
aguspe Jan 11, 2025
acce734
Improve typing
aguspe Jan 12, 2025
2197e67
Merge branch 'trunk' into add_response_handler
aguspe Jan 12, 2025
3ac73c1
Merge branch 'trunk' into add_response_handler
aguspe Jan 12, 2025
a98eafd
Merge branch 'trunk' into add_response_handler
aguspe Jan 13, 2025
0484ee2
Merge branch 'trunk' into add_response_handler
aguspe Jan 13, 2025
7760eb3
Merge branch 'trunk' into add_response_handler
aguspe Jan 13, 2025
5596a53
Merge branch 'trunk' into add_response_handler
aguspe Jan 13, 2025
71da231
Merge branch 'trunk' into add_response_handler
aguspe Jan 14, 2025
15ef477
transform serialize to as_json
aguspe Jan 15, 2025
c35bdc2
Merge remote-tracking branch 'origin/add_response_handler' into add_r…
aguspe Jan 15, 2025
1aa8835
Merge branch 'trunk' into add_response_handler
aguspe Jan 15, 2025
75e89a9
Simplify cookies and add extra unit tests
aguspe Jan 18, 2025
e8f35c4
Merge branch 'trunk' into add_response_handler
aguspe Jan 18, 2025
ca3a0fe
Merge branch 'trunk' into add_response_handler
aguspe Jan 18, 2025
8e27704
Simplify cookies and add extra unit tests
aguspe Jan 19, 2025
0daec6d
Merge branch 'trunk' into add_response_handler
aguspe Jan 19, 2025
00dd4aa
Merge branch 'trunk' into add_response_handler
aguspe Jan 20, 2025
14853b6
Merge branch 'trunk' into add_response_handler
aguspe Jan 22, 2025
68d28e1
Merge branch 'trunk' into add_response_handler
aguspe Jan 23, 2025
fe2bf4a
Merge branch 'trunk' into add_response_handler
aguspe Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rb/lib/selenium/webdriver/bidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class BiDi
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
autoload :Struct, 'selenium/webdriver/bidi/struct'
autoload :Network, 'selenium/webdriver/bidi/network'
autoload :InterceptedRequest, 'selenium/webdriver/bidi/network/intercepted_request'
autoload :InterceptedResponse, 'selenium/webdriver/bidi/network/intercepted_response'
autoload :InterceptedAuth, 'selenium/webdriver/bidi/network/intercepted_auth'
autoload :InterceptedItem, 'selenium/webdriver/bidi/network/intercepted_item'

def initialize(url:)
@ws = WebSocketConnection.new(url: url)
Expand Down
31 changes: 30 additions & 1 deletion rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,25 @@ def continue_with_auth(request_id, username, password)
)
end

def continue_without_auth(request_id)
@bidi.send_cmd(
'network.continueWithAuth',
'request' => request_id,
'action' => 'default'
)
end

def cancel_auth(request_id)
@bidi.send_cmd(
'network.continueWithAuth',
'request' => request_id,
'action' => 'cancel'
)
end

def continue_with_request(**args)
aguspe marked this conversation as resolved.
Show resolved Hide resolved
@bidi.send_cmd(
'network.continueWithRequest',
'network.continueRequest',
request: args[:request_id],
'body' => args[:body],
'cookies' => args[:cookies],
Expand All @@ -72,9 +88,22 @@ def continue_with_request(**args)
)
end

def continue_with_response(**args)
aguspe marked this conversation as resolved.
Show resolved Hide resolved
@bidi.send_cmd(
'network.continueResponse',
request: args[:response_id],
aguspe marked this conversation as resolved.
Show resolved Hide resolved
'body' => args[:body],
'cookies' => args[:cookies],
'credentials' => args[:credentials],
'headers' => args[:headers],
'status' => args[:status]
)
end

def on(event, &)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback(event, &)
@bidi.session.subscribe(event)
end
end # Network
end # BiDi
Expand Down
39 changes: 39 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class BiDi
class InterceptedAuth < InterceptedItem
def authenticate(username, password)
network.continue_with_auth(id, username, password)
end

def skip
network.continue_without_auth(id)
end

def cancel
network.cancel_auth(id)
end
end
end # BiDi
end # WebDriver
end # Selenium

46 changes: 46 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class BiDi
class InterceptedItem
attr_reader :network, :request

def initialize(network, request)
@network = network
@request = request
end

def id
@id ||= @request['request']
end

def headers
request['headers']
end

def headers=(new_headers)
request['headers'] = new_headers
end
end
end # BiDi
end # WebDriver
end # Selenium

31 changes: 31 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class BiDi
class InterceptedRequest < InterceptedItem
def continue
network.continue_with_request(request_id: id)
end
end
end # BiDi
end # WebDriver
end # Selenium

31 changes: 31 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class BiDi
class InterceptedResponse < InterceptedItem
def continue
network.continue_with_response(response_id: id)
end
end
end # BiDi
end # WebDriver
end # Selenium

56 changes: 33 additions & 23 deletions rb/lib/selenium/webdriver/common/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,57 @@
# specific language governing permissions and limitations
# under the License.

require 'forwardable'

module Selenium
module WebDriver
class Network
attr_reader :callbacks
extend Forwardable

attr_reader :callbacks, :network
alias bidi network

def_delegators :network, :continue_with_auth, :continue_with_request, :continue_with_response

def initialize(bridge)
@network = BiDi::Network.new(bridge.bidi)
@callbacks = {}
end

def add_authentication_handler(username, password)
intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:auth_required]])
auth_id = @network.on(:auth_required) do |event|
request_id = event['requestId']
@network.continue_with_auth(request_id, username, password)
end
@callbacks[auth_id] = intercept
def remove_handler(id)
intercept = callbacks[id]
network.remove_intercept(intercept['intercept'])
callbacks.delete(id)
end

auth_id
def clear_handlers
callbacks.each_key { |id| remove_handler(id) }
end

def remove_handler(id)
intercept = @callbacks[id]
@network.remove_intercept(intercept['intercept'])
@callbacks.delete(id)
def add_authentication_handler(&)
add_handler(:auth_required, BiDi::Network::PHASES[:auth_required], BiDi::InterceptedAuth, &)
end

def clear_handlers
@callbacks.each_key { |id| remove_handler(id) }
def add_request_handler(&)
add_handler(:before_request, BiDi::Network::PHASES[:before_request], BiDi::InterceptedRequest, &)
end

def add_request_handler
intercept = @network.add_intercept(phases: [BiDi::Network::PHASES[:before_request]])
request_id = @network.on(:before_request) do |event|
request_id = event['requestId']
@network.continue_with_request(request_id: request_id)
end
def add_response_handler(&)
add_handler(:response_started, BiDi::Network::PHASES[:response_started], BiDi::InterceptedResponse, &)
end

private

@callbacks[request_id] = intercept
def add_handler(event_type, phase, intercept_type, &block)
intercept = network.add_intercept(phases: [phase])
callback_id = network.on(event_type) do |event|
request = event['request']
intercepted_item = intercept_type.new(network, request)
block.call(intercepted_item)
end

request_id
callbacks[callback_id] = intercept
callback_id
end
end # Network
end # WebDriver
Expand Down
2 changes: 1 addition & 1 deletion rb/sig/lib/selenium/webdriver/bidi.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Selenium

def initialize: (url: String) -> void

def add_callback: (String event) { () -> void } -> Integer
def add_callback: (String | Symbol event) { () -> void } -> Integer

def close: () -> nil

Expand Down
8 changes: 7 additions & 1 deletion rb/sig/lib/selenium/webdriver/bidi/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ module Selenium

def add_intercept: (?phases: Array[String], ?contexts: BrowsingContext?, ?url_patterns: untyped?) -> Hash[String, String]

def continue_with_request: -> untyped
def cancel_auth: -> untyped

def continue_with_request: (**untyped args) -> untyped

def continue_with_response: (**untyped args) -> untyped

def continue_without_auth: -> untyped

def remove_intercept: (String intercept) -> untyped

Expand Down
8 changes: 8 additions & 0 deletions rb/sig/lib/selenium/webdriver/common/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ module Selenium

attr_reader callbacks: Hash[String, String]

attr_reader network: untyped

def initialize: (Remote::Bridge bridge) -> void

def add_authentication_handler: (String username, String password) -> String

def add_request_handler: -> Integer

def add_response_handler: -> String

alias bidi network

def clear_handlers: -> Hash[nil, nil]

def fetch_id: -> String

def remove_handler: (Integer id) -> nil
end
end
Expand Down
Loading
Loading