Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screenshot with Selenium Webdriver and PHPUnit

I know that screenshots could be taken by extending PHPUnit_Extensions_Selenium2TestCase but right now, I'm using Facebook's php-webdriver so I'm just extending with PHPUnit_Framework_TestCase

Is there anyway to take screenshots with this just the driver and not extending the extension?

Thanks.

like image 516
jaggy Avatar asked Sep 06 '25 03:09

jaggy


2 Answers

$driver->takeScreenshot('/path/to/image.png');
like image 77
gontrollez Avatar answered Sep 08 '25 00:09

gontrollez


I found such solution to make screenshot on failure:

/**
 * {@inheritdoc}
 */
public function tearDown()
{
    $status = $this->getStatus();
    if ($status == \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR 
        || $status == \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
        $now = new \DateTime('now');
        $screenshotFileName = $this->getParameter('screenshots_directory_full_path');
        $screenshotFileName = $screenshotFileName . $now->format('Y-m-d H:i:s') . ' test ' . $this->getName() . '.png';

        $this->webDriver->takeScreenshot($screenshotFileName);
    }
    $this->webDriver->quit();
}

You asked this long time ago, but hope it'll help somebody else :)

like image 39
a_sarana Avatar answered Sep 07 '25 23:09

a_sarana