Path: blob/trunk/rb/lib/selenium/webdriver/bidi/log_handler.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21class BiDi22class LogHandler23ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)24JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)2526def initialize(bidi)27@bidi = bidi28@log_entry_subscribed = false29end3031# @return [int] id of the handler32# steep:ignore:start33def add_message_handler(type)34subscribe_log_entry unless @log_entry_subscribed35@bidi.add_callback('log.entryAdded') do |params|36if params['type'] == type37log_entry_klass = type == 'console' ? ConsoleLogEntry : JavaScriptLogEntry38yield(log_entry_klass.new(**params))39end40end41end42# steep:ignore:end4344# @param [int] id of the handler previously added45def remove_message_handler(id)46@bidi.remove_callback('log.entryAdded', id)47unsubscribe_log_entry if @log_entry_subscribed && @bidi.callbacks['log.entryAdded'].empty?48end4950private5152def subscribe_log_entry53@bidi.session.subscribe('log.entryAdded')54@log_entry_subscribed = true55end5657def unsubscribe_log_entry58@bidi.session.unsubscribe('log.entryAdded')59@log_entry_subscribed = false60end61end # LogHandler62end # Bidi63end # WebDriver64end # Selenium656667