【问题标题】:How to capture a screenshot with PHPUnit and Selenium2 when the test fails?测试失败时如何使用 PHPUnit 和 Selenium2 截屏?
【发布时间】:2015-04-02 15:21:24
【问题描述】:

我将 PHPUnit 4.6 和 PHPUnit Selenium 1.4.2 与 PhantomJS 一起使用。当硒测试失败时,我想用最后一页截屏。 在PHPUnit Manual 中有一个 Selenium 1 的示例,但我正在尝试使用 Selenium 2,因为我需要使用 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");
    }
}

但这不是截图。

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

【问题讨论】:

    标签: testing selenium phpunit phantomjs ghostdriver


    【解决方案1】:

    嗯。 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有使用示例

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

    【讨论】:

    【解决方案2】:

    结合@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);
        }
    }
    

    【讨论】:

    • tearDown 似乎不需要。
    • tearDown 提前结束会话 - Screenshot generation doesn't work. The command http://localhost:4444/wd/hub/session/hash/screenshot is not recognized by the server. - phpunit-selenium": "^7.0" and phpunit": "7.5"
    【解决方案3】:

    在所有 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 测试类的名称作为文件名。

    【讨论】:

      【解决方案4】:

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

          $fp = fopen('path/35.png', 'wb');
          fwrite($fp, $this->currentScreenshot());
          fclose($fp);
          sleep(1);
      

      【讨论】:

        猜你喜欢
        • 2015-09-29
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-24
        • 2018-05-12
        相关资源
        最近更新 更多