Skip to content

Commit bb34eb8

Browse files
authored
[java] Close BiDi session on closing the last top-level browsing context
1 parent 7024fb8 commit bb34eb8

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import java.net.MalformedURLException;
7373
import java.net.URL;
7474
import java.time.Duration;
75+
import java.util.ArrayList;
7576
import java.util.Base64;
7677
import java.util.Collection;
7778
import java.util.Collections;
@@ -415,9 +416,16 @@ public void close() {
415416
}
416417
}
417418

418-
execute(DriverCommand.CLOSE);
419-
}
419+
Response response = execute(DriverCommand.CLOSE);
420+
Object value = response.getValue();
421+
List<String> windowHandles = (ArrayList<String>) value;
420422

423+
if (windowHandles.isEmpty() && this instanceof HasBiDi) {
424+
// If no top-level browsing contexts are open after calling close, it indicates that the WebDriver session is closed.
425+
// If the WebDriver session is closed, the BiDi session also needs to be closed.
426+
((HasBiDi) this).maybeGetBiDi().ifPresent(BiDi::close);
427+
}
428+
}
421429
@Override
422430
public void quit() {
423431
// no-op if session id is null. We're only going to make ourselves unhappy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.openqa.selenium.TimeoutException;
25+
import org.openqa.selenium.WindowType;
26+
import org.openqa.selenium.firefox.FirefoxDriver;
27+
import org.openqa.selenium.firefox.FirefoxOptions;
28+
29+
import java.util.Collections;
30+
31+
class BiDiSessionCleanUpTest {
32+
33+
private FirefoxDriver driver;
34+
35+
@Test
36+
void shouldNotCloseBiDiSessionIfOneWindowIsClosed() {
37+
FirefoxOptions options = new FirefoxOptions();
38+
// Enable BiDi
39+
options.setCapability("webSocketUrl", true);
40+
41+
driver = new FirefoxDriver(options);
42+
43+
BiDi biDi = driver.getBiDi();
44+
45+
BiDiSessionStatus status = biDi.send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
46+
assertThat(status).isNotNull();
47+
assertThat(status.getMessage()).isEqualTo("Session already started");
48+
49+
driver.switchTo().newWindow(WindowType.WINDOW);
50+
driver.switchTo().newWindow(WindowType.TAB);
51+
driver.switchTo().newWindow(WindowType.TAB);
52+
53+
driver.close();
54+
55+
BiDiSessionStatus statusAfterClosing = biDi.send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
56+
assertThat(statusAfterClosing).isNotNull();
57+
assertThat(status.getMessage()).isEqualTo("Session already started");
58+
driver.quit();
59+
}
60+
61+
@Test
62+
void shouldCloseBiDiSessionIfLastWindowIsClosed() {
63+
FirefoxOptions options = new FirefoxOptions();
64+
// Enable BiDi
65+
options.setCapability("webSocketUrl", true);
66+
67+
driver = new FirefoxDriver(options);
68+
69+
BiDi biDi = driver.getBiDi();
70+
71+
BiDiSessionStatus status = biDi.send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
72+
assertThat(status).isNotNull();
73+
assertThat(status.getMessage()).isEqualTo("Session already started");
74+
75+
driver.close();
76+
77+
// Closing the last top-level browsing context, closes the WebDriver and BiDi session
78+
assertThatExceptionOfType(TimeoutException.class)
79+
.isThrownBy(() -> biDi.send(new Command<>("session.status",
80+
Collections.emptyMap(),
81+
BiDiSessionStatus.class)));
82+
}
83+
}

java/test/org/openqa/selenium/remote/RemoteWebDriverUnitTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.IOException;
4141
import java.net.URL;
4242
import java.time.Duration;
43+
import java.util.ArrayList;
4344
import java.util.Arrays;
4445
import java.util.Collections;
4546
import java.util.List;
@@ -270,7 +271,7 @@ void canHandleGetWindowHandlesCommand() {
270271

271272
@Test
272273
void canHandleCloseCommand() {
273-
WebDriverFixture fixture = new WebDriverFixture(echoCapabilities, nullValueResponder);
274+
WebDriverFixture fixture = new WebDriverFixture(echoCapabilities, valueResponder(new ArrayList<>()));
274275

275276
fixture.driver.close();
276277

0 commit comments

Comments
 (0)