Skip to content

Commit 431f775

Browse files
authored
[java][cdp] Ensure console events return args as it is (#11435)
Fixes #11377
1 parent 5e06ee8 commit 431f775

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

java/src/org/openqa/selenium/devtools/events/ConsoleEvent.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@
2323

2424
import java.time.Instant;
2525
import java.util.List;
26+
import java.util.Objects;
2627
import java.util.stream.Collectors;
2728
import java.util.stream.Stream;
2829

2930
public class ConsoleEvent {
3031

3132
private final String type;
3233
private final Instant timestamp;
34+
private final List<Object> modifiedArgs;
3335
private final List<Object> args;
3436

35-
public ConsoleEvent(String type, Instant timestamp, Object... args) {
37+
public ConsoleEvent(String type, Instant timestamp, List<Object> modifiedArgs, Object... args) {
3638
this.type = type;
3739
this.timestamp = timestamp;
40+
this.modifiedArgs = modifiedArgs;
3841
this.args = ImmutableList.copyOf(args);
3942
}
4043

@@ -51,11 +54,10 @@ public List<Object> getArgs() {
5154
}
5255

5356
public List<String> getMessages() {
54-
return args.stream()
55-
.map(List.class::cast)
56-
.map(lst -> lst.get(0))
57+
return modifiedArgs.stream()
5758
.map(RemoteObject.class::cast)
5859
.map(RemoteObject::getValue)
60+
.filter(Objects::nonNull)
5961
.map(Object::toString)
6062
.collect(Collectors.toList());
6163
}

java/src/org/openqa/selenium/devtools/v105/V105Events.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) {
7474
return new ConsoleEvent(
7575
event.getType().toString(),
7676
Instant.ofEpochMilli(ts),
77-
modifiedArgs);
77+
modifiedArgs,
78+
event.getArgs());
7879
}
7980

8081
@Override

java/src/org/openqa/selenium/devtools/v106/V106Events.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) {
7474
return new ConsoleEvent(
7575
event.getType().toString(),
7676
Instant.ofEpochMilli(ts),
77-
modifiedArgs);
77+
modifiedArgs,
78+
event.getArgs());
7879
}
7980

8081
@Override

java/src/org/openqa/selenium/devtools/v107/V107Events.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) {
7575
return new ConsoleEvent(
7676
event.getType().toString(),
7777
Instant.ofEpochMilli(ts),
78-
modifiedArgs);
78+
modifiedArgs,
79+
event.getArgs());
7980
}
8081

8182
@Override

java/src/org/openqa/selenium/devtools/v108/V108Events.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) {
7575
return new ConsoleEvent(
7676
event.getType().toString(),
7777
Instant.ofEpochMilli(ts),
78-
modifiedArgs);
78+
modifiedArgs,
79+
event.getArgs());
7980
}
8081

8182
@Override

java/src/org/openqa/selenium/devtools/v85/V85Events.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) {
7575
return new ConsoleEvent(
7676
event.getType().toString(),
7777
Instant.ofEpochMilli(ts),
78-
modifiedArgs);
78+
modifiedArgs,
79+
event.getArgs());
7980
}
8081

8182
@Override

java/test/org/openqa/selenium/devtools/ConsoleEventsTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import org.junit.jupiter.api.Test;
2323
import org.openqa.selenium.By;
2424
import org.openqa.selenium.devtools.events.ConsoleEvent;
25+
import org.openqa.selenium.devtools.idealized.runtime.model.RemoteObject;
2526
import org.openqa.selenium.environment.webserver.Page;
2627
import org.openqa.selenium.testing.Ignore;
2728

29+
import java.util.List;
2830
import java.util.concurrent.CompletableFuture;
2931
import java.util.concurrent.ExecutionException;
3032
import java.util.concurrent.TimeUnit;
@@ -33,7 +35,6 @@
3335
class ConsoleEventsTest extends DevToolsTestBase {
3436

3537
@Test
36-
@Ignore(gitHubActions = true)
3738
public void canWatchConsoleEvents() throws InterruptedException, ExecutionException, TimeoutException {
3839
String page = appServer.create(
3940
new Page()
@@ -50,4 +51,23 @@ public void canWatchConsoleEvents() throws InterruptedException, ExecutionExcept
5051
assertThat(event.getMessages()).containsExactly("Hello, world!");
5152
}
5253

54+
@Test
55+
public void canWatchConsoleEventsWithArgs() throws InterruptedException, ExecutionException, TimeoutException {
56+
String page = appServer.create(
57+
new Page()
58+
.withBody("<div id='button' onclick='helloWorld()'>click me</div>")
59+
.withScripts("function helloWorld() { console.log(\"array\", [1, 2, 3]) }"));
60+
driver.get(page);
61+
62+
CompletableFuture<ConsoleEvent> future = new CompletableFuture<>();
63+
devTools.getDomains().events().addConsoleListener(future::complete);
64+
driver.findElement(By.id("button")).click();
65+
ConsoleEvent event = future.get(5, TimeUnit.SECONDS);
66+
67+
assertThat(event.getType()).isEqualTo("log");
68+
List<Object> args = event.getArgs();
69+
// Ensure args returned by CDP protocol are maintained
70+
assertThat(args).isNotInstanceOf((RemoteObject.class));
71+
}
72+
5373
}

0 commit comments

Comments
 (0)