作者:GIfi炬辉_904 | 来源:互联网 | 2023-05-23 17:43
我正在使用PHPUnit 4.6和PHPUnit Selenium 1.4.2与PhantomJS.当selenium测试失败时,我想捕获最后一页的截图.在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.
Silas Palmer..
6
结合@Jens A. Koch和@John Joseph的解决方案,我们得到了这个:
listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);
$this->setBrowser('firefox');
$this->setBrowserUrl('https://netbeans.org');
}
public function tearDown() {
$this->stop();
}
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, null);
parent::onNotSuccessfulTest($e);
}
}
Jens A. Koch..
5
嗯.SeleniumTestCase和Selenium2TestCase之间的区别在PHPUnit手册中没有很好的记录.对于Selenium2,常见案例也没有明确的分离和充分的使用示例.
PHPUnit_Extensions_Selenium2TestCase上不存在$ captureScreenshotOnFailure
.
无论如何,让我们尝试将它们组合在一起:
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
请注意,有一个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
这可能是一个更清晰的实现,以抓住测试失败和拍摄.
1> Silas Palmer..:
结合@Jens A. Koch和@John Joseph的解决方案,我们得到了这个:
listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);
$this->setBrowser('firefox');
$this->setBrowserUrl('https://netbeans.org');
}
public function tearDown() {
$this->stop();
}
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, null);
parent::onNotSuccessfulTest($e);
}
}
2> Jens A. Koch..:
嗯.SeleniumTestCase和Selenium2TestCase之间的区别在PHPUnit手册中没有很好的记录.对于Selenium2,常见案例也没有明确的分离和充分的使用示例.
PHPUnit_Extensions_Selenium2TestCase上不存在$ captureScreenshotOnFailure
.
无论如何,让我们尝试将它们组合在一起:
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
请注意,有一个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
这可能是一个更清晰的实现,以抓住测试失败和拍摄.