diff --git a/selenium_test/environment.py b/selenium_test/environment.py
index <HASH>..<HASH> 100644
--- a/selenium_test/environment.py
+++ b/selenium_test/environment.py
@@ -320,6 +320,15 @@ def before_scenario(context, scenario):
 def after_scenario(context, _scenario):
     driver = context.driver
 
+    # Close all extra tabs.
+    handles = driver.window_handles
+    if handles:
+        for handle in handles:
+            if handle != context.initial_window_handle:
+                driver.switch_to_window(handle)
+                driver.close()
+        driver.switch_to_window(context.initial_window_handle)
+
     #
     # Make sure we did not trip a fatal error.
     #
@@ -379,15 +388,6 @@ def after_scenario(context, _scenario):
     dump_javascript_log(context)
     assert_false(terminating, "should not have experienced a fatal error")
 
-    # Close all extra tabs.
-    handles = driver.window_handles
-    if handles:
-        for handle in handles:
-            if handle != context.initial_window_handle:
-                driver.switch_to_window(handle)
-                driver.close()
-        driver.switch_to_window(context.initial_window_handle)
-
 
 def before_step(context, step):
     if context.behave_captions:
Close tabs first.

It is unclear why this has never bit us before but now we close the tabs
first. Doing this ensures that the async code is run on the main page,
which is what we want. Otherwise, the async code may run on some
documentat tab, and fail.