|
| 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 | + |
| 27 | +module Selenium |
| 28 | + module WebDriver |
| 29 | + class BiDi |
| 30 | + class LogInspector |
| 31 | + EVENTS = { |
| 32 | + entry_added: 'entryAdded' |
| 33 | + }.freeze |
| 34 | + |
| 35 | + LOG_LEVEL = { |
| 36 | + DEBUG: "debug", |
| 37 | + ERROR: "error", |
| 38 | + INFO: "info", |
| 39 | + WARNING: "warning" |
| 40 | + }.freeze |
| 41 | + |
| 42 | + def initialize(driver, browsing_context_ids = nil) |
| 43 | + unless driver.capabilities.web_socket_url |
| 44 | + raise Error::WebDriverError, "WebDriver instance must support BiDi protocol" |
| 45 | + end |
| 46 | + |
| 47 | + @bidi = driver.bidi |
| 48 | + @bidi.session.subscribe("log.entryAdded", browsing_context_ids) |
| 49 | + end |
| 50 | + |
| 51 | + def on_console_entry(&block) |
| 52 | + enabled = log_listeners[:console].any? |
| 53 | + log_listeners[:console] << block |
| 54 | + return if enabled |
| 55 | + |
| 56 | + on_log do |params| |
| 57 | + type = params["type"] |
| 58 | + console_log_events(params) if type.eql?("console") |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def on_javascript_log(&block) |
| 63 | + enabled = log_listeners[:javascript].any? |
| 64 | + log_listeners[:javascript] << block |
| 65 | + return if enabled |
| 66 | + |
| 67 | + on_log do |params| |
| 68 | + type = params["type"] |
| 69 | + javascript_log_events(params) if type.eql?("javascript") |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def on_javascript_exception(&block) |
| 74 | + enabled = log_listeners[:js_exception].any? |
| 75 | + log_listeners[:js_exception] << block |
| 76 | + log_listeners[:javascript] << block |
| 77 | + return if enabled |
| 78 | + |
| 79 | + on_log do |params| |
| 80 | + type = params["type"] |
| 81 | + level = params["level"] |
| 82 | + |
| 83 | + javascript_log_events(params) if type.eql?("javascript") && level.eql?(LOG_LEVEL[:ERROR]) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def on_log(&block) |
| 88 | + on(:entry_added, &block) |
| 89 | + end |
| 90 | + |
| 91 | + private |
| 92 | + |
| 93 | + def on(event, &block) |
| 94 | + event = EVENTS[event] if event.is_a?(Symbol) |
| 95 | + @bidi.callbacks["log.#{event}"] << block |
| 96 | + end |
| 97 | + |
| 98 | + def log_listeners |
| 99 | + @log_listeners ||= Hash.new { |listeners, kind| listeners[kind] = [] } |
| 100 | + end |
| 101 | + |
| 102 | + def console_log_events(params) |
| 103 | + event = ConsoleLogEntry.new( |
| 104 | + level: params['level'], |
| 105 | + text: params['text'], |
| 106 | + timestamp: params['timestamp'], |
| 107 | + type: params['type'], |
| 108 | + method: params['method'], |
| 109 | + realm: params['realm'], |
| 110 | + args: params['args'], |
| 111 | + stack_trace: params['stackTrace'] |
| 112 | + ) |
| 113 | + log_listeners[:console].each do |listener| |
| 114 | + listener.call(event) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def javascript_log_events(params) |
| 119 | + event = JavascriptLogEntry.new( |
| 120 | + level: params['level'], |
| 121 | + text: params['text'], |
| 122 | + timestamp: params['timestamp'], |
| 123 | + type: params['type'], |
| 124 | + stack_trace: params['stackTrace'] |
| 125 | + ) |
| 126 | + log_listeners[:javascript].each do |listener| |
| 127 | + listener.call(event) |
| 128 | + end |
| 129 | + |
| 130 | + return unless params['level'].eql?(LOG_LEVEL[:ERROR]) |
| 131 | + |
| 132 | + log_listeners[:js_exception].each do |listener| |
| 133 | + listener.call(event) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + end # LogInspector |
| 138 | + end # Bidi |
| 139 | + end # WebDriver |
| 140 | +end # Selenium |
0 commit comments