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 commands for authentication and interception #14523

Open
wants to merge 42 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3c9e714
Start the network support for bidi
aguspe Sep 20, 2024
ce83b53
Add driver.network
aguspe Sep 20, 2024
43dbec6
Update to use auth handler
aguspe Sep 20, 2024
93d3695
Add remove and clear auth handlers
aguspe Sep 20, 2024
c8d734d
Update autoload for auth handler
aguspe Sep 20, 2024
9562499
Update tests
aguspe Sep 20, 2024
e33f096
Update network
aguspe Sep 27, 2024
816692e
first succesful intercept
aguspe Sep 27, 2024
ebac701
First test working
aguspe Sep 27, 2024
ef0093b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Sep 28, 2024
e61eeee
Auth required working
aguspe Sep 29, 2024
982717c
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe Sep 29, 2024
f00e6b9
improve continue with auth
aguspe Sep 30, 2024
7686279
Add unit tests
aguspe Oct 3, 2024
86d5a2c
Unit test and integration tests working better
aguspe Oct 3, 2024
5cd8046
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 3, 2024
3cf68b6
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 7, 2024
8386295
Updating add intercept
aguspe Oct 7, 2024
bb1d45e
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 8, 2024
c307ba6
Add the response data type
aguspe Oct 8, 2024
737a6e6
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 9, 2024
c6c3b24
Simplify network
aguspe Oct 11, 2024
e3f86ed
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe Oct 11, 2024
72550e3
Add types
aguspe Oct 11, 2024
c7e4bca
All network tests working
aguspe Oct 11, 2024
5012c85
All network tests working
aguspe Oct 11, 2024
cb1ef98
All network tests working
aguspe Oct 11, 2024
cfbfd37
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 11, 2024
f7c3f8d
add clear auth handlers
aguspe Oct 11, 2024
da15ccd
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe Oct 11, 2024
d2fafae
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 12, 2024
620ab24
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Oct 16, 2024
a2d804c
remove constant and use instance variable instead
aguspe Oct 16, 2024
bf3b1f6
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe Oct 16, 2024
eb01d3a
Remove unit tests
aguspe Oct 16, 2024
584b4ba
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Nov 2, 2024
81bb508
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Nov 3, 2024
3b8573b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Nov 4, 2024
6d0843e
Update review comments
aguspe Nov 4, 2024
63c4449
Remove unnecesary loads
aguspe Nov 4, 2024
d14087b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Nov 4, 2024
c7ab20c
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe Nov 4, 2024
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
30 changes: 28 additions & 2 deletions java/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/bidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BiDi
autoload :LogHandler, 'selenium/webdriver/bidi/log_handler'
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
autoload :Struct, 'selenium/webdriver/bidi/struct'
autoload :Network, 'selenium/webdriver/bidi/network'

def initialize(url:)
@ws = WebSocketConnection.new(url: url)
Expand Down
70 changes: 70 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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 Network
EVENTS = {
before_request: 'network.beforeRequestSent',
response_started: 'network.responseStarted',
response_completed: 'network.responseCompleted',
auth_required: 'network.authRequired',
FETCH_ERROR: 'network.fetchError'
}.freeze

PHASES = {
before_request: 'beforeRequestSent',
response_started: 'responseStarted',
auth_required: 'authRequired'
}.freeze

def initialize(bidi)
@bidi = bidi
end

def add_intercept(phases: [], contexts: nil, url_patterns: nil)
@bidi.send_cmd('network.addIntercept', phases: phases, contexts: contexts, urlPatterns: url_patterns)
end

def remove_intercept(intercept)
@bidi.send_cmd('network.removeIntercept', intercept: intercept)
end

def continue_with_auth(request_id, username, password)
@bidi.send_cmd(
'network.continueWithAuth',
'request' => request_id,
'action' => 'provideCredentials',
'credentials' => {
'type' => 'password',
'username' => username,
'password' => password
}
)
end

def on(event, &block)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback(event, &block)
end
end # Network
end # BiDi
end # WebDriver
end # Selenium
4 changes: 1 addition & 3 deletions rb/lib/selenium/webdriver/bidi/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ def camel_to_snake(camel_str)
end
end
end
end

# BiDi
end # BiDi
end # WebDriver
end # Selenium
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@
require 'selenium/webdriver/common/script'
require 'selenium/webdriver/common/fedcm/account'
require 'selenium/webdriver/common/fedcm/dialog'
require 'selenium/webdriver/common/network'
9 changes: 9 additions & 0 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ def add_virtual_authenticator(options)
bridge.add_virtual_authenticator(options)
end

#
# @return [Network]
# @see Network
#

def network
@network ||= WebDriver::Network.new(bridge)
end

#-------------------------------- sugar --------------------------------

#
Expand Down
52 changes: 52 additions & 0 deletions rb/lib/selenium/webdriver/common/network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 Network
attr_reader :auth_callbacks

def initialize(bridge)
@network = BiDi::Network.new(bridge.bidi)
@auth_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
@auth_callbacks[auth_id] = intercept

auth_id
end

def remove_authentication_handler(id)
intercept = @auth_callbacks[id]
@network.remove_intercept(intercept['intercept'])
@auth_callbacks.delete(id)
end

def clear_authentication_handlers
@auth_callbacks.each_key { |id| remove_authentication_handler(id) }
end
end # Network
end # WebDriver
end # Selenium
23 changes: 23 additions & 0 deletions rb/sig/lib/selenium/webdriver/bidi/network.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Selenium
module WebDriver
class BiDi
class Network
@bidi: BiDi

EVENTS: Hash[Symbol, String]

PHASES: Hash[Symbol, String]

def initialize: (BiDi bidi) -> void

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

def remove_intercept: (String intercept) -> untyped

def continue_with_auth: (String request_id, String username, String password) -> untyped

def on: (Symbol event) { (?) -> untyped } -> untyped
end
end
end
end
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/common/driver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module Selenium

def inspect: () -> String

def network: -> Network

def status: () -> Hash[untyped, untyped]

def navigate: () -> Navigation
Expand Down
17 changes: 17 additions & 0 deletions rb/sig/lib/selenium/webdriver/common/network.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Selenium
module WebDriver
class Network
@network: BiDi::Network

attr_reader auth_callbacks: Hash[String, String]

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

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

def clear_authentication_handlers: -> Hash[nil, nil]

def remove_authentication_handler: (String id) -> nil
end
end
end
63 changes: 63 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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.

require_relative '../spec_helper'

module Selenium
module WebDriver
class BiDi
describe Network, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
only: {browser: %i[chrome edge firefox]} do

it 'adds an intercept' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver.bidi)
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])
expect(intercept).not_to be_nil
end
end

it 'removes an intercept' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver.bidi)
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])
expect(network.remove_intercept(intercept['intercept'])).to be_empty
end
end

it 'continues with auth' do
username = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first
password = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver.bidi)
network.add_intercept(phases: [described_class::PHASES[:auth_required]])
network.on(:auth_required) do |event|
request_id = event['requestId']
network.continue_with_auth(request_id, username, password)
end

driver.navigate.to url_for('basicAuth')
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
end
end
end
end
end
end
Loading