2012-04-28 55 views
0

像verifyText这样的方法不会报告失败的行号,因此很难找到失败点。Selenium PHP“验证”失败不包括行号

硒IDE PHPUnit的出口产生的代码如下所示:

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

此行输出类似于下面的第2行,这是完全不可考

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

我已经修改了调用包含引用的文本可以让我搜索文本失败,但是在大型测试中这是不够的。我的代码中如何获取每次验证失败的行号/堆栈跟踪?

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'"); 
    } 
} 

注:为了得到踪迹返回引用到我的代码断言,我现在用的是stacktrace hack

回答

1

创造了这个验证方法,包括(太多)堆栈跟踪:

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

我还更新了引用的PHPUnit测试的dumpStack方法,以便根据类名称虚拟地删除框架调用

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; 
} 

最终结果,可在NetBeans中单击,不含任何PHPUnit框架行数

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