Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a screenshot with PHPUnit and Selenium2 when the test fails?

I'm using PHPUnit 4.6 and PHPUnit Selenium 1.4.2 with PhantomJS. I want capture a screenshot with the last page when selenium test fails. In PHPUnit Manual there is a example for Selenium 1, but I'm trying use with Selenium 2, because I need use GhostDriver.

WebTestCase.php

class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
    protected $captureScreenshotOnFailure = TRUE;
    protected $screenshotPath = '/../../screenshots';
    protected $screenshotUrl = 'http://localhost:8080/screenshots';

    protected function setUp() {
        $this->setBrowser('phantomjs');
        $this->setBrowserUrl("http://localhost:8080");
        $this->setHost('localhost');
    }
}

Test.php

class Test extends WebTestCase
{

    public function testTitle()
    {
        $this->url('');
        assertEquals($this->title(), "My App");
    }
}

But this not capture a screenshot.

$ vendor/bin/phpunit 
PHPUnit 4.6-ge85198b by Sebastian Bergmann and contributors.

Configuration read from /MyApp/phpunit.xml

F

Time: 231 ms, Memory: 5.50Mb

There was 1 failure:

1) Test::testTitle
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-''
+'My App'

/MyApp/tests/functional/Test.php:7

FAILURES!                            
Tests: 1, Assertions: 1, Failures: 1.
like image 427
acfreitas Avatar asked Dec 08 '22 04:12

acfreitas


1 Answers

Hmm. The difference between SeleniumTestCase and Selenium2TestCase is not really good documented in the PHPUnit Manual. Also there is no clear separation and not enough usage examples for common cases on Selenium2.

$captureScreenshotOnFailure does not exist on PHPUnit_Extensions_Selenium2TestCase.

Anyway, let's try putting this together:

<?php
class Test extends PHPUnit_Extensions_Selenium2TestCase
{

    protected function setUp() {
        $this->setBrowser('phantomjs');
        $this->setBrowserUrl("http://localhost:8080");
        $this->setHost('localhost');
    }

    public function testEnterText()
    {
        $this->url("/");

        try {

            $this->assertEquals($this->title(), "My App");

        } catch (Exception $e) {

            $this->screenshot( __DIR__.'/'.$this->getName().'-'.time(). '.png');    
        }
    }

    public function screenshot($file) 
    {
        $filedata = $this->currentScreenshot();
        file_put_contents($file, $filedata);
    }
}

The try-catch-block: in the try part the assertion is done, if the assertion fails, the exception is caught. The catch-block gives us a chance to (grab details of the exception or re-throw it or) make a screenshot.

The main function is $this->currentScreenshot(), which was used in this test https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php#L733

ScreenshotListener

Please note that there is a ScreenshotListener around, which might be worth looking at: https://github.com/giorgiosironi/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/ScreenshotListener.php

With usage example over at https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCase/ScreenshotListenerTest.php

This might be a cleaner implementation to grab test failures and make shots.

like image 156
Jens A. Koch Avatar answered Dec 11 '22 08:12

Jens A. Koch