Path: blob/trunk/rb/lib/selenium/webdriver/bidi/log_handler.rb
4089 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 BiDi22# Implements the Log of the WebDriver-BiDi specification23# This functionality should be accessed through `driver.script` method24#25# @api private26#27class LogHandler28ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)29JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)3031def initialize(bidi)32@bidi = bidi33@log_entry_subscribed = false34end3536# @return [int] id of the handler37# steep:ignore:start38def add_message_handler(type)39subscribe_log_entry unless @log_entry_subscribed40@bidi.add_callback('log.entryAdded') do |params|41if params['type'] == type42log_entry_klass = type == 'console' ? ConsoleLogEntry : JavaScriptLogEntry43yield(log_entry_klass.new(**params))44end45end46end47# steep:ignore:end4849# @param [int] id of the handler previously added50def remove_message_handler(id)51@bidi.remove_callback('log.entryAdded', id)52unsubscribe_log_entry if @log_entry_subscribed && @bidi.callbacks['log.entryAdded'].empty?53end5455private5657def subscribe_log_entry58@bidi.session.subscribe('log.entryAdded')59@log_entry_subscribed = true60end6162def unsubscribe_log_entry63@bidi.session.unsubscribe('log.entryAdded')64@log_entry_subscribed = false65end66end # LogHandler67end # Bidi68end # WebDriver69end # Selenium707172