diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php index fa8d5ed..d0ac487 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php @@ -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(); @@ -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. * @@ -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); } } diff --git a/tests/behat/features/html.feature b/tests/behat/features/html.feature index 426315d..2d1b23e 100644 --- a/tests/behat/features/html.feature +++ b/tests/behat/features/html.feature @@ -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 diff --git a/tests/behat/features/selenium.feature b/tests/behat/features/selenium.feature index 979645d..27fb2ea 100644 --- a/tests/behat/features/selenium.feature +++ b/tests/behat/features/selenium.feature @@ -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