Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions common/src/web/blank.html
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
<html lang="en">
<head>
<title>blank</title>
<style>
body {
background: #f5f5f5;
background: linear-gradient(
109deg,
rgba(245, 245, 245, 1) 0%,
rgba(225, 225, 225, 1) 35%,
rgba(205, 205, 205, 1) 100%
);
}
body {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<h1 id="header">A blank slate</h1>
<script>
localStorage.clear();
sessionStorage.clear();
const params = new URLSearchParams(window.location.search);
const test = params.get('test');
document.getElementById('header').innerText = test;
</script>
</body>
</html>
<html><head><title>blank</title></head><body></body></html>
1 change: 1 addition & 0 deletions java/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ If `createNewDriver(capabilities)` is called without an annotation, it closes th
| Annotation | When to Use |
|------------|-------------|
| `@SwitchToTopAfterTest` | Test navigates into frames. Automatically switches to default content after. |
| `@CloseExtraWindowsAfterTest` | Test opens windows or tabs. Closes everything except the session's first window after and switches back to it. Class-level or method-level. |
| `@NeedsSecureServer` | Class-level. All tests in the class need HTTPS. |

## Build Files
Expand Down
96 changes: 96 additions & 0 deletions java/test/org/openqa/selenium/CloseExtraWindowsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.openqa.selenium.testing.CloseExtraWindowsAfterTest;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NoDriverAfterTest;
import org.openqa.selenium.testing.SeleniumExtension;

@TestMethodOrder(OrderAnnotation.class)
@CloseExtraWindowsAfterTest
class CloseExtraWindowsTest extends JupiterTestBase {

private static String windowBeforeOpener;

@Test
@Order(1)
void leavesExtraWindowsOpenForTheAnnotationToClose() {
windowBeforeOpener = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.switchTo().newWindow(WindowType.WINDOW);

assertThat(driver.getWindowHandles()).hasSize(3);
Comment thread
titusfortner marked this conversation as resolved.
}

@Test
@Order(2)
void startsOnTheInitialWindowWithNothingElseOpen() {
assumeTrue(windowBeforeOpener != null);

assertThat(driver.getWindowHandles()).containsExactly(windowBeforeOpener);
assertThat(driver.getWindowHandle()).isEqualTo(windowBeforeOpener);
}

@Test
@Order(3)
void closesExtraWindowsAndRestoresTheInitialOne() {
String initial = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.switchTo().newWindow(WindowType.WINDOW);

SeleniumExtension.closeExtraWindows(driver, initial);

assertThat(driver.getWindowHandles()).containsExactly(initial);
assertThat(driver.getWindowHandle()).isEqualTo(initial);
}

@Test
@Order(4)
void restoresTheInitialWindowAfterTheFocusedOneWasClosed() {
String initial = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.close();

SeleniumExtension.closeExtraWindows(driver, initial);

assertThat(driver.getWindowHandles()).containsExactly(initial);
assertThat(driver.getWindowHandle()).isEqualTo(initial);
}

@Test
@Order(5)
@NoDriverAfterTest
void keepsTheFirstRemainingWindowWhenTheInitialOneIsGone() {
String initial = driver.getWindowHandle();
String replacement = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle();
driver.switchTo().window(initial).close();

SeleniumExtension.closeExtraWindows(driver, initial);

assertThat(driver.getWindowHandles()).containsExactly(replacement);
assertThat(driver.getWindowHandle()).isEqualTo(replacement);
}
}
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/HistoryNavigationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.testing.CloseExtraWindowsAfterTest;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
Expand All @@ -34,6 +35,7 @@ public class HistoryNavigationTest extends JupiterTestBase {
@NeedsFreshDriver
@Test
@Ignore(value = SAFARI, reason = "Hanging")
@CloseExtraWindowsAfterTest
public void testShouldDoNothingIfThereIsNothingToGoBackTo() {
((JavascriptExecutor) driver).executeScript("window.open('', 'newWindow')");
wait.until(windowToBeSwitchedToWithName("newWindow"));
Expand Down
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/ImplicitWaitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.CloseExtraWindowsAfterTest;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NotYetImplemented;
Expand Down Expand Up @@ -144,6 +145,7 @@ public void testShouldImplicitlyWaitForAnElementToBeVisibleBeforeInteracting() {

@Test
@NotYetImplemented(SAFARI)
@CloseExtraWindowsAfterTest
public void testShouldRetainImplicitlyWaitFromTheReturnedWebDriverOfFrameSwitchTo() {
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));
driver.get(pages.xhtmlTestPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.openqa.selenium.bidi.module.Browser;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.CloseExtraWindowsAfterTest;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;

Expand Down Expand Up @@ -171,6 +172,7 @@ void canSetDownloadBehaviorDenied() throws InterruptedException {

@Test
@NeedsFreshDriver
@CloseExtraWindowsAfterTest
void canSetDownloadBehaviorWithUserContext() throws InterruptedException {
String userContext = browser.createUserContext();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -42,28 +43,27 @@
import org.openqa.selenium.bidi.BiDiException;
import org.openqa.selenium.bidi.module.BrowsingContextInspector;
import org.openqa.selenium.bidi.module.Script;
import org.openqa.selenium.testing.CloseExtraWindowsAfterTest;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
import org.openqa.selenium.testing.NotYetImplemented;

@CloseExtraWindowsAfterTest
class BrowsingContextInspectorTest extends JupiterTestBase {

@Test
@NeedsFreshDriver
void canListenToWindowBrowsingContextCreatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();

inspector.onBrowsingContextCreated(future::complete);
Map<String, BrowsingContextInfo> createdWindows = new ConcurrentHashMap<>();
inspector.onBrowsingContextCreated(info -> createdWindows.put(info.getId(), info));

String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();

BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);

assertThat(browsingContextInfo.getId()).isEqualTo(windowHandle);
wait.until(d -> createdWindows.containsKey(windowHandle));
BrowsingContextInfo browsingContextInfo = createdWindows.get(windowHandle);
assertThat("about:blank").isEqualTo(browsingContextInfo.getUrl());
assertThat(browsingContextInfo.getId()).isEqualTo(windowHandle);
assertThat(browsingContextInfo.getChildren()).isEqualTo(null);
assertThat(browsingContextInfo.getParentBrowsingContext()).isEqualTo(null);
}
Expand All @@ -81,9 +81,11 @@ void canListenToBrowsingContextDestroyedEvent() {
closedWindows.put(context.getId(), context);
});

String originalWindow = driver.getWindowHandle();
String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();

driver.close();
driver.switchTo().window(originalWindow);

wait.until(d -> closedWindows.containsKey(windowHandle));

Expand All @@ -100,16 +102,14 @@ void canListenToBrowsingContextDestroyedEvent() {
void canListenToTabBrowsingContextCreatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
inspector.onBrowsingContextCreated(future::complete);
Map<String, BrowsingContextInfo> createdWindows = new ConcurrentHashMap<>();
inspector.onBrowsingContextCreated(info -> createdWindows.put(info.getId(), info));

String windowHandle = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle();

BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);

assertThat(browsingContextInfo.getId()).isEqualTo(windowHandle);
wait.until(d -> createdWindows.containsKey(windowHandle));
BrowsingContextInfo browsingContextInfo = createdWindows.get(windowHandle);
assertThat("about:blank").isEqualTo(browsingContextInfo.getUrl());
assertThat(browsingContextInfo.getId()).isEqualTo(windowHandle);
assertThat(browsingContextInfo.getChildren()).isEqualTo(null);
assertThat(browsingContextInfo.getParentBrowsingContext()).isEqualTo(null);
}
Expand Down
Loading
Loading