Skip to content

Commit 26dd4de

Browse files
committed
[java][bidi] Add filter parameter to LogInspector methods
1 parent 12f0d19 commit 26dd4de

File tree

8 files changed

+302
-76
lines changed

8 files changed

+302
-76
lines changed

java/src/org/openqa/selenium/bidi/LogInspector.java

Lines changed: 85 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,22 @@
2121

2222
import org.openqa.selenium.bidi.log.BaseLogEntry;
2323
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
24+
import org.openqa.selenium.bidi.log.FilterBy;
2425
import org.openqa.selenium.bidi.log.GenericLogEntry;
2526
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
2627
import org.openqa.selenium.bidi.log.Log;
2728
import org.openqa.selenium.bidi.log.LogEntry;
29+
import org.openqa.selenium.bidi.log.LogLevel;
2830
import org.openqa.selenium.internal.Require;
2931

3032
import java.util.Collections;
3133
import java.util.HashSet;
32-
import java.util.LinkedList;
33-
import java.util.List;
3434
import java.util.Set;
35+
import java.util.concurrent.atomic.AtomicReference;
3536
import java.util.function.Consumer;
3637

3738
public class LogInspector implements AutoCloseable {
3839

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<>();
4340
private final Set<String> browsingContextIds;
4441

4542
private final BiDi bidi;
@@ -62,62 +59,108 @@ public LogInspector(Set<String> browsingContextIds, WebDriver driver) {
6259

6360
this.bidi = ((HasBiDi) driver).getBiDi();
6461
this.browsingContextIds = browsingContextIds;
65-
initializeLogListener();
6662
}
6763

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-
);
64+
@Deprecated
65+
public void onConsoleLog(Consumer<ConsoleLogEntry> consumer) {
66+
Consumer<LogEntry> logEntryConsumer =
67+
logEntry -> logEntry.getConsoleLogEntry().ifPresent(consumer);
8468

85-
logEntry.getGenericLogEntry().ifPresent(
86-
genericLogEntry -> genericLogListeners.forEach(
87-
consumer -> consumer.accept(genericLogEntry)));
69+
addLogEntryAddedListener(logEntryConsumer);
70+
}
8871

89-
};
72+
public void onConsoleEntry(Consumer<ConsoleLogEntry> consumer) {
73+
Consumer<LogEntry> logEntryConsumer =
74+
logEntry -> logEntry.getConsoleLogEntry().ifPresent(consumer);
9075

91-
if (browsingContextIds.isEmpty()) {
92-
this.bidi.addListener(Log.entryAdded(), logEntryConsumer);
93-
} else {
94-
this.bidi.addListener(browsingContextIds, Log.entryAdded(), logEntryConsumer);
95-
}
76+
addLogEntryAddedListener(logEntryConsumer);
9677
}
9778

98-
@Deprecated
99-
public void onConsoleLog(Consumer<ConsoleLogEntry> consumer) {
100-
consoleLogListeners.add(consumer);
101-
}
79+
public void onConsoleEntry(Consumer<ConsoleLogEntry> consumer, FilterBy filter) {
80+
Consumer<LogEntry> logEntryConsumer =
81+
logEntry -> logEntry.getConsoleLogEntry().ifPresent(entry -> {
82+
if (filter.getLevel() != null && entry.getLevel() == filter.getLevel()) {
83+
consumer.accept(entry);
84+
}
85+
});
10286

103-
public void onConsoleEntry(Consumer<ConsoleLogEntry> consumer) {
104-
consoleLogListeners.add(consumer);
87+
addLogEntryAddedListener(logEntryConsumer);
10588
}
10689

90+
10791
public void onJavaScriptLog(Consumer<JavascriptLogEntry> consumer) {
108-
jsLogListeners.add(consumer);
92+
Consumer<LogEntry> logEntryConsumer =
93+
logEntry -> logEntry.getJavascriptLogEntry().ifPresent(consumer);
94+
95+
addLogEntryAddedListener(logEntryConsumer);
96+
}
97+
98+
public void onJavaScriptLog(Consumer<JavascriptLogEntry> consumer, FilterBy filter) {
99+
Consumer<LogEntry> logEntryConsumer =
100+
logEntry -> logEntry.getJavascriptLogEntry().ifPresent(entry -> {
101+
if (filter.getLevel() != null && entry.getLevel() == filter.getLevel()) {
102+
consumer.accept(entry);
103+
}
104+
});
105+
106+
addLogEntryAddedListener(logEntryConsumer);
109107
}
110108

111109
public void onJavaScriptException(Consumer<JavascriptLogEntry> consumer) {
112-
jsExceptionLogListeners.add(consumer);
110+
Consumer<LogEntry> logEntryConsumer =
111+
logEntry -> logEntry.getJavascriptLogEntry().ifPresent(entry -> {
112+
if (entry.getLevel() == LogLevel.ERROR) {
113+
consumer.accept(entry);
114+
}
115+
});
116+
117+
addLogEntryAddedListener(logEntryConsumer);
113118
}
114119

115120
public void onGenericLog(Consumer<GenericLogEntry> consumer) {
116-
genericLogListeners.add(consumer);
121+
Consumer<LogEntry> logEntryConsumer =
122+
logEntry -> logEntry.getGenericLogEntry().ifPresent(consumer);
123+
124+
addLogEntryAddedListener(logEntryConsumer);
125+
}
126+
127+
public void onGenericLog(Consumer<GenericLogEntry> consumer, FilterBy filter) {
128+
Consumer<LogEntry> logEntryConsumer =
129+
logEntry -> logEntry.getGenericLogEntry().ifPresent(entry -> {
130+
if (filter.getLevel() != null && entry.getLevel() == filter.getLevel()) {
131+
consumer.accept(entry);
132+
}
133+
});
134+
135+
addLogEntryAddedListener(logEntryConsumer);
117136
}
118137

119138
public void onLog(Consumer<LogEntry> consumer) {
120-
this.bidi.addListener(Log.entryAdded(), consumer);
139+
addLogEntryAddedListener(consumer);
140+
}
141+
142+
public void onLog(Consumer<LogEntry> consumer, FilterBy filter) {
143+
Consumer<LogEntry> logEntryConsumer = logEntry -> {
144+
AtomicReference<BaseLogEntry> baseLogEntry = new AtomicReference<>();
145+
146+
logEntry.getGenericLogEntry().ifPresent(baseLogEntry::set);
147+
logEntry.getConsoleLogEntry().ifPresent(baseLogEntry::set);
148+
logEntry.getJavascriptLogEntry().ifPresent(baseLogEntry::set);
149+
150+
if (filter.getLevel() != null && baseLogEntry.get().getLevel() == filter.getLevel()) {
151+
consumer.accept(logEntry);
152+
}
153+
};
154+
155+
addLogEntryAddedListener(logEntryConsumer);
156+
}
157+
158+
private void addLogEntryAddedListener(Consumer<LogEntry> consumer) {
159+
if (browsingContextIds.isEmpty()) {
160+
this.bidi.addListener(Log.entryAdded(), consumer);
161+
} else {
162+
this.bidi.addListener(browsingContextIds, Log.entryAdded(), consumer);
163+
}
121164
}
122165

123166
@Override

java/src/org/openqa/selenium/bidi/log/BaseLogEntry.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,4 @@ public BaseLogEntry(LogLevel level,
5050
this.timestamp = timestamp;
5151
this.stackTrace = stackTrace;
5252
}
53-
54-
public enum LogLevel {
55-
DEBUG("debug"),
56-
ERROR("error"),
57-
INFO("info"),
58-
WARNING("warning");
59-
60-
private final String level;
61-
62-
LogLevel(String level) {
63-
this.level = level;
64-
}
65-
66-
@Override
67-
public String toString() {
68-
return level;
69-
}
70-
}
7153
}

java/src/org/openqa/selenium/bidi/log/ConsoleLogEntry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ConsoleLogEntry extends GenericLogEntry {
3333
private final String realm;
3434
private final List<Object> args;
3535

36-
public ConsoleLogEntry(BaseLogEntry.LogLevel level,
36+
public ConsoleLogEntry(LogLevel level,
3737
String text,
3838
long timestamp,
3939
String type,
@@ -60,7 +60,7 @@ public List<Object> getArgs() {
6060
}
6161

6262
public static ConsoleLogEntry fromJson(JsonInput input) {
63-
BaseLogEntry.LogLevel level = null;
63+
LogLevel level = null;
6464
String text = null;
6565
long timestamp = 0;
6666
String type = null;
@@ -73,7 +73,7 @@ public static ConsoleLogEntry fromJson(JsonInput input) {
7373
while (input.hasNext()) {
7474
switch (input.nextName()) {
7575
case "level":
76-
level = input.read(BaseLogEntry.LogLevel.class);
76+
level = input.read(LogLevel.class);
7777
break;
7878

7979
case "text":
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
package org.openqa.selenium.bidi.log;
18+
19+
public class FilterBy {
20+
21+
private final LogLevel level;
22+
23+
private FilterBy(LogLevel logLevel) {
24+
this.level = logLevel;
25+
}
26+
27+
public static FilterBy logLevel(LogLevel logLevel) {
28+
return new FilterBy(logLevel);
29+
}
30+
31+
public LogLevel getLevel() {
32+
return level;
33+
}
34+
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.log;
19+
20+
public enum LogLevel {
21+
DEBUG("debug"),
22+
ERROR("error"),
23+
INFO("info"),
24+
WARNING("warning");
25+
26+
private final String level;
27+
28+
LogLevel(String level) {
29+
this.level = level;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return level;
35+
}
36+
}

java/test/org/openqa/selenium/bidi/BiDiTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openqa.selenium.bidi.browsingcontext.NavigationResult;
2929
import org.openqa.selenium.bidi.log.BaseLogEntry;
3030
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
31+
import org.openqa.selenium.bidi.log.LogLevel;
3132
import org.openqa.selenium.environment.webserver.AppServer;
3233
import org.openqa.selenium.environment.webserver.NettyAppServer;
3334
import org.openqa.selenium.firefox.FirefoxDriver;
@@ -77,7 +78,7 @@ void canNavigateAndListenToErrors()
7778

7879
assertThat(logEntry.getText()).isEqualTo("Error: Not working");
7980
assertThat(logEntry.getType()).isEqualTo("javascript");
80-
assertThat(logEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.ERROR);
81+
assertThat(logEntry.getLevel()).isEqualTo(LogLevel.ERROR);
8182
}
8283
}
8384

0 commit comments

Comments
 (0)