Skip to content

Instantly share code, notes, and snippets.

@tikolakin
Created May 5, 2016 08:08
Show Gist options
  • Save tikolakin/70ad6ff1477c8393a45b9a4e770590be to your computer and use it in GitHub Desktop.
Save tikolakin/70ad6ff1477c8393a45b9a4e770590be to your computer and use it in GitHub Desktop.
Take screenshot of a page where step fails.
<?php
/**
* Take screenshot of a page where step fails.
*
* @AfterStep
*/
public function afterFailedStepsTakeScreenshot(AfterStepScope $scope) {
if (\Behat\Testwork\Tester\Result\TestResult::FAILED === $scope->getTestResult()->getResultCode()) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) {
$feature_name = strtolower(
str_replace(
array(' ', '"', '<', '>', '\''),
array('_', '', '', '_'),
$scope->getFeature()->getTitle())
);
$step_name = strtolower(
str_replace(
array(' ', '"', '<', '>', '\'', '/'),
array('_', '', '', '_', '-'),
$scope->getStep()->getText())
);
system('mkdir -p ' . escapeshellarg($this->parameters['screens']));
$file_name = $feature_name . '_' . $step_name . time() . '.png';
file_put_contents($this->parameters['screens'] . $file_name, $this->getSession()->getScreenshot());
$message = '';
if (getenv('BUILD_URL')) {
$message .= getenv('BUILD_URL');
}
echo $message, 'artifact/behat_tests/build/html/assets/screenshots/', $file_name, PHP_EOL;
}
echo 'Failure at the ', $this->getSession()->getCurrentUrl(), ' .', PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment