diff --git a/README.md b/README.md index 46e5303..b83ad67 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,11 @@ config file. This `base_url` is used when concatenating URLs by `SilverStripeContext->joinUrlParts()` method. `admin_url` and `login_url` should not be changed in general (unless they are not correct) +Optional `screenshot_path` variable is used to store screenshot of a last know state +of a failed step. It defaults to whatever is returned by PHP's `sys_get_temp_dir()`. +Screenshot names within that directory consist of feature file filename and line +number that failed. + # behat.yml default: # ... @@ -46,6 +51,7 @@ be changed in general (unless they are not correct) base_url: http://localhost admin_url: /admin/ login_url: /Security/login + screenshot_path: features/screenshots/ ### Configuring extensions diff --git a/behat.yml b/behat.yml index dc9db6d..d30c03e 100644 --- a/behat.yml +++ b/behat.yml @@ -5,6 +5,7 @@ default: base_url: http://localhost admin_url: /admin/ login_url: /Security/login + screenshot_path: features/screenshots/ extensions: features/extensions/SilverStripeExtension/init.php: # Behat\SilverStripeExtension\Extension: diff --git a/features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php b/features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php index c9fb756..b240a4c 100644 --- a/features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php +++ b/features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php @@ -156,7 +156,22 @@ public function takeScreenshotAfterFailedStep(StepEvent $event) $feature = $parent->getFeature(); $step = $event->getStep(); - $screenshot_path = realpath(sprintf('%s/%s_%d.png', sys_get_temp_dir(), basename($feature->getFile()), $step->getLine())); + if (isset($this->context['screenshot_path'])) { + $screenshot_path = realpath($this->context['screenshot_path']); + } else { + $screenshot_path = realpath(sys_get_temp_dir()); + } + + if (!is_dir($screenshot_path)) { + file_put_contents('php://stderr', sprintf('"%s" is not valid directory' . PHP_EOL, $this->context['screenshot_path'])); + return; + } + if (!is_writable($screenshot_path)) { + file_put_contents('php://stderr', sprintf('"%s" directory is not writable' . PHP_EOL, $screenshot_path)); + return; + } + + $screenshot_path = sprintf('%s/%s_%d.png', $screenshot_path, basename($feature->getFile()), $step->getLine()); $screenshot = $driver->wdSession->screenshot(); file_put_contents($screenshot_path, base64_decode($screenshot)); file_put_contents('php://stderr', sprintf('Saving screenshot into %s' . PHP_EOL, $screenshot_path));