PHPUnit,Selenium基本测试因致命错误而失败

时间:2011-10-31 05:47:03

标签: php selenium phpunit tostring

我正在从Github运行PHP 5.3.6和最新版本的PHPUnit。当我从文档中复制示例17.1时,当assertTitle失败时,它会遇到致命错误。我收到此错误消息:

Fatal error: Call to a member function toString() on a non-object in <path>/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumTestCase.php on line 1041

当我将断言更改为pass时,PHPUnit运行得很好。

我挖出了这条线,这就是片段:

protected function onNotSuccessfulTest(Exception $e)
    {
        if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
            $buffer  = 'Current URL: ' . $this->drivers[0]->getLocation() .
                       "\n";
            $message = $e->getComparisonFailure()->toString();

$ e-&gt; getComparisonFailure()返回NULL,而不是对象。我做错了什么?

更新:

我找到了失败的原因,虽然目前尚无法解决问题。

在PHPUnit / Framework / Constraint.php的第92行,它使用

进行调用

$this->fail($other,$description)

定义为:

protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)

由于没有传递PHPUnit_Framework_ComparisonFailure,因此在PHPUnit / Framework / Constraint.php的第141行传递NULL

  

抛出新PHPUnit_Framework_ExpectationFailedException(   $ failureDescription,   $ comparisonFailure   );

由getComparisonFailure()提取,它应该返回一个对象,如上所述。

还有什么想法?

3 个答案:

答案 0 :(得分:4)

只需将$message = $e->getComparisonFailure()->toString();替换为$message = $e->getComparisonFailure()->exceptionToString;

即可

它适用于我

答案 1 :(得分:3)

此问题与Github上的PHPUnit-Selenium项目中的issue #66有关。我做了一个修复这个问题的提交,修复了Matthew提到的"setCustomMessage()" problem。我认为one commit in PHPUnit 3.6

引入了两个问题
$message = $e->getComparisonFailure()->toString();

改为

的地方
// ?: Is "comparison failure" set and therefore an object? (fixes issue #66)
if(is_object($e->getComparisonFailure())) {
    // -> Comparison failure is and object and should therefore have the toString method
    $message = $e->getComparisonFailure()->toString();
}
else {
    // -> Comparison failure is not an object. Lets use exception message instead
    $message = $e->getMessage();
}

在onNotSuccessfulTest()结束时:

$e->setCustomMessage($buffer);

改为:

// ?: Does the setCustomMessage method exist on the exception object? (fixes issue #67)
if(method_exists($e, 'setCustomMessage')) {
    // -> Method setCustomMessage does exist on the object, this means we are using PHPUnit 
    //    between 3.4 and 3.6
    $e->setCustomMessage($buffer);
}
else {
    // -> Method setCustomerMessages does not exist on the object, must make a new exception to 
    //    add the new message (inc. current URL, screenshot path, etc)
    $e = new PHPUnit_Framework_ExpectationFailedException($buffer, $e->getComparisonFailure());
}

希望这两个更改都将被接受到PHPUnit-Selenium项目中。至少它修复了我从PEAR获得PHPUnit和PHPUnit-Selenium最新更新的问题。

答案 2 :(得分:1)

我将$message = $e->getComparisonFailure()->toString();替换为:

if($e->getComparisonFailure() == NULL)
{
    $message = $e;
}
else
{
    $message = $e->getComparisonFailure()->toString();
}

我还将PHPUnit_Framework_ExpectationFailedException恢复为3.5.3,因此它有setCustomMessage()