Skip to content

Commit

Permalink
[rb] Add Bidi network commands for authentication and interception (S…
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspe authored and jkim2492 committed Nov 17, 2024
1 parent a6a405e commit d93248f
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 2 deletions.
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, &)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback(event, &)
end
end # Network
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
62 changes: 62 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,62 @@
# 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
58 changes: 58 additions & 0 deletions rb/spec/integration/selenium/webdriver/network_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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
describe Network, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
only: {browser: %i[chrome edge firefox]} do
let(:username) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first }
let(:password) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last }

it 'adds an auth handler' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_authentication_handler(username, password)
expect(network.auth_callbacks.count).to be 1
end
end

it 'removes an auth handler' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
id = network.add_authentication_handler(username, password)
network.remove_authentication_handler(id)
expect(network.auth_callbacks.count).to be 0
end
end

it 'clears all auth handlers' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_authentication_handler(username, password)
network.add_authentication_handler(username, password)
network.clear_authentication_handlers
expect(network.auth_callbacks.count).to be 0
end
end
end
end
end

0 comments on commit d93248f

Please sign in to comment.