PHPUnit has an option to take a screenshot upon a Selenium test case failure. However, the screenshot filename generated is a hash of something - I don't know what exactly. While the test result report allows me to match a particular failed test case with a screenshot filename, this is troublesome to use.

If I could rename the screenshot to use the message from the failed assert as well as a timestamp for instance, it makes the screenshots much easier to cross-reference. Is there any way to rename the generated screenshot filename at all?

有帮助吗?

解决方案 2

I ended up using a modified version of @sectus' answer:

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

Although the conditional check in tearDown() works fine, based on Extending phpunit error message, I decided to go with onNotSuccessfulTest() as it seemed cleaner.

The filename could not accept colons :, or I would get an error message from file_get_contents: failed to open stream: Protocol error

The function currentScreenshot also did not exist, so I ended up taking the screenshot in a different way according to http://www.devinzuczek.com/2011/08/taking-a-screenshot-with-phpunit-and-selenium-rc/.

其他提示

You could try something like this (it's works with 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());
    }
}

Also uncheck

protected $captureScreenshotOnFailure = FALSE;

Another method I played around with, as I still wanted to use $this->screenshotUrl and $this->screenshotPath for convenient configuration:

I overwrote takeScreenshot from https://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 '';
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top