|
| 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 | +} |
0 commit comments