4

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

user342775
  • 83
  • 2
  • 5
  • Doubt it's the cause of the problem, but should your `$screnshotUrl` variable be `$screenshotUrl`? – Dave Hunt May 17 '10 at 08:42
  • @Dave Hunt: Good point, and yes, this could indeed be the cause since $this->screenshotUrl MUST be filled in order to get the capture triggered. – nuqqsa May 18 '10 at 13:08

3 Answers3

2

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
  • 1,994
  • 2
  • 19
  • 30
  • Thanks guys. I've figured out why this wasn't working. 1) there was a typo screnshotUrl vs screenshotUrl 2) $selenium->fail will not trigger the screenshot to be taken, assert functions will trigger that on failure 3) 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 – user342775 May 25 '10 at 23:38
2

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

Ben
  • 54,723
  • 49
  • 178
  • 224
battcor
  • 21
  • 1
0

I recently had this error because I was following the tutorial.

The first example in the documentation is for PHPUnit_Extensions_Selenium2TestCase. All of the others on the page are for PHPUnit_Extensions_SeleniumTestCase.

Perhaps change

extends PHPUnit_Extensions_Selenium2TestCase

to

extends PHPUnit_Extensions_SeleniumTestCase
Ben
  • 54,723
  • 49
  • 178
  • 224