Selenium doesn't show bad line numbers

I have a Selenium server working with PHPUnit on a local MAMP server.

If the Assert error fails, the line with the failed number is not displayed, instead I see the phpunit number line.

When I run the phpunit-only test, I see the number line of the failed statement.

Test only for PHPUnit

$ cd '/Applications/MAMP/htdocs/my-client/tests' && phpunit -c 'phpunit.xml' '/Applications/MAMP/htdocs/my-client/tests/controllers/homeTest.php' PHPUnit 3.6.10 by Sebastian Bergmann. Configuration read from /Applications/MAMP/htdocs/my-client/tests/phpunit.xml . Time: 0 seconds, Memory: 8.00Mb There was 1 failure: 1) HomeTest::test_get_sections Failed asserting that Array ( blah, blah, blah ) ) is identical to Array (blah, blah, blah2 ) ). /Applications/MAMP/htdocs/my-client/tests/controllers/homeTest.php:56 /Applications/MAMP/bin/php/php5.3.6/bin/phpunit:46 FAILURES! Tests: 2, Assertions: 3, Failures: 1. 

PHPUnit Selenium Test

 $ cd '/Applications/MAMP/htdocs/my-client/tests' && phpunit -c 'phpunit.xml' '/Applications/MAMP/htdocs/my-client/tests/views/af_web_Test.php' PHPUnit 3.6.10 by Sebastian Bergmann. Configuration read from /Applications/MAMP/htdocs/my-client/tests/phpunit.xml E Time: 2 seconds, Memory: 8.75Mb There was 1 error: 1) af_web_Test::test_crear_una_af_nueva_y_validar_el_valor_por_defecto_de_los_campos Current URL: http://localhost:8888/my-client/index.php/home/view Failed asserting that '' matches PCRE pattern "/0/". /Applications/MAMP/bin/php/php5.3.6/bin/phpunit:46 FAILURES! Tests: 1, Assertions: 6, Errors: 1. 
+4
source share
1 answer

I had the same problem (albeit on a LAMP server), since I completely rely on these line numbers along with screenshots to determine exactly what is happening in my tests. It is reported that the error, which I assume is such. See https://github.com/sebastianbergmann/phpunit-selenium/issues/81 for reference.

As a temporary temporary , I forcibly entered the line number in the error message in an exceptional case (because the line number, of course, can be found in the trace). As a side effect, most exceptions are overwritten, and I just throw a PHPUnit_Framework_Error, but at least I get the line number in the output. As a temporary workaround until this is fixed, this works for me.

To do this, I extend PHPUnit_Extensions_SeleniumTestCase with my own SeleniumTestCase and insert the following functions into it:

A very slightly modified version of this function for my own use: fooobar.com/questions/194564 / ...

 protected function dumpStack(Exception $e) { $stack = ''; foreach ($e->getTrace() as $trace) { if (isset($trace['file']) && isset($trace['line']) && isset($trace['class']) && isset($trace['function'])) { $stack .= PHP_EOL . $trace['file'] . ':' . $trace['line'] . ' ' . $trace['class'] . '::' . $trace['function']; } } return $stack; } 

I override onNotSuccessfulTest from PHPUnit_Extensions_SeleniumTestCase :

 protected function onNotSuccessfulTest(Exception $e) { try { parent::onNotSuccessfulTest($e); } catch (PHPUnit_Framework_IncompleteTestError $e) { // Don't do anything with the incomplete test exception throw $e; } catch (PHPUnit_Framework_SkippedTestError $e) { // Don't do anything with the skipped test exception throw $e; } catch (Exception $e_parent) { // Include line number for specific test file in error $error_msg = chr(10).chr(10).$this->dumpStack($e); throw new PHPUnit_Framework_Error($e_parent->getMessage().$error_msg, $e_parent->getCode(), $e_parent->getFile(), $e_parent->getLine(), $e_parent->getTrace()); } } 

I do not like this hack, so if someone has a better solution, I would be happy to hear it!

UPDATE: I forgot to mention this initially: you, of course, need to do tests that extend your own SeleniumTestCase, not PHPUnit_Extensions_SeleniumTestCase.

+4
source

Source: https://habr.com/ru/post/1395756/


All Articles