Keeping selenium browser open with phpunit / selenium

when the tests fail, the browser on which the selenium tests were run closes. this is useless when trying to debug. I know that I have the ability to take a screen if it crashes, but this does not help without all the context. with an available browser, I can drop it back and check what happens.

Is there a way to open the browser even if the claims do not work or the elements are not found?

+7
source share
6 answers

found out by chance a few weeks later.

when starting the server, use the -browserSessionReuse option at the end. this will keep one browser session open during the tests and not close when it fails

+10
source

You can call selenium.stop () at the end of the test. You need to comment on this line of code and make sure your selenium object is not destroyed at the end of the test. This will open a window. The same question was answered here.

+1
source

Regarding the answer to -browserSessionReuse on https://stackoverflow.com/a/3/355356/ ... , keep in mind that you will have to deal with clearing cookies between tests. Otherwise, it may be difficult for you to debug errors.

+1
source

After hours of trying to create a relatively clean workaround, I now resorted to a very dirty but very easy hack. The source code for phpunit \ selenium is not easy to override; it would be nice if factories were created to create Session objects. I tried to hack the creation of the object by redefining and proxying the objects, but still could not get it to work. Finally, I did not want to spend more time on it and went along the simplest and most dirty path, i.e. Directly hacked the code of the classes PHPUnit_Extensions_Selenium2TestCase_Session and PHPUnit_Extensions_Selenium2TestCase. A static $ _instance is inserted into PHPUnit_Extensions_Selenium2TestCase, which can be used to check for a test failure condition. In PHPUnit_Extensions_Selenium2TestCase_Session, they intercepted the stop () method and requested the status of TestCase. Works like a charm, but dirty dirty dirty.

PHPUnit_Extensions_Selenium2TestCase:

private static $_instance; public static function getInstance() { return self::$_instance; } .... public function __construct($name = NULL, array $data = array(), $dataName = '') { self::$_instance = $this; .... } 

PHPUnit_Extensions_Selenium2TestCase_Session:

 public function stop() { if ($this->stopped || $this->hasFailedTests()) { return; } .... } private function hasFailedTests() { $status = PHPUnit_Extensions_Selenium2TestCase::getInstance()->getStatus(); return $status == \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR || $status == \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE; } 
0
source

Here's a cleaner solution. All classes associated with session creation are redefined. A session class is a session close, so the stop () method must be redefined.

TODO: Add options to control behavior if and when to leave the browser window open or not.

Overridden 5 classes: PHPUnit_Extensions_Selenium2TestCase, PHPUnit_Extensions_Selenium2TestCase_Driver, PHPUnit_Extensions_Selenium2TestCase_Session, PHPUnit_Extensions_Selenium2TestCase_SessionStrategy_Isium_itium_estition_test_test_test_test_sion Created 2 boot files, 1 for shared browser sessions, 1 for isolated browser sessions. Shows 1 test example.

OkxSelenium2TestCase:

 namespace OKInfoTech\PhpUnit\Selenium; abstract class OkxSelenium2TestCase extends \PHPUnit_Extensions_Selenium2TestCase { private static $_instance; public static function getInstance() { return self::$_instance; } public function __construct($name = NULL, array $data = array(), $dataName = '') { self::$_instance = $this; parent::__construct($name, $data, $dataName); $params = array( 'host' => 'localhost', 'port' => 4444, 'browser' => NULL, 'browserName' => NULL, 'desiredCapabilities' => array(), 'seleniumServerRequestsTimeout' => 60 ); $this->setUpSessionStrategy($params); } protected function setUpSessionStrategy($params) { parent::setUpSessionStrategy($params); if (isset($params['sessionStrategy'])) { $strat = $params['sessionStrategy']; switch ($strat) { case "isolated": self::$browserSessionStrategy = new OkxSelenium2TestCase_SessionStrategy_Isolated; break; case "shared": self::$browserSessionStrategy = new OkxSelenium2TestCase_SessionStrategy_Shared(new OkxSelenium2TestCase_SessionStrategy_Isolated); break; } } else { self::$browserSessionStrategy = new OkxSelenium2TestCase_SessionStrategy_Isolated; } $this->localSessionStrategy = self::$browserSessionStrategy; } } 

OkxSelenium2TestCase_Driver:

 namespace OKInfoTech\PhpUnit\Selenium; class OkxSelenium2TestCase_Driver extends \PHPUnit_Extensions_Selenium2TestCase_Driver { private $seleniumServerUrl; private $seleniumServerRequestsTimeout; public function __construct(\PHPUnit_Extensions_Selenium2TestCase_URL $seleniumServerUrl, $timeout = 60) { $this->seleniumServerUrl = $seleniumServerUrl; $this->seleniumServerRequestsTimeout = $timeout; parent::__construct($seleniumServerUrl, $timeout); } public function startSession(array $desiredCapabilities, \PHPUnit_Extensions_Selenium2TestCase_URL $browserUrl) { $sessionCreation = $this->seleniumServerUrl->descend("/wd/hub/session"); $response = $this->curl('POST', $sessionCreation, array( 'desiredCapabilities' => $desiredCapabilities )); $sessionPrefix = $response->getURL(); $timeouts = new \PHPUnit_Extensions_Selenium2TestCase_Session_Timeouts( $this, $sessionPrefix->descend('timeouts'), $this->seleniumServerRequestsTimeout * 1000 ); return new OkxSelenium2TestCase_Session( $this, $sessionPrefix, $browserUrl, $timeouts ); } } 

OkxSelenium2TestCase_Session:

 namespace OKInfoTech\PhpUnit\Selenium; class OkxSelenium2TestCase_Session extends \PHPUnit_Extensions_Selenium2TestCase_Session { public function stop() { if ($this->hasFailedTests()) { return; } parent::stop(); } private function hasFailedTests() { $status = OkxSelenium2TestCase::getInstance()->getStatus(); return $status == \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR || $status == \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE; } } 

OkxSelenium2TestCase_SessionStrategy_Isolated:

 namespace OKInfoTech\PhpUnit\Selenium; class OkxSelenium2TestCase_SessionStrategy_Isolated extends \PHPUnit_Extensions_Selenium2TestCase_SessionStrategy_Isolated { public function session(array $parameters) { $seleniumServerUrl = \PHPUnit_Extensions_Selenium2TestCase_URL::fromHostAndPort($parameters['host'], $parameters['port']); $driver = new OkxSelenium2TestCase_Driver($seleniumServerUrl, $parameters['seleniumServerRequestsTimeout']); $capabilities = array_merge($parameters['desiredCapabilities'], array( 'browserName' => $parameters['browserName'] )); $session = $driver->startSession($capabilities, $parameters['browserUrl']); return $session; } } 

OkxSelenium2TestCase_SessionStrategy_Shared:

 namespace OKInfoTech\PhpUnit\Selenium; class OkxSelenium2TestCase_SessionStrategy_Shared extends \PHPUnit_Extensions_Selenium2TestCase_SessionStrategy_Shared { } 

bootstrapIsolated.php:

 require_once dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php'; spl_autoload_register( function ($class) { static $classes = NULL; static $path = NULL; if ($classes === NULL) { $classes = [ 'okinfotech\phpunit\selenium\okxselenium2testcase' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_driver' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_Driver.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_session' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_Session.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_sessionstrategy_isolated' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_SessionStrategy_Isolated.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_sessionstrategy_shared' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_SessionStrategy_Shared.php', ]; $path = dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/'; } $cn = strtolower($class); if (isset($classes[$cn])) { require $path . $classes[$cn]; } } ); use OKInfoTech\PhpUnit\Selenium\OkxSelenium2TestCase; OkxSelenium2TestCase::shareSession(false); 

bootstrapShared.php:

 require_once dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php'; spl_autoload_register( function ($class) { static $classes = NULL; static $path = NULL; if ($classes === NULL) { $classes = [ 'okinfotech\phpunit\selenium\okxselenium2testcase' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_driver' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_Driver.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_session' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_Session.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_sessionstrategy_isolated' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_SessionStrategy_Isolated.php', 'okinfotech\phpunit\selenium\okxselenium2testcase_sessionstrategy_shared' => 'okinfotech/phpunit-selenium/PHPUnit/Extensions/OkxSelenium2TestCase_SessionStrategy_Shared.php', ]; $path = dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/'; } $cn = strtolower($class); if (isset($classes[$cn])) { require $path . $classes[$cn]; } } ); use OKInfoTech\PhpUnit\Selenium\OkxSelenium2TestCase; OkxSelenium2TestCase::shareSession(true); 

Selenium02Test:

 class Selenium02Test extends OkxSelenium2TestCase { public static function setUpBeforeClass() { } public static function tearDownAfterClass() { } public function setUp() { $this->setHost('...'); $this->setPort(4444); $this->setBrowser('chrome'); //$this->setBrowser('firefox'); $this->setBrowserUrl('...'); } public function tearDown() { } public function testPageExists() { $this->url('/'); // test if browser window stays open when test fails $this->assertTrue(false); } } 
0
source

A simple solution is to set the $ stop session property to true in the test setUp () method. Since the property is private, we need to use the Reflection api.

  function setUp() { .... // Set the private property $stopped of session to true, so the window is not closed on error or at the end of tests $myClassReflection = new \ReflectionClass( get_class( $this->prepareSession() ) ); $secret = $myClassReflection->getProperty( 'stopped' ); $secret->setAccessible( true ); $secret->setValue( $this->prepareSession(), true ); ... } 
0
source

All Articles