Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
92e6b01
php 8 support
May 27, 2021
494aa07
php 8 support
May 28, 2021
507616d
V10AGILE-6561: Fix dependency for onAfterSuiteTested hook [run ci]
Nov 25, 2021
8f35802
V10AGILE-6561: Reformat code [run ci]
Nov 25, 2021
2d7839d
Put screenshot into raport as base64
daniel-meyer Dec 5, 2022
de8436c
Put screenshot into raport as base64
daniel-meyer Dec 5, 2022
489f3c3
Put screenshot into raport as base64
daniel-meyer Dec 5, 2022
daa34f2
Update index.html.twig
daniel-meyer Dec 7, 2022
990b0dc
Update ScreenshotContext.php
daniel-meyer Dec 7, 2022
92c0b38
Update Scenario.php
daniel-meyer Dec 7, 2022
67a938e
Update Scenario.php
daniel-meyer Dec 7, 2022
42eb900
Update ScreenshotContext.php
daniel-meyer Dec 7, 2022
41a8262
Update BehatHTMLFormatter.php
daniel-meyer Dec 7, 2022
764b36d
Update BehatHTMLFormatter.php
daniel-meyer Dec 8, 2022
1efe7f4
Update ScreenshotContext.php
daniel-meyer Dec 8, 2022
52acb03
Update BehatHTMLFormatter.php
daniel-meyer Dec 8, 2022
c4187c8
Update ScreenshotContext.php
daniel-meyer Dec 8, 2022
283707a
Update BehatHTMLFormatter.php
daniel-meyer Dec 9, 2022
bf96345
Update ScreenshotContext.php
daniel-meyer Dec 9, 2022
cb17b89
Update ScreenshotContext.php
daniel-meyer Dec 9, 2022
32a0af8
Update BehatHTMLFormatter.php
daniel-meyer Dec 9, 2022
da230f6
Update ScreenshotContext.php
daniel-meyer Dec 9, 2022
1058322
Update ScreenshotContext.php
daniel-meyer Dec 10, 2022
c938c18
Update Scenario.php
daniel-meyer Dec 10, 2022
0351e9d
Update BehatHTMLFormatter.php
daniel-meyer Dec 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ Behat 3 extension for generating HTML reports from your test results.

![Twig Screenshot](http://i.imgur.com/o0zCqiB.png)

### Behat 2 report

![Behat2 Screenshot](http://i57.tinypic.com/287g942.jpg)


## How?

* The tool can be installed easily with composer.
Expand Down
18 changes: 14 additions & 4 deletions src/Classes/Scenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,24 @@ public function getScreenshotPath()
/**
* Gets relative path for screenshot.
*
* @return bool|string
* @return string
*/
public function getRelativeScreenshotPath()
{
if (!file_exists($this->screenshotPath)) {
return false;
if (!$this->hasScreenshot()) {
return '';
}

return '.'.substr($this->screenshotPath, strpos($this->screenshotPath, '/assets/screenshots'));
return $this->hasScreenshotData() ? $this->screenshotPath : 'data:image/png;base64,' . base64_encode(file_get_contents($this->screenshotPath));
}

public function hasScreenshot()
{
return $this->hasScreenshotData() || file_exists($this->screenshotPath);
}

public function hasScreenshotData()
{
return substr($this->screenshotPath, 0, 10) === 'data:image';
}
}
34 changes: 16 additions & 18 deletions src/Context/ScreenshotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@

class ScreenshotContext extends RawMinkContext
{
private $currentScenario;
private $screenshotDir;

private static $screenshotPath;

public function __construct($screenshotDir)
{
$this->screenshotDir = $screenshotDir;
// $this->screenshotDir = $screenshotDir;
}

/**
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*/
public function setUpTestEnvironment($scope)
public static function setScreenshotPath($screenshotPath)
{
$this->currentScenario = $scope->getScenario();
self::$screenshotPath = $screenshotPath;
}

/**
Expand All @@ -35,18 +29,22 @@ public function afterStep($scope)
if ($scope->getTestResult()->isPassed()) {
return;
}
if (!self::$screenshotPath) {
echo 'Screenshot path is empty!';
return;
}

// create filename string
$featureFolder = preg_replace('/\W/', '', $scope->getFeature()->getTitle());

$scenarioName = $this->currentScenario->getTitle();
$fileName = preg_replace('/\W/', '', $scenarioName).'.png';
$fileName = basename(self::$screenshotPath);
$screenshotDir = str_replace('/' . $fileName, '', self::$screenshotPath);

// create screenshots directory if it doesn't exist
if (!file_exists($this->screenshotDir.'/'.$featureFolder)) {
mkdir($this->screenshotDir.'/'.$featureFolder);
if (!file_exists($screenshotDir)) {
mkdir($screenshotDir);
}

$this->saveScreenshot($fileName, $this->screenshotDir.'/'.$featureFolder.'/');
$this->saveScreenshot($fileName, $screenshotDir);
$file = $screenshotDir . '/' . $fileName;
echo file_exists($file) ? "Saved screenshot: $file" : "Cannot save screenshot: $file";
}
}
17 changes: 12 additions & 5 deletions src/Formatter/BehatHTMLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
use Behat\Testwork\Counter\Memory;
use Behat\Testwork\Counter\Timer;
use Behat\Testwork\EventDispatcher\Event\AfterExerciseCompleted;
use Behat\Testwork\EventDispatcher\Event\AfterSuiteTested;
use Behat\Testwork\EventDispatcher\Event\BeforeExerciseCompleted;
use Behat\Testwork\EventDispatcher\Event\BeforeSuiteTested;
use Behat\Testwork\EventDispatcher\Event\SuiteTested;
use Behat\Testwork\Output\Formatter;
use Behat\Testwork\Output\Printer\OutputPrinter;
use emuse\BehatHTMLFormatter\Classes\Feature;
use emuse\BehatHTMLFormatter\Classes\Scenario;
use emuse\BehatHTMLFormatter\Classes\Step;
use emuse\BehatHTMLFormatter\Classes\Suite;
use emuse\BehatHTMLFormatter\Context\ScreenshotContext;
use emuse\BehatHTMLFormatter\Printer\FileOutputPrinter;
use emuse\BehatHTMLFormatter\Renderer\BaseRenderer;

Expand Down Expand Up @@ -431,9 +432,9 @@ public function onBeforeSuiteTested(BeforeSuiteTested $event)
}

/**
* @param AfterSuiteTested $event
* @param SuiteTested $event
*/
public function onAfterSuiteTested(AfterSuiteTested $event)
public function onAfterSuiteTested(SuiteTested $event)
{
$this->suites[] = $this->currentSuite;

Expand Down Expand Up @@ -487,11 +488,11 @@ public function onBeforeScenarioTested(BeforeScenarioTested $event)
$scenario->setLine($event->getScenario()->getLine());
$scenario->setScreenshotName($event->getScenario()->getTitle());
$scenario->setScreenshotPath(
$this->printer->getOutputPath().
'/assets/screenshots/'.
'/tmp/'.
preg_replace('/\W/', '', $event->getFeature()->getTitle()).'/'.
preg_replace('/\W/', '', $event->getScenario()->getTitle()).'.png'
);
ScreenshotContext::setScreenshotPath($scenario->getScreenshotPath());
$this->currentScenario = $scenario;

$print = $this->renderer->renderBeforeScenario($this);
Expand Down Expand Up @@ -536,6 +537,12 @@ public function onBeforeOutlineTested(BeforeOutlineTested $event)
$scenario->setName($event->getOutline()->getTitle());
$scenario->setTags($event->getOutline()->getTags());
$scenario->setLine($event->getOutline()->getLine());
$scenario->setScreenshotName($event->getOutline()->getTitle());
$scenario->setScreenshotPath(
'/tmp/'.
preg_replace('/\W/', '', $event->getFeature()->getTitle()).'/'.
preg_replace('/\W/', '', $event->getOutline()->getTitle()).'.png'
);
$this->currentScenario = $scenario;

$print = $this->renderer->renderBeforeOutline($this);
Expand Down
56 changes: 31 additions & 25 deletions src/Printer/FileOutputPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($rendererList, $filename, $base_path)
foreach ($rendererList as $renderer) {
if ('generated' == $filename) {
$date = date('YmdHis');
$this->rendererFiles[$renderer] = $renderer.'_'.$date;
$this->rendererFiles[$renderer] = $renderer . '_' . $date;
} else {
$this->rendererFiles[$renderer] = $filename;
}
Expand All @@ -60,22 +60,22 @@ public function setOutputPath($path)
if (!file_exists($outpath)) {
if (!mkdir($outpath, 0755, true)) {
throw new BadOutputPathException(
sprintf(
'Output path %s does not exist and could not be created!',
$outpath
),
$outpath
);
sprintf(
'Output path %s does not exist and could not be created!',
$outpath
),
$outpath
);
}
} else {
if (!is_dir(realpath($outpath))) {
throw new BadOutputPathException(
sprintf(
'The argument to `output` is expected to the a directory, but got %s!',
$outpath
),
$outpath
);
sprintf(
'The argument to `output` is expected to the a directory, but got %s!',
$outpath
),
$outpath
);
}
}
$this->outputPath = $outpath;
Expand Down Expand Up @@ -155,7 +155,7 @@ public function write($messages = array())
{
//Write it for each message = each renderer
foreach ($messages as $key => $message) {
$file = $this->outputPath.DIRECTORY_SEPARATOR.$this->rendererFiles[$key].'.html';
$file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html';
file_put_contents($file, $message);
$this->copyAssets($key);
}
Expand All @@ -170,7 +170,7 @@ public function writeln($messages = array())
{
//Write it for each message = each renderer
foreach ($messages as $key => $message) {
$file = $this->outputPath.DIRECTORY_SEPARATOR.$this->rendererFiles[$key].'.html';
$file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html';
file_put_contents($file, $message, FILE_APPEND);
}
}
Expand All @@ -184,9 +184,9 @@ public function writeBeginning($messages = array())
{
//Write it for each message = each renderer
foreach ($messages as $key => $message) {
$file = $this->outputPath.DIRECTORY_SEPARATOR.$this->rendererFiles[$key].'.html';
$file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html';
$fileContents = file_get_contents($file);
file_put_contents($file, $message.$fileContents);
file_put_contents($file, $message . $fileContents);
}
}

Expand All @@ -199,17 +199,20 @@ public function copyAssets($renderer)
{
// If the assets folder doesn' exist in the output path for this renderer, copy it
$source = realpath(dirname(__FILE__));
$assets_source = realpath($source.'/../../assets/'.$renderer);
$assets_source = realpath($source . '/../../assets/' . $renderer);
if (false === $assets_source) {
//There is no assets to copy for this renderer
return;
}

//first create the assets dir
$destination = $this->outputPath.DIRECTORY_SEPARATOR.'assets';
@mkdir($destination);
$destination = $this->outputPath . DIRECTORY_SEPARATOR . 'assets';

$this->recurse_copy($assets_source, $destination.DIRECTORY_SEPARATOR.$renderer);
if (!file_exists($destination)) {
mkdir($destination);
}

$this->recurse_copy($assets_source, $destination . DIRECTORY_SEPARATOR . $renderer);
}

/**
Expand All @@ -221,13 +224,16 @@ public function copyAssets($renderer)
private function recurse_copy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
if (!file_exists($dst)) {
mkdir($dst);
}

while (false !== ($file = readdir($dir))) {
if (('.' != $file) && ('..' != $file)) {
if (is_dir($src.'/'.$file)) {
$this->recurse_copy($src.'/'.$file, $dst.'/'.$file);
if (is_dir($src . '/' . $file)) {
$this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src.'/'.$file, $dst.'/'.$file);
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Renderer/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace emuse\BehatHTMLFormatter\Renderer;

use emuse\BehatHTMLFormatter\Formatter\BehatHTMLFormatter;
use Twig_Environment;
use Twig_Loader_Filesystem;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

/**
* Twig renderer for Behat report.
Expand Down Expand Up @@ -35,8 +35,8 @@ public function renderBeforeExercise(BehatHTMLFormatter $obj)
public function renderAfterExercise(BehatHTMLFormatter $obj)
{
$templatePath = dirname(__FILE__).'/../../templates';
$loader = new Twig_Loader_Filesystem($templatePath);
$twig = new Twig_Environment($loader, array());
$loader = new FilesystemLoader($templatePath);
$twig = new Environment($loader, array());
$print = $twig->render('index.html.twig',
array(
'suites' => $obj->getSuites(),
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
{% for tag in scenario.tags %}
<span class="badge">{{ tag }}</span>
{% endfor %}
{% if scenario.getRelativeScreenshotPath() %}
{% if scenario.hasScreenshot() %}
<a class="label label-default" href="{{ scenario.getRelativeScreenshotPath() }}" target="_blank">Screenshot</a>
{% endif %}
</div>
Expand Down