I'm trying to take a screenshot in onNotSuccessfulTest function. But my browser is closed in tearDown and no screenshot is taken because there is no session to take it. Is there any possible way to first take a screenshot and then quit session?
This is my code I'm using:
public function onNotSuccessfulTest($e)
{
$this->driver->takeScreenshot('test.png');
$this->driver->quit();
parent::onNotSuccessfulTest($e);
}
public function tearDown()
{
$this->driver->quit();
parent::tearDown();
}
public function testTest()
{
$this->driver->get($this->url);
$this->assertContains('Google', $this->driver->getTitle());
}
This is issue with the order in which PHPUnit executes the tearDown sequence... I guess you could do it in custom listener, like we did here: https://github.com/lmc-eu/steward/blob/master/src/Listener/SnapshotListener.php (but you must also start WebDriver in Listener...).
Or you can even use some test runner that already handles the screenshots for you on error, like https://github.com/lmc-eu/steward/ itself.
Thanks. I'll look at this.
I'm doing this with hasFailed() in tearDown()
public function tearDown()
{
if ($this->hasFailed()) {
$today = (new Date('now'))->toDateString();
$this->driver->takeScreenshot('FailedTestScreenshots/' . $today . '_' . time() . '_' . $this->getName() . '.jpg');
}
$this->driver->quit();
}
Most helpful comment
I'm doing this with hasFailed() in tearDown()