forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rb] Add Bidi network commands for authentication and interception (S…
- Loading branch information
Showing
11 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |