Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/bidi/log_inspector.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
# This file is automatically generated. Any changes will be lost!
21
22
require_relative 'log/base_log_entry'
23
require_relative 'log/generic_log_entry'
24
require_relative 'log/console_log_entry'
25
require_relative 'log/javascript_log_entry'
26
require_relative 'log/filter_by'
27
28
module Selenium
29
module WebDriver
30
class BiDi
31
class LogInspector
32
EVENTS = {
33
entry_added: 'entryAdded'
34
}.freeze
35
36
LOG_LEVEL = {
37
DEBUG: 'debug',
38
ERROR: 'error',
39
INFO: 'info',
40
WARNING: 'warning'
41
}.freeze
42
43
def initialize(driver, browsing_context_ids = nil)
44
WebDriver.logger.deprecate('LogInspector class',
45
'Script class with driver.script',
46
id: :log_inspector)
47
48
unless driver.capabilities.web_socket_url
49
raise Error::WebDriverError,
50
'WebDriver instance must support BiDi protocol'
51
end
52
53
@bidi = driver.bidi
54
@bidi.session.subscribe('log.entryAdded', browsing_context_ids)
55
end
56
57
def on_console_entry(filter_by = nil, &block)
58
check_valid_filter(filter_by)
59
60
on_log do |params|
61
type = params['type']
62
console_log_events(params, filter_by, &block) if type.eql?('console')
63
end
64
end
65
66
def on_javascript_log(filter_by = nil, &block)
67
check_valid_filter(filter_by)
68
69
on_log do |params|
70
type = params['type']
71
javascript_log_events(params, filter_by, &block) if type.eql?('javascript')
72
end
73
end
74
75
def on_javascript_exception(&block)
76
on_log do |params|
77
type = params['type']
78
javascript_log_events(params, FilterBy.log_level('error'), &block) if type.eql?('javascript')
79
end
80
end
81
82
def on_log(filter_by = nil, &block)
83
unless filter_by.nil?
84
check_valid_filter(filter_by)
85
86
on(:entry_added) do |params|
87
yield(params) if params['level'] == filter_by.level
88
end
89
return
90
end
91
92
on(:entry_added, &block)
93
end
94
95
private
96
97
def on(event, &block)
98
event = EVENTS[event] if event.is_a?(Symbol)
99
@bidi.add_callback("log.#{event}", &block)
100
end
101
102
def check_valid_filter(filter_by)
103
return if filter_by.nil? || filter_by.instance_of?(FilterBy)
104
105
raise "Pass valid FilterBy object. Received: #{filter_by.inspect}"
106
end
107
108
def console_log_events(params, filter_by)
109
event = ConsoleLogEntry.new(
110
level: params['level'],
111
text: params['text'],
112
timestamp: params['timestamp'],
113
type: params['type'],
114
method: params['method'],
115
realm: params['realm'],
116
args: params['args'],
117
stack_trace: params['stackTrace']
118
)
119
120
unless filter_by.nil?
121
yield(event) if params['level'] == filter_by.level
122
return
123
end
124
125
yield(event)
126
end
127
128
def javascript_log_events(params, filter_by)
129
event = JavascriptLogEntry.new(
130
level: params['level'],
131
text: params['text'],
132
timestamp: params['timestamp'],
133
type: params['type'],
134
stack_trace: params['stackTrace']
135
)
136
137
unless filter_by.nil?
138
yield(event) if params['level'] == filter_by.level
139
return
140
end
141
142
yield(event)
143
end
144
end # LogInspector
145
end # Bidi
146
end # WebDriver
147
end # Selenium
148
149