|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi; |
| 19 | + |
| 20 | +import org.openqa.selenium.WebDriver; |
| 21 | + |
| 22 | +import org.openqa.selenium.bidi.log.BaseLogEntry; |
| 23 | +import org.openqa.selenium.bidi.log.ConsoleLogEntry; |
| 24 | +import org.openqa.selenium.bidi.log.GenericLogEntry; |
| 25 | +import org.openqa.selenium.bidi.log.JavascriptLogEntry; |
| 26 | +import org.openqa.selenium.bidi.log.Log; |
| 27 | +import org.openqa.selenium.bidi.log.LogEntry; |
| 28 | +import org.openqa.selenium.internal.Require; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.LinkedList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.function.Consumer; |
| 36 | + |
| 37 | +public class LogInspector implements AutoCloseable { |
| 38 | + |
| 39 | + private final List<Consumer<ConsoleLogEntry>> consoleLogListeners = new LinkedList<>(); |
| 40 | + private final List<Consumer<JavascriptLogEntry>> jsExceptionLogListeners = new LinkedList<>(); |
| 41 | + private final List<Consumer<JavascriptLogEntry>> jsLogListeners = new LinkedList<>(); |
| 42 | + private final List<Consumer<GenericLogEntry>> genericLogListeners = new LinkedList<>(); |
| 43 | + private final Set<String> browsingContextIds; |
| 44 | + |
| 45 | + private final BiDi bidi; |
| 46 | + |
| 47 | + public LogInspector(WebDriver driver) { |
| 48 | + this(new HashSet<>(), driver); |
| 49 | + } |
| 50 | + |
| 51 | + public LogInspector(String browsingContextId, WebDriver driver) { |
| 52 | + this(Collections.singleton(Require.nonNull("Browsing context id", browsingContextId)), driver); |
| 53 | + } |
| 54 | + |
| 55 | + public LogInspector(Set<String> browsingContextIds, WebDriver driver) { |
| 56 | + Require.nonNull("WebDriver", driver); |
| 57 | + Require.nonNull("Browsing context id list", browsingContextIds); |
| 58 | + |
| 59 | + if (!(driver instanceof HasBiDi)) { |
| 60 | + throw new IllegalArgumentException("WebDriver instance must support BiDi protocol"); |
| 61 | + } |
| 62 | + |
| 63 | + this.bidi = ((HasBiDi) driver).getBiDi(); |
| 64 | + this.browsingContextIds = browsingContextIds; |
| 65 | + initializeLogListener(); |
| 66 | + } |
| 67 | + |
| 68 | + private void initializeLogListener() { |
| 69 | + Consumer<LogEntry> logEntryConsumer = logEntry -> { |
| 70 | + logEntry.getConsoleLogEntry().ifPresent( |
| 71 | + consoleLogEntry -> consoleLogListeners.forEach( |
| 72 | + consumer -> consumer.accept(consoleLogEntry))); |
| 73 | + |
| 74 | + logEntry.getJavascriptLogEntry().ifPresent( |
| 75 | + jsLogEntry -> { |
| 76 | + if (jsLogEntry.getLevel() == BaseLogEntry.LogLevel.ERROR) { |
| 77 | + jsExceptionLogListeners.forEach( |
| 78 | + consumer -> consumer.accept(jsLogEntry)); |
| 79 | + } |
| 80 | + jsLogListeners.forEach( |
| 81 | + consumer -> consumer.accept(jsLogEntry)); |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + logEntry.getGenericLogEntry().ifPresent( |
| 86 | + genericLogEntry -> genericLogListeners.forEach( |
| 87 | + consumer -> consumer.accept(genericLogEntry))); |
| 88 | + |
| 89 | + }; |
| 90 | + |
| 91 | + if (browsingContextIds.isEmpty()) { |
| 92 | + this.bidi.addListener(Log.entryAdded(), logEntryConsumer); |
| 93 | + } else { |
| 94 | + this.bidi.addListener(browsingContextIds, Log.entryAdded(), logEntryConsumer); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void onConsoleLog(Consumer<ConsoleLogEntry> consumer) { |
| 99 | + consoleLogListeners.add(consumer); |
| 100 | + } |
| 101 | + |
| 102 | + public void onJavaScriptLog(Consumer<JavascriptLogEntry> consumer) { |
| 103 | + jsLogListeners.add(consumer); |
| 104 | + } |
| 105 | + |
| 106 | + public void onJavaScriptException(Consumer<JavascriptLogEntry> consumer) { |
| 107 | + jsExceptionLogListeners.add(consumer); |
| 108 | + } |
| 109 | + |
| 110 | + public void onGenericLog(Consumer<GenericLogEntry> consumer) { |
| 111 | + genericLogListeners.add(consumer); |
| 112 | + } |
| 113 | + |
| 114 | + public void onLog(Consumer<LogEntry> consumer) { |
| 115 | + this.bidi.addListener(Log.entryAdded(), consumer); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void close() { |
| 120 | + this.bidi.clearListener(Log.entryAdded()); |
| 121 | + } |
| 122 | +} |
0 commit comments