Skip to content

Commit

Permalink
Merge pull request #135 from aik099/shared-session-strategy-popup-clo…
Browse files Browse the repository at this point in the history
…sing

Closes popups after test finishes (shared strategy)
  • Loading branch information
aik099 committed Apr 7, 2024
2 parents 7142faa + 6f7c188 commit 126c515
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion library/aik099/PHPUnit/Session/SharedSessionStrategy.php
Expand Up @@ -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();
}
}

/**
Expand Down
29 changes: 26 additions & 3 deletions tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
Expand Up @@ -34,7 +34,7 @@ class SharedSessionStrategyTest extends BrowserStackAwareTestCase
/**
* @large
*/
public function testOne()
public function testOpensPage()
{
$session = $this->getSession();
$session->visit('https://www.google.com');
Expand All @@ -44,14 +44,37 @@ public function testOne()

/**
* @large
* @depends testOne
* @depends testOpensPage
*/
public function testTwo()
public function testUsesOpenedPage()
{
$session = $this->getSession();
$url = $session->getCurrentUrl();

$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.
}

}
19 changes: 17 additions & 2 deletions tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php
Expand Up @@ -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');
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 126c515

Please sign in to comment.