Testing 当测试失败时,如何使用PHPUnit和Selenium2捕获屏幕截图?
我将PHPUnit 4.6和PHPUnit Selenium 1.4.2用于PhantomJS。我想在selenium测试失败时捕获最后一页的屏幕截图。 中有一个Selenium 1的示例,但我正在尝试使用Selenium 2,因为我需要使用GhostDriver WebTestCase.phpTesting 当测试失败时,如何使用PHPUnit和Selenium2捕获屏幕截图?,testing,selenium,phpunit,phantomjs,ghostdriver,Testing,Selenium,Phpunit,Phantomjs,Ghostdriver,我将PHPUnit 4.6和PHPUnit Selenium 1.4.2用于PhantomJS。我想在selenium测试失败时捕获最后一页的屏幕截图。 中有一个Selenium 1的示例,但我正在尝试使用Selenium 2,因为我需要使用GhostDriver WebTestCase.php class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase { protected $captureScreenshotOnFa
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.
嗯。在PHPUnit手册中并没有很好地记录SeleniumTestCase和Selenium2TestCase之间的差异。此外,对于硒M2的常见情况,也没有明确的分离和足够的使用示例 上不存在$captureScreenshotOnFailure 无论如何,让我们试着把这些放在一起:
<?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);
}
}
跨所有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测试类的名称作为文件名 结合和的解决方案,我们得到:
<?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);
}
}
使用此选项保存屏幕截图..对于无头浏览器非常有用
$fp = fopen('path/35.png', 'wb');
fwrite($fp, $this->currentScreenshot());
fclose($fp);
sleep(1);
ScreenshotListener需要添加任何异常。我想拍一个截图,如果只有一些测试失败,是可能的吗?谢谢你的提醒。是的。请看一下代码。请参阅函数TestStoresScreenshotinCaseofFailure:tearDown
似乎不需要。tearDown结束会话以提前生成-屏幕截图不起作用。命令http://localhost:4444/wd/hub/session/hash/screenshot 服务器无法识别。
-phpunit selenium:“^7.0”和phpunit:“7.5”