Selenium don't show failed number lines

2019-06-25 05:41发布

问题:

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

When an Assert fail, the failed number line is not showing, instead I see a phpunit number line.

When I execute a "phpunit only" test I can see the number line of the failed assert.

PHPUnit only test

$ 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.

回答1:

I've had the same problem (albeit on a LAMP server), as I quite rely on these line numbers along with screenshots to figure out exactly what's going wrong in my tests. The bug, which I assume this is, is apparently reported. See https://github.com/sebastianbergmann/phpunit-selenium/issues/81 for reference.

As a temporary workaround I've forced the line number into the error message in the exception that is being thrown (because the line number can of course be found in the trace). As a side-effect, most exceptions are rethrown and I am only throwing 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 achieve this, I extend PHPUnit_Extensions_SeleniumTestCase with my own SeleniumTestCase, and put these functions in it:

Very slightly modified version of this function for my own use: https://stackoverflow.com/a/6608751

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 don't like this hack, so if anyone has a better solution, I'd be happy to hear it!

UPDATE: I forgot to mention this initially: You of course have to make the tests extend your own custom SeleniumTestCase, and not the PHPUnit_Extensions_SeleniumTestCase.