/** * Take screenshot when step fails. Works only with Selenium2Driver. * Screenshot is saved at [Date]/[Feature]/[Scenario]/[Step].jpg * @AfterStep */ public function after(Behat\Behat\Hook\Scope\AfterStepScope $scope) { if ($scope->getTestResult()->getResultCode() === 99) { $driver = $this->getSession()->getDriver(); if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) { $fileName = date('d-m-y') . '-' . uniqid() . '.png'; $this->saveScreenshot($fileName, $this->screenshot_dir); print 'Screenshot at: ' . $this->screenshot_dir . '/' . $fileName; } } }
/** * @AfterStep * * Take screenshot when step fails. * Works only with Selenium2Driver. * * @param \Behat\Behat\Hook\Scope\AfterStepScope $scope */ public function takeScreenshotAfterFailedStep(Behat\Behat\Hook\Scope\AfterStepScope $scope) { if (!is_dir('temp')) { mkdir('temp'); } if (self::$clean === false) { $files = glob('temp/BEHAT*.png'); array_map('unlink', $files); self::$clean = true; } if ($scope->getTestResult()->getResultCode() === Behat\Testwork\Tester\Result\TestResult::FAILED) { $driver = $this->getSession()->getDriver(); if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) { $browser = $this->getMinkParameters()['browser_name']; $feature = iconv('UTF-8', 'ASCII//TRANSLIT', $scope->getFeature()->getTitle()); $line = $scope->getStep()->getLine(); $description = iconv('UTF-8', 'ASCII//TRANSLIT', $scope->getStep()->getText()); $description = preg_replace('/[^\\s\\w]/', '', $description); $image = sprintf('%s.%s.%s.%s.png', $browser, $feature, $line, $description); file_put_contents('temp/BEHAT.' . $image, $driver->getScreenshot()); } } }
/** * @AfterStep */ public function takeScreenshotAfterFailedStep(Behat\Behat\Hook\Scope\AfterStepScope $scope) { if (99 === $scope->getTestResult()->getResultCode()) { $this->takeScreenshot($scope->getStep()->getText(), ''); } }