@@ -661,14 +661,25 @@ describe('Connect in a new window', () => {
661661 await cleanup ( compass ) ;
662662 } ) ;
663663
664+ afterEach ( async function ( ) {
665+ // Close any windows opened while testing, in reverse order
666+ const [ mainWindow , ...otherWindows ] = await browser . getWindowHandles ( ) ;
667+ for ( const window of otherWindows . reverse ( ) ) {
668+ await browser . switchToWindow ( window ) ;
669+ await browser . closeWindow ( ) ;
670+ }
671+ await browser . switchToWindow ( mainWindow ) ;
672+ } ) ;
673+
664674 it ( 'can connect in new window' , async function ( this ) {
665675 skipForWeb ( this , 'connecting in new window is not supported on web' ) ;
666676
667677 const connectionName = DEFAULT_CONNECTION_NAMES [ 0 ] ;
668678 const connectionSelector = Selectors . sidebarConnection ( connectionName ) ;
669679 await browser . hover ( connectionSelector ) ;
670680
671- expect ( ( await browser . getWindowHandles ( ) ) . length ) . equals ( 1 ) ;
681+ const windowsBefore = await browser . getWindowHandles ( ) ;
682+ expect ( windowsBefore . length ) . equals ( 1 ) ;
672683
673684 const connectionElement = browser . $ ( connectionSelector ) ;
674685 await browser . clickVisible (
@@ -681,6 +692,7 @@ describe('Connect in a new window', () => {
681692 const windowsAfter = await browser . getWindowHandles ( ) ;
682693 expect ( windowsAfter . length ) . equals ( 2 ) ;
683694 const [ , newWindowHandle ] = windowsAfter ;
695+
684696 await browser . switchToWindow ( newWindowHandle ) ;
685697 await browser . waitForConnectionResult ( connectionName , {
686698 connectionStatus : 'success' ,
0 commit comments