Skip to content

Commit

Permalink
[#15] Added failed test prefix support.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Aug 7, 2017
1 parent 13ee5e2 commit 55a1497
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ default:
browser_name: chrome
IntegratedExperts\BehatScreenshotExtension:
dir: %paths.base%/screenshots
fail: false
fail: true
purge: true
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ default:
dir: %paths.base%/screenshots
fail: true
purge: false
purge_prefix: "fail_"
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class ScreenshotContextInitializer implements ContextInitializer
*/
protected $fail;

/**
* Prefix for failed screenshot files.
*
* @var string
*/
private $failPrefix;

/**
* Purge dir before start test.
*
Expand All @@ -49,16 +56,18 @@ class ScreenshotContextInitializer implements ContextInitializer
/**
* ScreenshotContextInitializer constructor.
*
* @param string $dir Screenshot dir.
* @param bool $fail Screenshot when fail.
* @param bool $purge Purge dir before start script.
* @param string $dir Screenshot dir.
* @param bool $fail Screenshot when fail.
* @param bool $failPrefix File name prefix for a failed test.
* @param bool $purge Purge dir before start script.
*/
public function __construct($dir, $fail, $purge)
public function __construct($dir, $fail, $failPrefix, $purge)
{
$this->needsPurging = true;
$this->dir = $dir;
$this->fail = $fail;
$this->purge = $purge;
$this->fail = $fail;
$this->failPrefix = $failPrefix;
}

/**
Expand All @@ -68,7 +77,7 @@ public function initializeContext(Context $context)
{
if ($context instanceof ScreenshotAwareContext) {
$dir = $this->resolveDir();
$context->setScreenshotParameters($dir, $this->fail);
$context->setScreenshotParameters($dir, $this->fail, $this->failPrefix);
if ($this->purge && $this->needsPurging) {
$this->purgeFilesInDir();
$this->needsPurging = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ interface ScreenshotAwareContext extends Context
/**
* Set context parameters.
*
* @param string $dir
* @param bool $fail
* @param string $dir Directory to store screenshots.
* @param bool $fail Create screenshots on fail.
* @param bool $failPrefix File name prefix for a failed test.
*
* @return $this
*/
public function setScreenshotParameters($dir, $fail);
public function setScreenshotParameters($dir, $fail, $failPrefix);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex
*/
private $fail;

/**
* Prefix for failed screenshot files.
*
* @var string
*/
private $failPrefix;

/**
* {@inheritdoc}
*/
public function setScreenshotParameters($dir, $fail)
public function setScreenshotParameters($dir, $fail, $failPrefix)
{
$this->dir = $dir;
$this->fail = $fail;
$this->failPrefix = $failPrefix;

return $this;
}
Expand Down Expand Up @@ -99,7 +107,7 @@ public function beforeStepInit(BeforeStepScope $scope)
public function printLastResponseOnError(AfterStepScope $event)
{
if ($this->fail && !$event->getTestResult()->isPassed()) {
$this->iSaveScreenshot();
$this->iSaveScreenshot(true);
}
}

Expand All @@ -108,14 +116,18 @@ public function printLastResponseOnError(AfterStepScope $event)
*
* Handles different driver types.
*
* @param bool $fail Denotes if this was called in a context of the failed
* test.
*
* @When save screenshot
* @When I save screenshot
*/
public function iSaveScreenshot()
public function iSaveScreenshot($fail = false)
{
$data = null;
$driver = $this->getSession()->getDriver();

$ext = 'html';
if ($driver instanceof Selenium2Driver) {
$data = $this->getSession()->getScreenshot();
$ext = 'png';
Expand All @@ -125,7 +137,11 @@ public function iSaveScreenshot()
}

if ($data) {
$this->saveScreenshotData($this->makeFileName($ext), $data);
if ($fail) {
$this->saveScreenshotData($this->makeFileName($ext, $this->failPrefix), $data);
} else {
$this->saveScreenshotData($this->makeFileName($ext), $data);
}
}
}

Expand Down Expand Up @@ -159,12 +175,13 @@ protected function prepareDir($dir)
*
* Format: microseconds.featurefilename_linenumber.ext
*
* @param string $ext File extension without dot.
* @param string $ext File extension without dot.
* @param string $prefix Optional file name prefix for a filed test.
*
* @return string Unique file name.
*/
protected function makeFileName($ext)
protected function makeFileName($ext, $prefix = '')
{
return sprintf('%01.2f.%s_[%s].%s', microtime(true), basename($this->featureFile), $this->stepLine, $ext);
return sprintf('%01.2f.%s%s_%s.%s', microtime(true), $prefix, basename($this->featureFile), $this->stepLine, $ext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function configure(ArrayNodeDefinition $builder)
$builder->children()
->scalarNode('dir')->isRequired()->cannotBeEmpty()->end()
->scalarNode('fail')->isRequired()->cannotBeEmpty()->end()
->scalarNode('fail_prefix')->defaultValue('failed_')->end()
->scalarNode('purge')->isRequired()->cannotBeEmpty()->end();
}

Expand All @@ -66,6 +67,7 @@ public function load(ContainerBuilder $container, array $config)
$definition = new Definition('IntegratedExperts\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer', [
$config['dir'],
$config['fail'],
$config['fail_prefix'],
$config['purge'],
]);
$definition->addTag(ContextExtension::INITIALIZER_TAG, ['priority' => 0]);
Expand Down
2 changes: 1 addition & 1 deletion tests/behat/features/html.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Feature: HTML screenshots
When I am on the screenshot test page
And the response status code should be 200
And I save screenshot
Then file wildcard "*.html.feature_\[9\]\.html" should exist
Then file wildcard "*.html.feature_9\.html" should exist
2 changes: 1 addition & 1 deletion tests/behat/features/selenium.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Feature: Selenium screenshots
Scenario: Capture a screenshot using Selenium driver
When I am on the screenshot test page
And save screenshot
Then file wildcard "*.selenium.feature_[\8\]\.png" should exist
Then file wildcard "*.selenium.feature_8.png" should exist

0 comments on commit 55a1497

Please sign in to comment.