Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes popups after test finishes (shared strategy) #135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,18 @@
*/
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();

Check warning on line 111 in library/aik099/PHPUnit/Session/SharedSessionStrategy.php

View check run for this annotation

Codecov / codecov/patch

library/aik099/PHPUnit/Session/SharedSessionStrategy.php#L109-L111

Added lines #L109 - L111 were not covered by tests
}
}

/**
Expand Down
29 changes: 26 additions & 3 deletions tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading