4

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.
4

4 回答 4

6

唔。SeleniumTestCase 和 Selenium2TestCase 之间的区别并没有很好地记录在 PHPUnit 手册中。对于 Selenium2 上的常见情况,也没有明确的分隔和足够的使用示例。

$captureScreenshotOnFailure 在PHPUnit_Extensions_Selenium2TestCase上不存在 。

无论如何,让我们试着把它放在一起:

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

try-catch-block:在try部分断言完成,如果断言失败,则捕获异常。catch-block 让我们有机会(获取异常的详细信息或重新抛出它或)制作屏幕截图。

主要函数是$this->currentScreenshot(),在本次测试中用到了 https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php#L733

截图监听器

请注意,周围有一个 ScreenshotListener,可能值得一看: https ://github.com/giorgiosironi/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/ScreenshotListener.php

使用示例在https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCase/ScreenshotListenerTest.php

这可能是一个更简洁的实现来抓取测试失败并进行拍摄。

于 2015-02-03T14:12:16.117 回答
6

结合@Jens A. Koch和@John Joseph的解决方案,我们得到:

<?php

class homepageTest extends PHPUnit_Extensions_Selenium2TestCase {

    private $listener;

    public function setUp() {

        // Your screenshots will be saved in '/var/www/vhosts/screenshots/'
        $screenshots_dir = '/var/www/vhosts/screenshots/';

        $this->listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);

        $this->setBrowser('firefox');
        $this->setBrowserUrl('https://netbeans.org');
    }

    public function testNetbeansContainsHorses() {
        $this->url('https://netbeans.org');
        $this->assertContains('Equestrian', $this->title()); // Will fail on NetBeans page.
    }


    public function onNotSuccessfulTest($e) {
        $this->listener->addError($this, $e, microtime(true));        
        parent::onNotSuccessfulTest($e);
    }
}
于 2016-05-23T03:31:32.983 回答
2

在所有 Web 测试中执行此操作的一种方法是覆盖父测试用例类中的一个测试失败函数,并在那里捕获您的屏幕截图。

例子:

class MyBaseWebTests
{

    $this->directory = '/some_path_to_put_screenshots_in/';

    // Override PHPUnit_Extensions_Selenium2TestCase::onNotSuccessfulTest
    public function onNotSuccessfulTest(Exception $e)
    {
        $filedata   = $this->currentScreenshot();
        $file       = $this->directory . get_class($this) . '.png';
        file_put_contents($file, $filedata);

        parent::onNotSuccessfulTest($e);
    }
}

现在,在您的任何 Web 测试失败后,他们会将屏幕截图转储到该文件夹​​中,并以 Web 测试类的名称作为文件名。

于 2015-08-14T13:52:48.623 回答
0

使用它来保存屏幕截图..在无头浏览器的情况下非常有用。

    $fp = fopen('path/35.png', 'wb');
    fwrite($fp, $this->currentScreenshot());
    fclose($fp);
    sleep(1);
于 2017-07-21T10:37:58.520 回答