Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/bidi/log_handler.rb
4089 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
class BiDi
23
# Implements the Log of the WebDriver-BiDi specification
24
# This functionality should be accessed through `driver.script` method
25
#
26
# @api private
27
#
28
class LogHandler
29
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
30
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)
31
32
def initialize(bidi)
33
@bidi = bidi
34
@log_entry_subscribed = false
35
end
36
37
# @return [int] id of the handler
38
# steep:ignore:start
39
def add_message_handler(type)
40
subscribe_log_entry unless @log_entry_subscribed
41
@bidi.add_callback('log.entryAdded') do |params|
42
if params['type'] == type
43
log_entry_klass = type == 'console' ? ConsoleLogEntry : JavaScriptLogEntry
44
yield(log_entry_klass.new(**params))
45
end
46
end
47
end
48
# steep:ignore:end
49
50
# @param [int] id of the handler previously added
51
def remove_message_handler(id)
52
@bidi.remove_callback('log.entryAdded', id)
53
unsubscribe_log_entry if @log_entry_subscribed && @bidi.callbacks['log.entryAdded'].empty?
54
end
55
56
private
57
58
def subscribe_log_entry
59
@bidi.session.subscribe('log.entryAdded')
60
@log_entry_subscribed = true
61
end
62
63
def unsubscribe_log_entry
64
@bidi.session.unsubscribe('log.entryAdded')
65
@log_entry_subscribed = false
66
end
67
end # LogHandler
68
end # Bidi
69
end # WebDriver
70
end # Selenium
71
72