2012-04-28 49 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