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
1865 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
class LogHandler
24
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
25
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)
26
27
def initialize(bidi)
28
@bidi = bidi
29
@log_entry_subscribed = false
30
end
31
32
# @return [int] id of the handler
33
# steep:ignore:start
34
def add_message_handler(type)
35
subscribe_log_entry unless @log_entry_subscribed
36
@bidi.add_callback('log.entryAdded') do |params|
37
if params['type'] == type
38
log_entry_klass = type == 'console' ? ConsoleLogEntry : JavaScriptLogEntry
39
yield(log_entry_klass.new(**params))
40
end
41
end
42
end
43
# steep:ignore:end
44
45
# @param [int] id of the handler previously added
46
def remove_message_handler(id)
47
@bidi.remove_callback('log.entryAdded', id)
48
unsubscribe_log_entry if @log_entry_subscribed && @bidi.callbacks['log.entryAdded'].empty?
49
end
50
51
private
52
53
def subscribe_log_entry
54
@bidi.session.subscribe('log.entryAdded')
55
@log_entry_subscribed = true
56
end
57
58
def unsubscribe_log_entry
59
@bidi.session.unsubscribe('log.entryAdded')
60
@log_entry_subscribed = false
61
end
62
end # LogHandler
63
end # Bidi
64
end # WebDriver
65
end # Selenium
66
67