From 6f7c188b28a5a99e7757b66c82a59fdc8039dcc7 Mon Sep 17 00:00:00 2001
From: Alex <aik099@users.noreply.github.com>
Date: Sun, 7 Apr 2024 22:45:20 +0300
Subject: [PATCH] Closes popups after test finishes (shared strategy)

---
 CHANGELOG.md                                  |  1 +
 .../PHPUnit/Session/SharedSessionStrategy.php | 13 ++++++++-
 .../Integration/SharedSessionStrategyTest.php | 29 +++++++++++++++++--
 .../Session/SharedSessionStrategyTest.php     | 19 ++++++++++--
 4 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a1b746e..28a7121 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 - (Not a BC break) Some public methods of the `BrowserTestCase` class are protected now. Affected methods: `setRemoteCoverageScriptUrl`, `setBrowser`, `getBrowser`, `setSessionStrategy`, `getSessionStrategy`, `getCollectCodeCoverageInformation`, `getRemoteCodeCoverageInformation`.
 - (Not a BC break) Some protected properties of the `BrowserTestCase` class are private now. Affected properties: `sessionStrategyManager`, `remoteCoverageHelper`, `sessionStrategy`.
 - Bumped minimal required `Behat/Mink` version to 1.8 (needed after `SessionProxy` class removal).
+- Shared session strategy now also closes popups left order from the previous test before switching back to the main window.
 
 ### Fixed
 - The remote code coverage collection cookies were set even, when the remote code coverage script URL wasn't specified.
diff --git a/library/aik099/PHPUnit/Session/SharedSessionStrategy.php b/library/aik099/PHPUnit/Session/SharedSessionStrategy.php
index 9b003dc..89bb8cd 100644
--- a/library/aik099/PHPUnit/Session/SharedSessionStrategy.php
+++ b/library/aik099/PHPUnit/Session/SharedSessionStrategy.php
@@ -98,7 +98,18 @@ protected function stopAndForgetSession()
 	 */
 	private function _switchToMainWindow()
 	{
-		$this->_session->switchToWindow(null);
+		$this->_session->switchToWindow();
+		$actual_initial_window_name = $this->_session->getWindowName(); // Account for initial window rename.
+
+		foreach ( $this->_session->getWindowNames() as $name ) {
+			if ( $name === $actual_initial_window_name ) {
+				continue;
+			}
+
+			$this->_session->switchToWindow($name);
+			$this->_session->executeScript('window.close();');
+			$this->_session->switchToWindow();
+		}
 	}
 
 	/**
diff --git a/tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php b/tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
index 939ef6d..ad0ce2a 100644
--- a/tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
+++ b/tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
@@ -34,7 +34,7 @@ class SharedSessionStrategyTest extends BrowserStackAwareTestCase
 	/**
 	 * @large
 	 */
-	public function testOne()
+	public function testOpensPage()
 	{
 		$session = $this->getSession();
 		$session->visit('https://www.google.com');
@@ -44,9 +44,9 @@ public function testOne()
 
 	/**
 	 * @large
-	 * @depends testOne
+	 * @depends testOpensPage
 	 */
-	public function testTwo()
+	public function testUsesOpenedPage()
 	{
 		$session = $this->getSession();
 		$url = $session->getCurrentUrl();
@@ -54,4 +54,27 @@ public function testTwo()
 		$this->assertStringContainsString('https://www.google.com', $url);
 	}
 
+	public function testOpensPopups()
+	{
+		$session = $this->getSession();
+		$session->visit('https://the-internet.herokuapp.com/windows');
+
+		$page = $session->getPage();
+		$page->clickLink('Click Here');
+		$page->clickLink('Click Here');
+
+		$this->assertCount(3, $session->getWindowNames()); // Main window + 2 popups.
+	}
+
+	/**
+	 * @depends testOpensPopups
+	 */
+	public function testNoPopupsBeforeTest()
+	{
+		$session = $this->getSession();
+		$this->assertEquals('https://the-internet.herokuapp.com/windows', $session->getCurrentUrl());
+
+		$this->assertCount(1, $session->getWindowNames()); // Main window.
+	}
+
 }
diff --git a/tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php b/tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php
index 28a84d9..a8d5d58 100644
--- a/tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php
+++ b/tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php
@@ -72,7 +72,7 @@ public function testSessionSharing(\Exception $e = null)
 		$this->_originalStrategy->shouldReceive('session')->once()->with($browser)->andReturn($this->_session1);
 		$this->_originalStrategy->shouldReceive('isFreshSession')->once()->andReturn(true);
 
-		$this->_session1->shouldReceive('switchToWindow')->once();
+		$this->expectNoPopups($this->_session1);
 
 		$this->assertSame($this->_session1, $this->strategy->session($browser));
 		$this->assertTrue($this->strategy->isFreshSession(), 'First created session must be fresh');
@@ -85,6 +85,21 @@ public function testSessionSharing(\Exception $e = null)
 		$this->assertFalse($this->strategy->isFreshSession(), 'Reused session must not be fresh');
 	}
 
+	/**
+	 * Expects no popups.
+	 *
+	 * @param MockInterface $session Session.
+	 *
+	 * @return void
+	 */
+	protected function expectNoPopups(MockInterface $session)
+	{
+		// Testing if popup windows are actually closed will be done in the integration test.
+		$session->shouldReceive('switchToWindow')->atLeast()->once();
+		$session->shouldReceive('getWindowName')->once()->andReturn('initial-window-name');
+		$session->shouldReceive('getWindowNames')->once()->andReturn(array('initial-window-name'));
+	}
+
 	/**
 	 * Returns exceptions, that doesn't reset session.
 	 *
@@ -121,7 +136,7 @@ public function testSessionResetOnFailure()
 
 		$this->_session1->shouldReceive('isStarted')->once()->andReturn(true);
 		$this->_session1->shouldReceive('stop')->once();
-		$this->_session2->shouldReceive('switchToWindow')->once();
+		$this->expectNoPopups($this->_session2);
 
 		$session = $this->strategy->session($browser);
 		$this->assertSame($this->_session1, $session);