PHPUnit Selenium Server - Best / Custom Error Handling?

Is there a way that PHPUnit can continue after an error? For example, I have a large test suite (400+ steps), and I would prefer that if he said the element was not found, it would not stop the rest of my script.

+4
source share
2 answers

There is a better way to do this. Instead of overloading each assert * () method, you can only overload one method: runTest (). This is for every statement, and exceptions can be caught:

abstract class AMyTestCase extends PHPUnit_Framework_TestCase { public function runTest() { try { parent::runTest(); } catch ( MyCustomException $Exc ) { // will continue tests } catch ( Exception $Exc ) { if ( false === strpos($Exc->getMessage(), 'element not found') ) { // rethrow: throw $Exc; } // will also continue } } } 
+1
source

We do the same in our selenium tests. You need to catch the exceptions caused by assertion errors, and the only way to do this is to create your own test case base class that overrides the assertion methods. You can save fault messages and end the test at the end with a test listener.

I don't have the code in front of me, but it was pretty straight forward. For instance,

 abstract class DelayedFailureSeleniumTestCase extends PHPUnit_Extension_SeleniumTestCase { public function assertElementText($element, $text) { try { parent::assertElementText($element, $text); } catch (PHPUnit_Framework_AssertionFailedException $e) { FailureTrackerListener::addAssertionFailure($e->getMessage()); } } ... other assertion functions ... } class FailureTrackerListener implements PHPUnit_Framework_TestListener { private static $messages; public function startTest() { self::$messages = array(); } public static function addAssertionFailure($message) { self::$messages[] = $message; } public function endTest() { if (self::$messages) { throw new PHPUnit_Framework_AssertionFailedException( implode("\n", self::$messages)); } } } 
+2
source

All Articles