diff --git a/java/client/test/org/openqa/selenium/WindowSwitchingTest.java b/java/client/test/org/openqa/selenium/WindowSwitchingTest.java
index <HASH>..<HASH> 100644
--- a/java/client/test/org/openqa/selenium/WindowSwitchingTest.java
+++ b/java/client/test/org/openqa/selenium/WindowSwitchingTest.java
@@ -65,9 +65,8 @@ public class WindowSwitchingTest extends JUnit4TestBase {
     @Override
     protected void finished(Description description) {
       try {
-        if (! mainWindow.equals(driver.getWindowHandle())) {
-          driver.close();
-        }
+        driver.getWindowHandles().stream().filter(handle -> ! mainWindow.equals(handle))
+            .forEach(handle -> driver.switchTo().window(handle).close());
       } catch (Exception ignore) {
       }
       try {
Fixing test cleanup to close all non-main windows