Skip to content
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

[rb] Implement High Level Logging API with BiDi #14073

Merged
merged 13 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions rb/lib/selenium/webdriver/bidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ def close
end

def callbacks
WebDriver.logger.deprecate('BiDi#callbacks to add a callback to the list of callbacks',
'BiDi#add_callback(event, &block)', id: :callbacks)

@ws.callbacks
end

def add_callback(event, &block)
@ws.add_callback(event, &block)
end

def remove_callback(event, id)
@ws.remove_callback(event, id)
end

def session
@session ||= Session.new(self)
end
Expand Down
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/bidi/log_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def on_log(filter_by = nil, &block)

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

def check_valid_filter(filter_by)
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@
require 'selenium/webdriver/common/shadow_root'
require 'selenium/webdriver/common/websocket_connection'
require 'selenium/webdriver/common/child_process'
require 'selenium/webdriver/common/script'
20 changes: 14 additions & 6 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def navigate
@navigate ||= WebDriver::Navigation.new(bridge)
end

#
# @return [Script]
# @see Script
#

def script(*args)
if args.any?
# Maintain legacy alias
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
execute_script(*args)
else
@script ||= WebDriver::Script.new(bridge)
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
end
end

#
# @return [TargetLocator]
# @see TargetLocator
Expand Down Expand Up @@ -262,12 +276,6 @@ def add_virtual_authenticator(options)

alias all find_elements

#
# driver.script('function() { ... };')
#

alias script execute_script

# Get the first element matching the given selector. If given a
# String or Symbol, it will be used as the id of the element.
#
Expand Down
59 changes: 59 additions & 0 deletions rb/lib/selenium/webdriver/common/script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 Script
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
def initialize(bridge)
@bidi = bridge.bidi
@log_entry_subscribed = false
end

# @return [int] ID of the handler
def add_console_message_handler
subscribe_log_entry unless @log_entry_subscribed
@bidi.add_callback('log.entryAdded') do |params|
yield(params) if params['type'] == 'console'
end
end

# @return [int] ID of the handler
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
def add_javascript_error_handler
subscribe_log_entry unless @log_entry_subscribed
@bidi.add_callback('log.entryAdded') do |params|
yield(params) if params['type'] == 'javascript'
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
end
end

# @param [int] id of the handler previously added
def remove_console_message_handler(id)
@bidi.remove_callback('log.entryAdded', id)
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
end

alias remove_javascript_error_handler remove_console_message_handler

private

def subscribe_log_entry
@bidi.session.subscribe('log.entryAdded')
@log_entry_subscribed = true
end
end # Script
end # WebDriver
end # Selenium
14 changes: 14 additions & 0 deletions rb/lib/selenium/webdriver/common/websocket_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def initialize(url:)

process_handshake
@socket_thread = attach_socket_listener
@track_callbacks = Hash.new { |callbacks, event| callbacks[event] = {} }
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
@track_callbacks_id = 0
end

def close
Expand All @@ -52,6 +54,18 @@ def callbacks
@callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] }
end

def add_callback(event, &block)
@track_callbacks[event][@track_callbacks_id] = block
callbacks[event] << block
@track_callbacks_id += 1
@track_callbacks_id - 1
end

def remove_callback(event, id)
call = @track_callbacks[id]
@callbacks[event].delete(call)
end

def send_cmd(**payload)
id = next_id
data = payload.merge(id: id)
Expand Down
1 change: 1 addition & 0 deletions rb/sig/lib/selenium/webdriver/common/driver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Selenium
@devtools: untyped
@navigate: untyped

@script: untyped
@service_manager: untyped

def self.for: (untyped browser, Hash[untyped, untyped] opts) -> untyped
Expand Down
20 changes: 20 additions & 0 deletions rb/sig/selenium/web_driver/script.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Selenium
module WebDriver
class Script
@bidi: BiDi
@log_entry_subscribed: bool

def add_console_message_handler: -> untyped

def add_javascript_error_handler: -> untyped

def remove_console_message_handler: -> untyped

alias remove_javascript_error_handler remove_console_message_handler

private

def subscribe_log_entry: -> untyped
end
end
end
99 changes: 99 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi/script_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# 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 Script, only: {browser: %i[chrome edge firefox]} do
before { reset_driver!(web_socket_url: true) }
after(:all) { quit_driver }

it 'errors when bidi not enabled' do
reset_driver! do |driver|
expect {
driver.script
}.to raise_error(WebDriver::Error::WebDriverError, /Script methods requires enabling BiDi/)
end
end

it 'logs console messages' do
log_entries = []

driver.script.add_console_message_handler { |log| log_entries << log }
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
driver.navigate.to url_for('bidi/logEntryAdded.html')

driver.find_element(id: 'jsException').click
driver.find_element(id: 'consoleLog').click

wait.until { log_entries.any? }
expect(log_entries.size).to eq(1)
expect(log_entries.first).to include('level' => 'info',
titusfortner marked this conversation as resolved.
Show resolved Hide resolved
'method' => 'log',
'text' => 'Hello, world!',
'type' => 'console')
end

it 'logs multiple console messages' do
log_entries = []

driver.script.add_console_message_handler { |log| log_entries << log }
driver.script.add_console_message_handler { |log| log_entries << log }
driver.navigate.to url_for('bidi/logEntryAdded.html')

driver.find_element(id: 'jsException').click
driver.find_element(id: 'consoleLog').click

wait.until { log_entries.size > 1 }
expect(log_entries.size).to eq(2)
end

it 'logs removes console message hander' do
log_entries = []

id = driver.script.add_console_message_handler { |log| log_entries << log }
driver.script.add_console_message_handler { |log| log_entries << log }
driver.navigate.to url_for('bidi/logEntryAdded.html')
driver.find_element(id: 'consoleLog').click

driver.script.remove_console_message_hander(id)

driver.find_element(id: 'consoleLog').click

wait.until { log_entries.size > 2 }
expect(log_entries.size).to eq(3)
end

it 'logs javascript errors' do
log_entries = []

driver.script.add_javascript_error_handler { |log| log_entries << log }
driver.navigate.to url_for('bidi/logEntryAdded.html')
driver.find_element(id: 'consoleLog').click
driver.find_element(id: 'jsException').click

wait.until { log_entries.any? }
expect(log_entries.size).to eq(1)
expect(log_entries.first).to include('level' => 'error',
'text' => 'Error: Not working',
'type' => 'javascript')
end
end
end
end
10 changes: 10 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ module WebDriver
# do nothing
end

it 'errors when bidi not enabled' do
reset_driver! do |driver|
expect { driver.bidi }.to raise_error(WebDriver::Error::WebDriverError)
end
end

it '#callbacks throws deprecation' do
expect { driver.bidi.callbacks }.to have_deprecated(:callbacks)
end

it 'gets session status' do
status = driver.bidi.session.status
expect(status).to respond_to(:ready)
Expand Down
Loading