【问题标题】:Selenium PHP "verify" fails do not include line numbersSelenium PHP“验证”失败不包括行号
【发布时间】:2012-05-08 23:03:52
【问题描述】:

verifyText等方法不报告失败的行号,很难找到失败点。

Selenium 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

【问题讨论】:

    标签: netbeans selenium phpunit


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      相关资源
      最近更新 更多