phpselenium-webdrivernetbeansphpunit

Selenium PHP "verify" fails do not include line numbers


Methods like verifyText do not report the line number of the failure, making it hard to find the point of failure.

The code created by Selenium IDE PHPUnit export looks like this:

try {
    $this->assertEquals($text, $this->getTitle());
} catch (PHPUnit_Framework_AssertionFailedError $e) {
    array_push($this->verificationErrors, $e->toString());
}

The output for this line looks like line 2 below, which is completely untraceable

Failed asserting that '' matches PCRE pattern "/Harlem/".
Failed asserting that two strings are equal.
Failed asserting that '(Pattern A)' matches PCRE pattern "/\(Pattern B\)/".

I've amended the call to include the referenced text which lets me search for the text failure, but in a large test this is not sufficient. How do I get the line number/stacktrace for each verify failure within my code?

public function verifyTitle($text) {
    $title = $this->getTitle();
    try {
        $this->assertEquals($text, $title);
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors,
             "Title is '$title' but should be '$text'");
    }
}

Note: in order to get stacktraces returning references to my code for asserts, I am using the stacktrace hack


Solution

  • Created this verification Method to include (too much) stacktrace:

    public function appendVerification($message,$e) {
            array_push($this->verificationErrors,$message."\n".$this->dumpStack($e));
    }
    

    I also updated the referenced dumpStack method for PHPUnit tests to dumbly strip out framework calls by class name

    protected function dumpStack(Exception $e) {
        $stack = '';
        foreach ($e->getTrace() as $trace) {
            if (isset($trace['file']) &&
                    isset($trace['line'])) {
                if (!isFramework($trace['file']))
                    $stack .= PHP_EOL .
                            $trace['file'] . ':' .
                            $trace['line'] . ' ';
            }
        }
        return $stack;
    }
    
    function isFramework($fileName) {
       $test = ((preg_match("/PHPUnit/i",$fileName) +
               preg_match("/php.phpunit/i",$fileName)) > 0);
       return $test;
    }
    

    End result, clickable in netbeans, free of any PHPUnit framework line numbers

    Failed asserting that 'waitForElementPresent failed for selector: css=input#address.valid' is false.
    C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:228
    C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:115
    C:\dev\Automation_Dev\phpTests\library\SeleniumTestCase.php:176
    C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:72
    C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:39
    C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:23
    C:\xampp\php\phpunit:46