Skip to content

Commit

Permalink
[#28] Allow to save the screenshot name. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 authored Feb 21, 2024
1 parent 48b5cc2 commit 0abe416
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,18 @@ public function printLastResponseOnError(AfterStepScope $event): void
*
* Handles different driver types.
*
* @param bool $fail Denotes if this was called in a context of the failed
* test.
* @param bool $fail Denotes if this was called in a context of the failed
* test.
* @param string|null $filename File name.
*
* @When save screenshot
* @When I save screenshot
*/
public function iSaveScreenshot($fail = false): void
public function iSaveScreenshot($fail = false, $filename = null): void
{
$driver = $this->getSession()->getDriver();

$fileName = $this->makeFileName('html', $fail ? $this->failPrefix : '');
$fileName = $this->makeFileName('html', $fail ? $this->failPrefix : '', $filename);

try {
$data = $driver->getContent();
Expand All @@ -164,6 +165,18 @@ public function iSaveScreenshot($fail = false): void
}
}

/**
* Save screenshot with name.
*
* @param string $filename File name.
*
* @When I save screenshot with name :filename
*/
public function iSaveScreenshotWithName(string $filename): void
{
$this->iSaveScreenshot(false, $filename);
}

/**
* Save screenshot with specific dimensions.
*
Expand Down Expand Up @@ -213,13 +226,18 @@ protected function prepareDir(string $dir): void
*
* Format: microseconds.featurefilename_linenumber.ext
*
* @param string $ext File extension without dot.
* @param string $prefix Optional file name prefix for a filed test.
* @param string $ext File extension without dot.
* @param string $prefix Optional file name prefix for a filed test.
* @param string|null $filename Optional file name.
*
* @return string Unique file name.
*/
protected function makeFileName(string $ext, string $prefix = ''): string
protected function makeFileName(string $ext, string $prefix = '', string $filename = null): string
{
if (!empty($filename)) {
return sprintf('%s.%s', $filename, $ext);
}

return sprintf('%01.2f.%s%s_%s.%s', microtime(true), $prefix, basename($this->featureFile), $this->stepLine, $ext);
}
}
8 changes: 8 additions & 0 deletions tests/behat/features/html.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ Feature: HTML screenshots
And I save screenshot
Then file wildcard "*.html.feature_9\.html" should exist
And file wildcard "*.html.feature_9\.png" should not exist

@phpserver
Scenario: Capture a screenshot with name using HTML-based driver
When I am on the screenshot test page
And the response status code should be 200
And I save screenshot with name "hello-screenshot"
Then file wildcard "hello-screenshot\.html" should exist
And file wildcard "hello-screenshot\.png" should not exist
7 changes: 7 additions & 0 deletions tests/behat/features/selenium.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ Feature: Selenium screenshots
Then file wildcard "*.selenium.feature_11.png" should exist
And save 1440 x 900 screenshot
And file wildcard "*.selenium.feature_13.html" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver
When I am on the screenshot test page
And I save screenshot with name "hello-selenium-screenshot"
Then file wildcard "hello-selenium-screenshot.png" should exist
And file wildcard "hello-selenium-screenshot.html" should exist

0 comments on commit 0abe416

Please sign in to comment.