-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[rb] Add Bidi network commands for authentication and interception #14523
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
Merged
p0deje
merged 48 commits into
SeleniumHQ:trunk
from
aguspe:bidi_add_authentication_handler
Nov 11, 2024
Merged
Changes from 37 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
3c9e714
Start the network support for bidi
aguspe ce83b53
Add driver.network
aguspe 43dbec6
Update to use auth handler
aguspe 93d3695
Add remove and clear auth handlers
aguspe c8d734d
Update autoload for auth handler
aguspe 9562499
Update tests
aguspe e33f096
Update network
aguspe 816692e
first succesful intercept
aguspe ebac701
First test working
aguspe ef0093b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe e61eeee
Auth required working
aguspe 982717c
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe f00e6b9
improve continue with auth
aguspe 7686279
Add unit tests
aguspe 86d5a2c
Unit test and integration tests working better
aguspe 5cd8046
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 3cf68b6
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 8386295
Updating add intercept
aguspe bb1d45e
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe c307ba6
Add the response data type
aguspe 737a6e6
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe c6c3b24
Simplify network
aguspe e3f86ed
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe 72550e3
Add types
aguspe c7e4bca
All network tests working
aguspe 5012c85
All network tests working
aguspe cb1ef98
All network tests working
aguspe cfbfd37
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe f7c3f8d
add clear auth handlers
aguspe da15ccd
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe d2fafae
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 620ab24
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe a2d804c
remove constant and use instance variable instead
aguspe bf3b1f6
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe eb01d3a
Remove unit tests
aguspe 584b4ba
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 81bb508
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 3b8573b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 6d0843e
Update review comments
aguspe 63c4449
Remove unnecesary loads
aguspe d14087b
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe c7ab20c
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 613436f
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 23be2e6
Fixed linter issues
aguspe ff376b2
Merge remote-tracking branch 'origin/bidi_add_authentication_handler'…
aguspe f8a8400
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 9f6b084
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe 49f568f
Merge branch 'trunk' into bidi_add_authentication_handler
aguspe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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, &block) | ||
| event = EVENTS[event] if event.is_a?(Symbol) | ||
| @bidi.add_callback(event, &block) | ||
| end | ||
| end # Network | ||
| end # BiDi | ||
| end # WebDriver | ||
| end # Selenium |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -37,8 +37,6 @@ def camel_to_snake(camel_str) | |
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # BiDi | ||
| end # BiDi | ||
| end # WebDriver | ||
| end # Selenium | ||
This file contains hidden or 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 hidden or 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 hidden or 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_auth_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_auth_handler(id) | ||
aguspe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| intercept = @auth_callbacks[id] | ||
| @network.remove_intercept(intercept['intercept']) | ||
| @auth_callbacks.delete(id) | ||
| end | ||
|
|
||
| def clear_auth_handlers | ||
aguspe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @auth_callbacks.each_key { |id| remove_auth_handler(id) } | ||
| end | ||
| end # Network | ||
| end # WebDriver | ||
| end # Selenium | ||
This file contains hidden or 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 hidden or 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 hidden or 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_auth_handler: (String username, String password) -> String | ||
|
|
||
| def clear_auth_handlers: -> Hash[nil, nil] | ||
|
|
||
| def remove_auth_handler: (String id) -> nil | ||
| end | ||
| end | ||
| end |
65 changes: 65 additions & 0 deletions
65
rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
This file contains hidden or 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,65 @@ | ||
| # 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' | ||
| require_relative '../../../../../lib/selenium/webdriver/bidi/session' | ||
aguspe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require_relative '../../../../../lib/selenium/webdriver/bidi/network' | ||
|
|
||
| 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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.