Skip to content

Commit 126c515

Browse files
authored
Merge pull request #135 from aik099/shared-session-strategy-popup-closing
Closes popups after test finishes (shared strategy)
2 parents 7142faa + 6f7c188 commit 126c515

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1717
- (Not a BC break) Some public methods of the `BrowserTestCase` class are protected now. Affected methods: `setRemoteCoverageScriptUrl`, `setBrowser`, `getBrowser`, `setSessionStrategy`, `getSessionStrategy`, `getCollectCodeCoverageInformation`, `getRemoteCodeCoverageInformation`.
1818
- (Not a BC break) Some protected properties of the `BrowserTestCase` class are private now. Affected properties: `sessionStrategyManager`, `remoteCoverageHelper`, `sessionStrategy`.
1919
- Bumped minimal required `Behat/Mink` version to 1.8 (needed after `SessionProxy` class removal).
20+
- Shared session strategy now also closes popups left order from the previous test before switching back to the main window.
2021

2122
### Fixed
2223
- The remote code coverage collection cookies were set even, when the remote code coverage script URL wasn't specified.

library/aik099/PHPUnit/Session/SharedSessionStrategy.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,18 @@ protected function stopAndForgetSession()
9898
*/
9999
private function _switchToMainWindow()
100100
{
101-
$this->_session->switchToWindow(null);
101+
$this->_session->switchToWindow();
102+
$actual_initial_window_name = $this->_session->getWindowName(); // Account for initial window rename.
103+
104+
foreach ( $this->_session->getWindowNames() as $name ) {
105+
if ( $name === $actual_initial_window_name ) {
106+
continue;
107+
}
108+
109+
$this->_session->switchToWindow($name);
110+
$this->_session->executeScript('window.close();');
111+
$this->_session->switchToWindow();
112+
}
102113
}
103114

104115
/**

tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SharedSessionStrategyTest extends BrowserStackAwareTestCase
3434
/**
3535
* @large
3636
*/
37-
public function testOne()
37+
public function testOpensPage()
3838
{
3939
$session = $this->getSession();
4040
$session->visit('https://www.google.com');
@@ -44,14 +44,37 @@ public function testOne()
4444

4545
/**
4646
* @large
47-
* @depends testOne
47+
* @depends testOpensPage
4848
*/
49-
public function testTwo()
49+
public function testUsesOpenedPage()
5050
{
5151
$session = $this->getSession();
5252
$url = $session->getCurrentUrl();
5353

5454
$this->assertStringContainsString('https://www.google.com', $url);
5555
}
5656

57+
public function testOpensPopups()
58+
{
59+
$session = $this->getSession();
60+
$session->visit('https://the-internet.herokuapp.com/windows');
61+
62+
$page = $session->getPage();
63+
$page->clickLink('Click Here');
64+
$page->clickLink('Click Here');
65+
66+
$this->assertCount(3, $session->getWindowNames()); // Main window + 2 popups.
67+
}
68+
69+
/**
70+
* @depends testOpensPopups
71+
*/
72+
public function testNoPopupsBeforeTest()
73+
{
74+
$session = $this->getSession();
75+
$this->assertEquals('https://the-internet.herokuapp.com/windows', $session->getCurrentUrl());
76+
77+
$this->assertCount(1, $session->getWindowNames()); // Main window.
78+
}
79+
5780
}

tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testSessionSharing(\Exception $e = null)
7272
$this->_originalStrategy->shouldReceive('session')->once()->with($browser)->andReturn($this->_session1);
7373
$this->_originalStrategy->shouldReceive('isFreshSession')->once()->andReturn(true);
7474

75-
$this->_session1->shouldReceive('switchToWindow')->once();
75+
$this->expectNoPopups($this->_session1);
7676

7777
$this->assertSame($this->_session1, $this->strategy->session($browser));
7878
$this->assertTrue($this->strategy->isFreshSession(), 'First created session must be fresh');
@@ -85,6 +85,21 @@ public function testSessionSharing(\Exception $e = null)
8585
$this->assertFalse($this->strategy->isFreshSession(), 'Reused session must not be fresh');
8686
}
8787

88+
/**
89+
* Expects no popups.
90+
*
91+
* @param MockInterface $session Session.
92+
*
93+
* @return void
94+
*/
95+
protected function expectNoPopups(MockInterface $session)
96+
{
97+
// Testing if popup windows are actually closed will be done in the integration test.
98+
$session->shouldReceive('switchToWindow')->atLeast()->once();
99+
$session->shouldReceive('getWindowName')->once()->andReturn('initial-window-name');
100+
$session->shouldReceive('getWindowNames')->once()->andReturn(array('initial-window-name'));
101+
}
102+
88103
/**
89104
* Returns exceptions, that doesn't reset session.
90105
*
@@ -121,7 +136,7 @@ public function testSessionResetOnFailure()
121136

122137
$this->_session1->shouldReceive('isStarted')->once()->andReturn(true);
123138
$this->_session1->shouldReceive('stop')->once();
124-
$this->_session2->shouldReceive('switchToWindow')->once();
139+
$this->expectNoPopups($this->_session2);
125140

126141
$session = $this->strategy->session($browser);
127142
$this->assertSame($this->_session1, $session);

0 commit comments

Comments
 (0)