Skip to content

[rb] Use rbs trace #15686

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

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
2 changes: 2 additions & 0 deletions rb/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem 'activesupport', '~> 7.0', require: false, platforms: %i[mri mingw x64_mingw
gem 'curb', '~> 1.0.5', require: false, platforms: %i[mri mingw x64_mingw]
gem 'debug', '~> 1.7', require: false, platforms: %i[mri mingw x64_mingw]
gem 'steep', '~> 1.5.0', require: false, platforms: %i[mri mingw x64_mingw]

gem 'rbs-trace', '~> 0.5.1', require: false, platforms: %i[mri mingw x64_mingw]
4 changes: 4 additions & 0 deletions rb/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ GEM
ffi (~> 1.0)
rbs (3.9.2)
logger
rbs-trace (0.5.1)
prism (>= 0.3.0)
rbs (>= 3.5.0)
rchardet (1.9.0)
rdoc (6.13.1)
psych (>= 4.0.0)
Expand Down Expand Up @@ -198,6 +201,7 @@ DEPENDENCIES
git (~> 1.19)
rack (~> 2.0)
rake (~> 13.0)
rbs-trace (~> 0.5.1)
rspec (~> 3.0)
rubocop (~> 1.75)
rubocop-performance (~> 1.25)
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/devtools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module DevTools
class << self
attr_accessor :version

# @rbs () -> void
def load_version
require "selenium/devtools/v#{@version}"
rescue LoadError
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def stop
@log_file&.close
end

# @rbs () -> String
def webdriver_url
"http://#{@host}:#{@port}/wd/hub"
end
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def self.for(*args)
# @return [Logger]
#

# @rbs (**nil) -> Selenium::WebDriver::Logger
def self.logger(**opts)
level = $DEBUG || ENV.key?('DEBUG') ? :debug : :info
@logger ||= WebDriver::Logger.new('Selenium', default_level: level, **opts)
Expand Down
3 changes: 3 additions & 0 deletions rb/lib/selenium/webdriver/atoms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
module Selenium
module WebDriver
module Atoms
# @rbs (Symbol) -> String
def atom_script(function_name)
format("/* #{function_name} */return (%<atom>s).apply(null, arguments)",
atom: read_atom(function_name))
end

private

# @rbs (Symbol) -> String
def read_atom(function)
File.read(File.expand_path("../atoms/#{function}.js", __FILE__))
end

# @rbs (Symbol, *Selenium::WebDriver::Element | String | Hash[untyped, untyped]) -> (String | Array[untyped])
def execute_atom(function_name, *arguments)
execute_script(atom_script(function_name), *arguments)
end
Expand Down
8 changes: 8 additions & 0 deletions rb/lib/selenium/webdriver/bidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,37 @@ class BiDi
autoload :InterceptedAuth, 'selenium/webdriver/bidi/network/intercepted_auth'
autoload :InterceptedItem, 'selenium/webdriver/bidi/network/intercepted_item'

# @rbs (url: String) -> void
def initialize(url:)
@ws = WebSocketConnection.new(url: url)
end

# @rbs () -> nil
def close
@ws.close
end

# @rbs () -> Hash[untyped, untyped]
def callbacks
@ws.callbacks
end

# @rbs (String) -> void
def add_callback(event, &block)
@ws.add_callback(event, &block)
end

# @rbs (String, Integer) -> void
def remove_callback(event, id)
@ws.remove_callback(event, id)
end

# @rbs () -> Selenium::WebDriver::BiDi::Session
def session
@session ||= Session.new(self)
end

# @rbs (String, **String | String | Integer | String | bool) -> Hash[untyped, untyped]
def send_cmd(method, **params)
data = {method: method, params: params.compact}
message = @ws.send_cmd(**data)
Expand All @@ -63,6 +70,7 @@ def send_cmd(method, **params)
message['result']
end

# @rbs (Hash[untyped, untyped]) -> String
def error_message(message)
"#{message['error']}: #{message['message']}\n#{message['stacktrace']}"
end
Expand Down
9 changes: 9 additions & 0 deletions rb/lib/selenium/webdriver/bidi/browsing_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BrowsingContext
}.freeze

# TODO: store current window handle in bridge object instead of always calling it
# @rbs (Selenium::WebDriver::Remote::BiDiBridge) -> void
def initialize(bridge)
@bridge = bridge
@bidi = @bridge.bidi
Expand All @@ -44,6 +45,7 @@ def initialize(bridge)
# @param url [String] The URL to navigate to.
# @param context_id [String, NilClass] The ID of the browsing context to navigate in.
# Defaults to the window handle of the current context.
# @rbs (String, ?context_id: nil) -> Hash[untyped, untyped]
def navigate(url, context_id: nil)
context_id ||= @bridge.window_handle
@bidi.send_cmd('browsingContext.navigate', context: context_id, url: url, wait: @readiness)
Expand All @@ -55,6 +57,7 @@ def navigate(url, context_id: nil)
# Positive values go forwards, negative values go backwards.
# @param context_id [String, NilClass] The ID of the context to traverse.
# Defaults to the window handle of the current context.
# @rbs (Integer, ?context_id: nil) -> Hash[untyped, untyped]
def traverse_history(delta, context_id: nil)
context_id ||= @bridge.window_handle
@bidi.send_cmd('browsingContext.traverseHistory', context: context_id, delta: delta)
Expand All @@ -65,6 +68,7 @@ def traverse_history(delta, context_id: nil)
# Defaults to the window handle of the current context.
# @param [Boolean] ignore_cache Whether to bypass the cache when reloading.
# Defaults to false.
# @rbs (?context_id: nil, ?ignore_cache: bool) -> Hash[untyped, untyped]
def reload(context_id: nil, ignore_cache: false)
context_id ||= @bridge.window_handle
params = {context: context_id, ignore_cache: ignore_cache, wait: @readiness}
Expand All @@ -75,6 +79,7 @@ def reload(context_id: nil, ignore_cache: false)
#
# @param [String] context_id The ID of the context to close.
# Defaults to the window handle of the current context.
# @rbs (?context_id: String) -> void
def close(context_id: nil)
context_id ||= @bridge.window_handle
@bidi.send_cmd('browsingContext.close', context: context_id)
Expand All @@ -88,23 +93,27 @@ def close(context_id: nil)
# Defaults to the current window handle.
#
# @return [String] The context ID of the created browsing context.
# @rbs (?type: nil, ?context_id: nil) -> String
def create(type: nil, context_id: nil)
type ||= :window
context_id ||= @bridge.window_handle
result = @bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: context_id)
result['context']
end

# @rbs (?context_id: nil, ?width: Integer, ?height: Integer, ?device_pixel_ratio: Float) -> void
def set_viewport(context_id: nil, width: nil, height: nil, device_pixel_ratio: nil)
context_id ||= @bridge.window_handle
params = {context: context_id, viewport: {width:, height:}, device_pixel_ratio:}
@bidi.send_cmd('browsingContext.setViewport', **params)
end

# @rbs (String, ?accept: bool, ?text: nil | String) -> void
def handle_user_prompt(context_id, accept: true, text: nil)
@bidi.send_cmd('browsingContext.handleUserPrompt', context: context_id, accept: accept, text: text)
end

# @rbs (?context_id: nil) -> void
def activate(context_id: nil)
context_id ||= @bridge.window_handle
@bidi.send_cmd('browsingContext.activate', context: context_id)
Expand Down
4 changes: 4 additions & 0 deletions rb/lib/selenium/webdriver/bidi/log_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class LogHandler
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)

# @rbs (Selenium::WebDriver::BiDi) -> void
def initialize(bidi)
@bidi = bidi
@log_entry_subscribed = false
end

# @return [int] id of the handler
# steep:ignore:start
# @rbs (String) -> Integer
def add_message_handler(type)
subscribe_log_entry unless @log_entry_subscribed
@bidi.add_callback('log.entryAdded') do |params|
Expand All @@ -43,13 +45,15 @@ def add_message_handler(type)
# steep:ignore:end

# @param [int] id of the handler previously added
# @rbs (Integer) -> nil
def remove_message_handler(id)
@bidi.remove_callback('log.entryAdded', id)
unsubscribe_log_entry if @log_entry_subscribed && @bidi.callbacks['log.entryAdded'].empty?
end

private

# @rbs () -> bool
def subscribe_log_entry
@bidi.session.subscribe('log.entryAdded')
@log_entry_subscribed = true
Expand Down
9 changes: 9 additions & 0 deletions rb/lib/selenium/webdriver/bidi/log_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class LogInspector
WARNING: 'warning'
}.freeze

# @rbs (Selenium::WebDriver::Chrome::Driver, ?nil) -> void
def initialize(driver, browsing_context_ids = nil)
WebDriver.logger.deprecate('LogInspector class',
'Script class with driver.script',
Expand All @@ -54,6 +55,7 @@ def initialize(driver, browsing_context_ids = nil)
@bidi.session.subscribe('log.entryAdded', browsing_context_ids)
end

# @rbs (?Selenium::WebDriver::BiDi::FilterBy?) -> void
def on_console_entry(filter_by = nil, &block)
check_valid_filter(filter_by)

Expand All @@ -63,6 +65,7 @@ def on_console_entry(filter_by = nil, &block)
end
end

# @rbs (?Selenium::WebDriver::BiDi::FilterBy?) -> void
def on_javascript_log(filter_by = nil, &block)
check_valid_filter(filter_by)

Expand All @@ -72,13 +75,15 @@ def on_javascript_log(filter_by = nil, &block)
end
end

# @rbs () -> void
def on_javascript_exception(&block)
on_log do |params|
type = params['type']
javascript_log_events(params, FilterBy.log_level('error'), &block) if type.eql?('javascript')
end
end

# @rbs (?Selenium::WebDriver::BiDi::FilterBy?) -> Integer?
def on_log(filter_by = nil, &block)
unless filter_by.nil?
check_valid_filter(filter_by)
Expand All @@ -94,17 +99,20 @@ def on_log(filter_by = nil, &block)

private

# @rbs (Symbol) -> Integer
def on(event, &block)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback("log.#{event}", &block)
end

# @rbs (Selenium::WebDriver::BiDi::FilterBy?) -> void
def check_valid_filter(filter_by)
return if filter_by.nil? || filter_by.instance_of?(FilterBy)

raise "Pass valid FilterBy object. Received: #{filter_by.inspect}"
end

# @rbs (Hash[untyped, untyped], Selenium::WebDriver::BiDi::FilterBy?) -> Array[untyped]?
def console_log_events(params, filter_by)
event = ConsoleLogEntry.new(
level: params['level'],
Expand All @@ -125,6 +133,7 @@ def console_log_events(params, filter_by)
yield(event)
end

# @rbs (Hash[untyped, untyped], Selenium::WebDriver::BiDi::FilterBy?) -> Selenium::WebDriver::BiDi::JavascriptLogEntry?
def javascript_log_events(params, filter_by)
event = JavascriptLogEntry.new(
level: params['level'],
Expand Down
12 changes: 12 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class Network
auth_required: 'authRequired'
}.freeze

# @rbs (Selenium::WebDriver::BiDi) -> void
def initialize(bidi)
@bidi = bidi
end

# @rbs (?phases: Array[untyped], ?contexts: nil, ?url_patterns: Array[untyped], ?pattern_type: nil | Symbol) -> Hash[untyped, untyped]
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
@bidi.send_cmd('network.addIntercept',
Expand All @@ -48,10 +50,12 @@ def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :s
urlPatterns: url_patterns)
end

# @rbs (String) -> void
def remove_intercept(intercept)
@bidi.send_cmd('network.removeIntercept', intercept: intercept)
end

# @rbs (String, String, String) -> Hash[untyped, untyped]
def continue_with_auth(request_id, username, password)
@bidi.send_cmd(
'network.continueWithAuth',
Expand All @@ -65,6 +69,7 @@ def continue_with_auth(request_id, username, password)
)
end

# @rbs (String) -> Hash[untyped, untyped]
def continue_without_auth(request_id)
@bidi.send_cmd(
'network.continueWithAuth',
Expand All @@ -73,6 +78,7 @@ def continue_without_auth(request_id)
)
end

# @rbs (String) -> Hash[untyped, untyped]
def cancel_auth(request_id)
@bidi.send_cmd(
'network.continueWithAuth',
Expand All @@ -81,6 +87,7 @@ def cancel_auth(request_id)
)
end

# @rbs (**(String | Array[untyped])? | String | Hash[untyped, untyped] | Array[untyped]) -> Hash[untyped, untyped]?
def continue_request(**args)
@bidi.send_cmd(
'network.continueRequest',
Expand All @@ -93,13 +100,15 @@ def continue_request(**args)
)
end

# @rbs (String) -> Hash[untyped, untyped]?
def fail_request(request_id)
@bidi.send_cmd(
'network.failRequest',
request: request_id
)
end

# @rbs (**(String | Array[untyped])? | (String | Array[untyped] | Hash[untyped, untyped])?) -> Hash[untyped, untyped]?
def continue_response(**args)
@bidi.send_cmd(
'network.continueResponse',
Expand All @@ -112,6 +121,7 @@ def continue_response(**args)
)
end

# @rbs (**(String | Array[untyped] | Hash[untyped, untyped] | Integer)?) -> Hash[untyped, untyped]
def provide_response(**args)
@bidi.send_cmd(
'network.provideResponse',
Expand All @@ -124,10 +134,12 @@ def provide_response(**args)
)
end

# @rbs (String, *String) -> Hash[untyped, untyped]
def set_cache_behavior(behavior, *contexts)
@bidi.send_cmd('network.setCacheBehavior', cacheBehavior: behavior, contexts: contexts)
end

# @rbs (Symbol) -> Hash[untyped, untyped]
def on(event, &block)
event = EVENTS[event] if event.is_a?(Symbol)
@bidi.add_callback(event, &block)
Expand Down
3 changes: 3 additions & 0 deletions rb/lib/selenium/webdriver/bidi/network/intercepted_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ module Selenium
module WebDriver
class BiDi
class InterceptedAuth < InterceptedItem
# @rbs (String, String) -> Hash[untyped, untyped]
def authenticate(username, password)
network.continue_with_auth(id, username, password)
end

# @rbs () -> Hash[untyped, untyped]
def skip
network.continue_without_auth(id)
end

# @rbs () -> Hash[untyped, untyped]
def cancel
network.cancel_auth(id)
end
Expand Down
Loading
Loading