|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Behat\Mink\Tests\Driver\Js; |
| 4 | + |
| 5 | +use Behat\Mink\Tests\Driver\TestCase; |
| 6 | + |
| 7 | +final class SessionResetTest extends TestCase |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @dataProvider initialWindowNameDataProvider |
| 11 | + */ |
| 12 | + public function testSessionResetClosesWindows(?string $initialWindowName): void |
| 13 | + { |
| 14 | + $session = $this->getSession(); |
| 15 | + $session->visit($this->pathTo('/window.html')); |
| 16 | + |
| 17 | + if (null !== $initialWindowName) { |
| 18 | + $session->executeScript('window.name = "'.$initialWindowName.'";'); |
| 19 | + } |
| 20 | + |
| 21 | + $page = $session->getPage(); |
| 22 | + |
| 23 | + $page->clickLink('Popup #1'); |
| 24 | + $page->clickLink('Popup #2'); |
| 25 | + |
| 26 | + $expectedInitialWindowName = $session->evaluateScript('window.name'); |
| 27 | + |
| 28 | + $windowNames = $session->getWindowNames(); |
| 29 | + $this->assertCount(3, $windowNames, 'Incorrect opened window count.'); // Initial window + 2 opened popups. |
| 30 | + |
| 31 | + $session->reset(); |
| 32 | + |
| 33 | + $windowNames = $session->getWindowNames(); |
| 34 | + $this->assertCount(1, $windowNames, 'Incorrect opened window count.'); // Initial window only. |
| 35 | + |
| 36 | + $actualInitialWindowName = $session->evaluateScript('window.name'); |
| 37 | + $this->assertEquals($expectedInitialWindowName, $actualInitialWindowName, 'Not inside an initial window.'); |
| 38 | + } |
| 39 | + |
| 40 | + public static function initialWindowNameDataProvider(): array |
| 41 | + { |
| 42 | + return array( |
| 43 | + 'no name' => array(null), |
| 44 | + 'non-empty name' => array('initial-window'), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @after |
| 50 | + */ |
| 51 | + protected function resetSessions() |
| 52 | + { |
| 53 | + $session = $this->getSession(); |
| 54 | + |
| 55 | + // Stop the session instead of resetting, because resetting behavior is being tested. |
| 56 | + if ($session->isStarted()) { |
| 57 | + $session->stop(); |
| 58 | + } |
| 59 | + |
| 60 | + parent::resetSessions(); |
| 61 | + } |
| 62 | +} |
0 commit comments