Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ def initialize(bidi)
@bidi = bidi
end

def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :string)
url_patterns = url_patterns && pattern_type ? UrlPattern.format_pattern(url_patterns, pattern_type) : nil
def add_intercept(phases: [], contexts: nil, filters: nil)
@bidi.send_cmd('network.addIntercept',
phases: phases,
contexts: contexts,
urlPatterns: url_patterns)
urlPatterns: UrlPattern.format_pattern(filters))
end

def remove_intercept(intercept)
Expand Down
56 changes: 28 additions & 28 deletions rb/lib/selenium/webdriver/bidi/network/url_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ class BiDi
module UrlPattern
module_function

def format_pattern(url_patterns, pattern_type)
case pattern_type
when :string
to_url_string_pattern(url_patterns)
when :url
to_url_pattern(url_patterns)
else
raise ArgumentError, "Unknown pattern type: #{pattern_type}"
end
end
ALLOWED_KEYS = %i[protocol hostname port pathname search].freeze
WEB = [{ type: 'pattern', protocol: 'https' }.freeze, { type: 'pattern', protocol: 'http' }.freeze].freeze

def to_url_pattern(*url_patterns)
url_patterns.flatten.map do |url_pattern|
uri = URI.parse(url_pattern)
def format_pattern(filters)
patterns = Array(filters).flatten.compact
return WEB.dup if patterns.empty?

{
type: 'pattern',
protocol: uri.scheme || '',
hostname: uri.host || '',
port: uri.port.to_s,
pathname: uri.path || '',
search: uri.query || ''
}
patterns.each_with_object([]) do |pattern, array|
case pattern
when String, ::URI::Generic
array << { type: 'string', pattern: pattern.to_s }
when Hash
url_pattern = to_url_pattern(pattern)
if url_pattern.key?(:protocol)
array << url_pattern
else
array << url_pattern.merge(protocol: 'http')
array << url_pattern.merge(protocol: 'https')
end
else
raise TypeError, "pattern must be a String, URI or a Hash of keys: #{ALLOWED_KEYS.join(", ")}"
end
end

end

def to_url_string_pattern(*url_patterns)
url_patterns.flatten.map do |url_pattern|
{
type: 'string',
pattern: url_pattern
}
end
private

def to_url_pattern(pattern)
unknown = pattern.keys - ALLOWED_KEYS
raise ArgumentError, "Unknown keys in pattern hash: #{unknown.inspect}" unless unknown.empty?

pattern.slice(*ALLOWED_KEYS).merge(type: 'pattern')
end
end
end # BiDi
Expand Down
19 changes: 8 additions & 11 deletions rb/lib/selenium/webdriver/common/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def clear_handlers
callbacks.each_key { |id| remove_handler(id) }
end

def add_authentication_handler(username = nil, password = nil, *filter, pattern_type: nil, &block)
def add_authentication_handler(username = nil, password = nil, *filters, &block)
selected_block =
if username && password
proc { |auth| auth.authenticate(username, password) }
Expand All @@ -56,38 +56,35 @@ def add_authentication_handler(username = nil, password = nil, *filter, pattern_
:auth_required,
BiDi::Network::PHASES[:auth_required],
BiDi::InterceptedAuth,
filter,
pattern_type: pattern_type,
Array(filters).flatten,
&selected_block
)
end

def add_request_handler(*filter, pattern_type: nil, &block)
def add_request_handler(*filters, &block)
add_handler(
:before_request,
BiDi::Network::PHASES[:before_request],
BiDi::InterceptedRequest,
filter,
pattern_type: pattern_type,
Array(filters).flatten,
&block
)
end

def add_response_handler(*filter, pattern_type: nil, &block)
def add_response_handler(*filters, &block)
add_handler(
:response_started,
BiDi::Network::PHASES[:response_started],
BiDi::InterceptedResponse,
filter,
pattern_type: pattern_type,
Array(filters).flatten,
&block
)
end

private

def add_handler(event_type, phase, intercept_type, filter, pattern_type: nil)
intercept = network.add_intercept(phases: [phase], url_patterns: [filter].flatten, pattern_type: pattern_type)
def add_handler(event_type, phase, intercept_type, filters)
intercept = network.add_intercept(phases: [phase], filters: filters)
callback_id = network.on(event_type) do |event|
request = event['request']
intercepted_item = intercept_type.new(network, request)
Expand Down
4 changes: 2 additions & 2 deletions rb/sig/lib/selenium/webdriver/bidi/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Selenium

def initialize: (BiDi bidi) -> void

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

def cancel_auth: -> Hash[nil, nil]

Expand All @@ -22,7 +22,7 @@ module Selenium

def fail_request: -> Hash[nil, nil]

def remove_intercept: (String intercept) -> Hash[nil, nil]
def remove_intercept: (String? intercept) -> Hash[nil, nil]

def continue_with_auth: (Integer request_id, String username, String password) -> Hash[nil, nil]

Expand Down
8 changes: 5 additions & 3 deletions rb/sig/lib/selenium/webdriver/bidi/network/url_pattern.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Selenium
module WebDriver
class BiDi
module UrlPattern
def self?.format_pattern: (Array[String] | String url_patterns, Symbol pattern_type) -> Array[Hash[Symbol, String]]
ALLOWED_KEYS: Array[Symbol]

def self?.to_url_pattern: (*String url_patterns) -> Array[Hash[Symbol, String]]
WEB: Array[Hash[Symbol, String]]

def self?.to_url_string_pattern: (*String url_patterns) -> Array[Hash[Symbol, String]]
def self?.format_pattern: (nil | Array[String] | Array[URI] | Array[Hash[Symbol, String]]) -> Array[Hash[Symbol, String]]

def self?.to_url_pattern: (Hash[Symbol, String]) -> Hash[Symbol, String]
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions rb/sig/lib/selenium/webdriver/common/network.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ module Selenium

alias bidi network

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

def remove_handler: (Numeric id) -> untyped

def clear_handlers: () -> untyped

def add_authentication_handler: (?String? username, ?String? password, *String filter, ?pattern_type: Symbol?) { (?) -> untyped } -> untyped
def add_authentication_handler: (?String? username, ?String? password, *(String | Hash[Symbol, String])?) { (BiDi::InterceptedAuth) -> void } -> Hash[String, String]

def add_request_handler: (*String filter, ?pattern_type: Symbol?) -> Hash[String, String]
def add_request_handler: (*(String | Hash[Symbol, String])?) { (BiDi::InterceptedRequest) -> void } -> Hash[String, String]

def add_response_handler: (*String filter, ?pattern_type: Symbol?) -> Hash[String, String]
def add_response_handler: (*(String | Hash[Symbol, String])?) { (BiDi::InterceptedResponse) -> void } -> Hash[String, String]

private

def add_handler: (Symbol event_type, String phase, BiDi::InterceptedRequest | BiDi::InterceptedAuth | BiDi::InterceptedResponse intercept_type, Array[String] filter, ?pattern_type: Symbol?) { (untyped) -> untyped } -> untyped
def add_handler: (Symbol event_type, String phase, Class intercept_type, Array[String | Hash[Symbol, String]] filters) { (untyped) -> untyped } -> untyped
end
end
end
6 changes: 2 additions & 4 deletions rb/spec/integration/selenium/webdriver/bidi/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ class BiDi
it 'adds an intercept with a default pattern type' do
network = described_class.new(driver.bidi)
pattern = 'http://localhost:4444/formPage.html'
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], url_patterns: pattern)
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], filters: [pattern])
expect(intercept).not_to be_nil
end

it 'adds an intercept with a url pattern' do
network = described_class.new(driver.bidi)
pattern = 'http://localhost:4444/formPage.html'
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]],
url_patterns: pattern,
pattern_type: :url)
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], filters: [pattern])
expect(intercept).not_to be_nil
end

Expand Down
6 changes: 3 additions & 3 deletions rb/spec/integration/selenium/webdriver/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module WebDriver
it 'adds an auth handler with a pattern type' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_authentication_handler(username, password, url_for('basicAuth'), pattern_type: :url)
network.add_authentication_handler(username, password, url_for('basicAuth'))
driver.navigate.to url_for('basicAuth')
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
expect(network.callbacks.count).to be 1
Expand Down Expand Up @@ -135,7 +135,7 @@ module WebDriver
it 'adds a request handler with a pattern type' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_request_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
network.add_request_handler(url_for('formPage.html'), &:continue)
driver.navigate.to url_for('formPage.html')
expect(driver.current_url).to eq(url_for('formPage.html'))
expect(network.callbacks.count).to be 1
Expand Down Expand Up @@ -230,7 +230,7 @@ module WebDriver
it 'adds a response handler with a pattern type' do
reset_driver!(web_socket_url: true) do |driver|
network = described_class.new(driver)
network.add_response_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
network.add_response_handler(url_for('formPage.html'), &:continue)
driver.navigate.to url_for('formPage.html')
expect(driver.current_url).to eq(url_for('formPage.html'))
expect(network.callbacks.count).to be 1
Expand Down
Loading