Skip to content

Commit

Permalink
Converted Behat screenshot to extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
bladeaweb authored and AlexSkrypnyk committed May 26, 2017
1 parent 73b95ef commit cd152dc
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 108 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ default:
suites:
default:
contexts:
- IntegratedExperts\BehatScreenshot\ScreenshotContext:
-
dir: %paths.base%/screenshots
fail: true
purge: false
- FeatureContext
- IntegratedExperts\BehatScreenshot\ScreenshotContext
- FeatureContext
extensions:
IntegratedExperts\BehatScreenshot\ScreenshotExtension:
dir: %paths.base%/screenshots
fail: true
purge: false
```
In your feature:
Expand Down
10 changes: 5 additions & 5 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ default:
- IntegratedExperts\BehatPhpServer\PhpServerContext:
-
docroot: %paths.base%/tests/behat/features/fixtures
- IntegratedExperts\BehatScreenshot\ScreenshotContext:
-
dir: %paths.base%/screenshots
fail: false
purge: true
- IntegratedExperts\Behat\Screenshot\Context\ScreenshotContext
- FeatureContext:
-
screenshot_dir: %paths.base%/screenshots
Expand All @@ -21,3 +17,7 @@ default:
files_path: %paths.base%/tests/behat/features/fixtures
selenium2: ~
browser_name: chrome
IntegratedExperts\Behat\Screenshot\ScreenshotExtension:
dir: %paths.base%/screenshots
fail: false
purge: true
1 change: 0 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ test:
override:
- composer cs
- composer test
- BEHAT_SCREENSHOT_DIR=$CIRCLE_ARTIFACTS/behat composer test
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"autoload": {
"psr-0": {
"IntegratedExperts\\BehatScreenshot": "src/"
"IntegratedExperts\\Behat\\Screenshot": "src/"
}
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* @file
* This file is part of the IntegratedExperts\BehatScreenshot package.
*/

namespace IntegratedExperts\Behat\Screenshot\Context\Initializer;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Initializer\ContextInitializer;
use IntegratedExperts\Behat\Screenshot\Context\ScreenshotContextInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

/**
* Class ScreenshotContextInitializer
*/
class ScreenshotContextInitializer implements ContextInitializer
{

/**
* Screenshot directory name.
*
* @var string
*/
private $dir;

/**
* Makes screenshot when fail.
*
* @var bool
*/
private $fail;

/**
* Purge dir before start test.
*
* @var bool
*/
private $purge;

/**
* Does need to clear directory trigger.
*
* @var bool
*/
private $toPurge;

/**
* ScreenshotContextInitializer constructor.
*
* @param string $dir Screenshot dir.
* @param bool $fail Screenshot when fail.
* @param bool $purge Purge dir before start script.
*/
public function __construct($dir, $fail, $purge)
{
$this->toPurge = true;
$this->dir = $dir;
$this->fail = $fail;
$this->purge = $purge;
}

/**
* {@inheritdoc}
*/
public function initializeContext(Context $context)
{
if ($context instanceof ScreenshotContextInterface) {
$context->setParameters($this->dir, $this->fail);
// Calling clearing screenshot directory function.
if ($this->purge && $this->toPurge) {
$this->purgeFilesInDir();
$this->toPurge = false;
}
}
}

/**
* Remove files in directory.
*/
protected function purgeFilesInDir()
{
$fs = new Filesystem();
$finder = new Finder();
if ($fs->exists($this->dir)) {
$fs->remove($finder->files()->in($this->dir));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
* Behat context to enable Screenshot support in tests.
*/

namespace IntegratedExperts\BehatScreenshot;
namespace IntegratedExperts\Behat\Screenshot\Context;

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Exception\RuntimeException;

/**
* Class ScreenshotContext.
*/
class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContext
class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContext, ScreenshotContextInterface
{

/**
Expand All @@ -39,51 +36,28 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex
protected $stepLine;

/**
* Directory where screenshots are stored.
* Screenshot directory name.
*
* @var string
*/
protected $dir;
private $dir;

/**
* Flag to create a screenshot when test fails.
* Makes screenshot when fail.
*
* @var bool
*/
protected $onFail;
private $fail;

/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the context constructor through
* behat.yml.
*
* @param array $parameters Get parameters for construct test.
*/
public function __construct($parameters = [])
{
$this->dir = $this->extractParameterFromEnv('BEHAT_SCREENSHOT_DIR', $parameters['dir']);
$this->onFail = isset($parameters['fail']) ? $parameters['fail'] : true;
}

/**
* Init function before tests run.
*
* @param BeforeSuiteScope $scope
*
* @BeforeSuite
* {@inheritdoc}
*/
public static function beforeSuitInit(BeforeSuiteScope $scope)
public function setParameters($dir, $fail)
{
$contextSettings = self::getSettingsFromScope($scope);
$this->dir = $dir;
$this->fail = $fail;

$dir = self::extractParameterFromEnv('BEHAT_SCREENSHOT_DIR', $contextSettings['dir']);
$purge = self::extractParameterFromEnv('BEHAT_SCREENSHOT_PURGE', $contextSettings['purge'], false);

if ($purge) {
self::purgeFilesInDir($dir);
}
return $this;
}

/**
Expand Down Expand Up @@ -124,8 +98,8 @@ public function beforeStepInit(BeforeStepScope $scope)
*/
public function printLastResponseOnError(AfterStepScope $event)
{
if ($this->onFail && !$event->getTestResult()->isPassed()) {
$this->saveDebugScreenshot();
if ($this->fail && !$event->getTestResult()->isPassed()) {
$this->iSaveScreenshot();
}
}

Expand All @@ -137,7 +111,7 @@ public function printLastResponseOnError(AfterStepScope $event)
* @When save screenshot
* @When I save screenshot
*/
public function saveDebugScreenshot()
public function iSaveScreenshot()
{
$data = null;
$driver = $this->getSession()->getDriver();
Expand All @@ -155,28 +129,24 @@ public function saveDebugScreenshot()
}
}

protected function saveScreenshotData($filename, $data)
{
$this->prepareDir($this->dir);
file_put_contents($this->dir.DIRECTORY_SEPARATOR.$filename, $data);
}

/**
* Make screenshot filename.
*
* Format: microseconds.featurefilename_linenumber.ext
*
* @param string $ext File extension without dot.
* Save screenshot data into a file.
*
* @return string Unique file name.
* @param string $filename
* File name to write.
* @param string $data
* Data to write into a file.
*/
protected function makeFileName($ext)
protected function saveScreenshotData($filename, $data)
{
return sprintf('%01.2f.%s_[%s].%s', microtime(true), basename($this->featureFile), $this->stepLine, $ext);
$this->prepareDir($this->dir);
file_put_contents($this->dir.DIRECTORY_SEPARATOR.$filename, $data);
}

/**
* Prepare directory.
*
* @param string $dir Name of preparing directory.
*/
protected function prepareDir($dir)
{
Expand All @@ -185,49 +155,16 @@ protected function prepareDir($dir)
}

/**
* Remove files in directory.
* Make screenshot filename.
*
* @param string $dir Directory name.
*/
protected static function purgeFilesInDir($dir)
{
$fs = new Filesystem();
$finder = new Finder();
if ($fs->exists($dir)) {
$fs->remove($finder->files()->in($dir));
}
}

/**
* Extract parameter from the list of provided parameters.
*/
protected static function extractParameterFromEnv()
{
$candidates = func_get_args();
$candidates[0] = getenv($candidates[0]) === false ? null : getenv($candidates[0]);

foreach ($candidates as $candidate) {
if (isset($candidate)) {
return $candidate;
}
}

throw new RuntimeException('One of the parameters was not provided');
}

/**
* Extarct settings from scope.
* Format: microseconds.featurefilename_linenumber.ext
*
* @param string $ext File extension without dot.
*
* @return string Unique file name.
*/
protected static function getSettingsFromScope(BeforeSuiteScope $scope)
protected function makeFileName($ext)
{
$settings = null;
foreach ($scope->getSuite()->getSetting('contexts') as $context) {
if (is_array($context) && isset($context['IntegratedExperts\BehatScreenshot\ScreenshotContext'][0])) {
$settings = $context['IntegratedExperts\BehatScreenshot\ScreenshotContext'][0];
break;
}
}

return $settings;
return sprintf('%01.2f.%s_[%s].%s', microtime(true), basename($this->featureFile), $this->stepLine, $ext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @file
* Behat context interface to enable Screenshot.
*/

namespace IntegratedExperts\Behat\Screenshot\Context;

use Behat\Behat\Context\Context;

/**
* Interface ScreenshotContext.
*/
interface ScreenshotContextInterface extends Context
{

/**
* Set context parameters.
*
* @param string $dir
* @param bool $fail
*
* @return $this
*/
public function setParameters($dir, $fail);
}
Loading

0 comments on commit cd152dc

Please sign in to comment.