Skip to content

Commit cc00a3c

Browse files
committed
[java] Fix unsubscribe event BiDi API methods
1 parent 01fe86e commit cc00a3c

File tree

4 files changed

+70
-39
lines changed

4 files changed

+70
-39
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public BiDi(Connection connection) {
4242
public void close() {
4343
clearListeners();
4444
disconnectSession();
45+
connection.close();
4546
}
4647

4748
public void disconnectSession() {
@@ -64,8 +65,16 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
6465
connection.addListener(event, handler);
6566
}
6667

68+
public <X> void clearListener(Event<X> event) {
69+
Require.nonNull("Event to listen for", event);
70+
71+
send(new Command<>("session.unsubscribe",
72+
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
73+
74+
connection.clearListener(event);
75+
}
76+
6777
public void clearListeners() {
68-
send(new Command<>("session.unsubscribe", Collections.emptyMap()));
6978
connection.clearListeners();
7079
}
7180

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.io.Closeable;
4040
import java.io.StringReader;
4141
import java.time.Duration;
42+
import java.util.Collections;
43+
import java.util.List;
4244
import java.util.Map;
4345
import java.util.concurrent.CompletableFuture;
4446
import java.util.concurrent.ConcurrentHashMap;
@@ -53,6 +55,7 @@
5355
import java.util.function.Consumer;
5456
import java.util.logging.Level;
5557
import java.util.logging.Logger;
58+
import java.util.stream.Collectors;
5659

5760
public class Connection implements Closeable {
5861

@@ -166,10 +169,28 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
166169
}
167170
}
168171

172+
public <X> void clearListener(Event<X> event) {
173+
Lock lock = callbacksLock.writeLock();
174+
lock.lock();
175+
try {
176+
eventCallbacks.removeAll(event);
177+
} finally {
178+
lock.unlock();
179+
}
180+
}
181+
169182
public void clearListeners() {
170183
Lock lock = callbacksLock.writeLock();
171184
lock.lock();
172185
try {
186+
List<String> events = eventCallbacks.keySet()
187+
.stream()
188+
.map(Event::getMethod)
189+
.collect(Collectors.toList());
190+
191+
send(new Command<>("session.unsubscribe",
192+
ImmutableMap.of("events", events)));
193+
173194
eventCallbacks.clear();
174195
} finally {
175196
lock.unlock();

java/test/org/openqa/selenium/bidi/log/BiDiLogTest.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,24 @@ public void canListenToConsoleLog()
6161

6262
CompletableFuture<LogEntry> future = new CompletableFuture<>();
6363

64-
BiDi biDi = driver.getBiDi();
64+
try (BiDi biDi = driver.getBiDi()) {
6565

66-
biDi.addListener(Log.entryAdded(), future::complete);
66+
biDi.addListener(Log.entryAdded(), future::complete);
6767

68-
driver.findElement(By.id("consoleLog")).click();
69-
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
68+
driver.findElement(By.id("consoleLog")).click();
69+
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
7070

71-
assertThat(logEntry.getConsoleLogEntry().isPresent()).isTrue();
71+
assertThat(logEntry.getConsoleLogEntry().isPresent()).isTrue();
7272

73-
ConsoleLogEntry consoleLogEntry = logEntry.getConsoleLogEntry().get();
74-
assertThat(consoleLogEntry.getText()).isEqualTo("Hello, world!");
75-
assertThat(consoleLogEntry.getRealm()).isNull();
76-
assertThat(consoleLogEntry.getArgs().size()).isEqualTo(1);
77-
assertThat(consoleLogEntry.getType()).isEqualTo("console");
78-
assertThat(consoleLogEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.INFO);
79-
assertThat(consoleLogEntry.getMethod()).isEqualTo("log");
80-
assertThat(consoleLogEntry.getStackTrace()).isNull();
73+
ConsoleLogEntry consoleLogEntry = logEntry.getConsoleLogEntry().get();
74+
assertThat(consoleLogEntry.getText()).isEqualTo("Hello, world!");
75+
assertThat(consoleLogEntry.getRealm()).isNull();
76+
assertThat(consoleLogEntry.getArgs().size()).isEqualTo(1);
77+
assertThat(consoleLogEntry.getType()).isEqualTo("console");
78+
assertThat(consoleLogEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.INFO);
79+
assertThat(consoleLogEntry.getMethod()).isEqualTo("log");
80+
assertThat(consoleLogEntry.getStackTrace()).isNull();
81+
}
8182
}
8283

8384
@Test
@@ -89,19 +90,19 @@ public void canListenToJavascriptLog()
8990

9091
CompletableFuture<LogEntry> future = new CompletableFuture<>();
9192

92-
BiDi biDi = driver.getBiDi();
93-
94-
biDi.addListener(Log.entryAdded(), future::complete);
93+
try (BiDi biDi = driver.getBiDi()) {
94+
biDi.addListener(Log.entryAdded(), future::complete);
9595

96-
driver.findElement(By.id("jsException")).click();
97-
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
96+
driver.findElement(By.id("jsException")).click();
97+
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
9898

99-
assertThat(logEntry.getJavascriptLogEntry().isPresent()).isTrue();
99+
assertThat(logEntry.getJavascriptLogEntry().isPresent()).isTrue();
100100

101-
GenericLogEntry javascriptLogEntry = logEntry.getJavascriptLogEntry().get();
102-
assertThat(javascriptLogEntry.getText()).isEqualTo("Error: Not working");
103-
assertThat(javascriptLogEntry.getType()).isEqualTo("javascript");
104-
assertThat(javascriptLogEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.ERROR);
101+
GenericLogEntry javascriptLogEntry = logEntry.getJavascriptLogEntry().get();
102+
assertThat(javascriptLogEntry.getText()).isEqualTo("Error: Not working");
103+
assertThat(javascriptLogEntry.getType()).isEqualTo("javascript");
104+
assertThat(javascriptLogEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.ERROR);
105+
}
105106
}
106107

107108
@Test
@@ -113,17 +114,18 @@ public void canRetrieveStacktraceForALog()
113114

114115
CompletableFuture<LogEntry> future = new CompletableFuture<>();
115116

116-
BiDi biDi = driver.getBiDi();
117+
try (BiDi biDi = driver.getBiDi()) {
117118

118-
biDi.addListener(Log.entryAdded(), future::complete);
119+
biDi.addListener(Log.entryAdded(), future::complete);
119120

120-
driver.findElement(By.id("logWithStacktrace")).click();
121-
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
121+
driver.findElement(By.id("logWithStacktrace")).click();
122+
LogEntry logEntry = future.get(5, TimeUnit.SECONDS);
122123

123-
assertThat(logEntry.getJavascriptLogEntry().isPresent()).isTrue();
124-
StackTrace stackTrace = logEntry.getJavascriptLogEntry().get().getStackTrace();
125-
assertThat(stackTrace).isNotNull();
126-
assertThat(stackTrace.getCallFrames().size()).isEqualTo(4);
124+
assertThat(logEntry.getJavascriptLogEntry().isPresent()).isTrue();
125+
StackTrace stackTrace = logEntry.getJavascriptLogEntry().get().getStackTrace();
126+
assertThat(stackTrace).isNotNull();
127+
assertThat(stackTrace.getCallFrames().size()).isEqualTo(4);
128+
}
127129
}
128130

129131
@After

java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ void ensureBiDiSessionCreation() {
5454
WebDriver driver = new RemoteWebDriver(deployment.getServer().getUrl(), options);
5555
driver = new Augmenter().augment(driver);
5656

57-
BiDi biDi = ((HasBiDi) driver).getBiDi();
58-
59-
BiDiSessionStatus status =
60-
biDi.send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
61-
assertThat(status).isNotNull();
62-
assertThat(status.getMessage()).isEqualTo("Session already started");
63-
57+
try (BiDi biDi = ((HasBiDi) driver).getBiDi()) {
58+
BiDiSessionStatus status =
59+
biDi.send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
60+
assertThat(status).isNotNull();
61+
assertThat(status.getMessage()).isEqualTo("Session already started");
62+
}
6463
}
6564
}

0 commit comments

Comments
 (0)