1

PHPUnit 可以选择在 Selenium 测试用例失败时截取屏幕截图。但是,生成的屏幕截图文件名是一些东西的哈希 - 我不知道到底是什么。虽然测试结果报告允许我将特定失败的测试用例与屏幕截图文件名进行匹配,但这使用起来很麻烦。

例如,如果我可以重命名屏幕截图以使用来自失败断言的消息以及时间戳,它会使屏幕截图更容易交叉引用。有没有办法重命名生成的屏幕截图文件名?

4

3 回答 3

3

你可以尝试这样的事情(它适用于 selenium2):

protected function tearDown() {
    $status = $this->getStatus();
    if ($status == \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR || $status == \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
        $file_name = sys_get_temp_dir() . '/' . get_class($this) . ':' . $this->getName() . '_' . date('Y-m-d_H:i:s') . '.png';
        file_put_contents($file_name, $this->currentScreenshot());
    }
}

也取消选中

protected $captureScreenshotOnFailure = FALSE;
于 2013-11-04T09:30:06.807 回答
1

我最终使用了@sectus 答案的修改版本:

public function onNotSuccessfulTest(Exception $e) {
    $file_name = '/' . date('Y-m-d_H-i-s') . ' ' . $this->getName() . '.png';
    file_put_contents($this->screenshotPath . $file_name, base64_decode($this->captureEntirePageScreenshotToString()));
    parent::onNotSuccessfulTest($e);
}

尽管条件检查tearDown()工作正常,但基于Extending phpunit error message,我决定使用它,onNotSuccessfulTest()因为它看起来更干净。

文件名不能接受冒号:,否则我会收到一条错误消息file_get_contentsfailed to open stream: Protocol error

该功能currentScreenshot也不存在,所以我最终根据http://www.devinzuczek.com/2011/08/taking-a-screenshot-with-phpunit-and-selenium-rc/以不同的方式截取了屏幕截图.

于 2013-12-06T08:37:03.647 回答
0

我玩过的另一种方法,因为我仍然想使用$this->screenshotUrl$this->screenshotPath方便配置:

我覆盖takeScreenshothttps://github.com/sebastianbergmann/phpunit-selenium/blob/master/PHPUnit/Extensions/SeleniumTestCase.php

protected function takeScreenshot() {
    if (!empty($this->screenshotPath) &&
        !empty($this->screenshotUrl)) {
        $file_name = '/' . date('Y-m-d_H-i-s') . ' ' . $this->getName() . '.png';
        file_put_contents($this->screenshotPath . $file_name, base64_decode($this->captureEntirePageScreenshotToString()));

        return 'Screenshot: ' . $this->screenshotUrl . '/' . $file_name . ".png\n";
    } else {
        return '';
    }
}
于 2013-12-20T08:37:26.980 回答