views:

290

answers:

2

I am using PHPUnit 3.4.12 to drive my selenium tests. I'd like to be able to get a screenshot taken automatically when a test fails. This should be supported as explained at http://www.phpunit.de/manual/current/en/selenium.html#selenium.seleniumtestcase.examples.WebTest2.php

class WebTest 
{
    protected $captureScreenshotOnFailure = true;
    protected $screenshotPath = 'C:\selenium';
    protected $screnshotUrl = 'http://localhost/screenshots';

    public function testLandingPage($selenium)
    {
            $selenium->open("http://www.example.com");
            $selenium->fail("fail");
            ...
    }
}

As you can see, I am making the test to fail and in theory when it does it should take a screenshot and put it in C:\selenium, as I am running the selenium RC server on Windows.

However, when I run the test it will just give me the following:

[root@testbox selenium]$ sh run
PHPUnit 3.4.12 by Sebastian Bergmann.

F

Time: 8 seconds, Memory: 5.50Mb

There was 1 failure:

1) WebTest::testLandingPage
fail

/home/root/selenium/WebTest.php:32

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.

I do not see any screenshot in C:\selenium. I can however get a screenshot with $selenium->captureScreenshot("C:/selenium/image.png");

Any ideas or suggestions most welcome.

Thanks

A: 

The error handling of this is rather poor on phpunit's part; if everything isn't perfect it will silently ignore your other options without a warning.

As Dave mentioned, if any of the variables are misspelled it will silently not work, and you might also try assigning them to the instance in your setUp.

Also, not every condition triggers a screenshot. Try $selenium->assertTextPresent("foobarbaz") instead of your $selenium->fail() for a sanity check.

mrooney
Thanks guys. I've figured out why this wasn't working.1) there was a typo screnshotUrl vs screenshotUrl2) $selenium->fail will not trigger the screenshot to be taken, assert functions will trigger that on failure3) my tearDown function doesn't seem to be phpunit3.4 compatible, see http://stackoverflow.com/questions/2846928/selenium-rc-throws-sessionsid-should-not-be-null-exception-with-asserttextpresent I had to remove it until that question is solved. Also, does anyone know if I can specify the file name for the screenshot?thanks heaps
If an answer such as mine has helped you, please accept it as the answer, thanks!
mrooney
A: 

you may try adding these line of codes


    try {
        $this->assertTrue($this->isTextPresent("You searched for \"Brakes\" (2 matches)"));
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors, $e->toString());
        $this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . rawurlencode($this->getLocation()) . '.png');
    }

battcor