From 13189e609bba4938a8382ae6d1df7558f4176516 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Wed, 21 Feb 2024 15:01:28 +0700 Subject: [PATCH 1/3] Add save screenshot with name definition step. --- .../Context/ScreenshotContext.php | 33 +++++++++++++++---- tests/behat/features/html.feature | 8 +++++ tests/behat/features/selenium.feature | 7 ++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php index fa8d5ed..712de02 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php @@ -11,6 +11,7 @@ use Behat\Behat\Hook\Scope\AfterStepScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Behat\Hook\Scope\BeforeStepScope; +use Behat\Behat\Tester\Exception\PendingException; use Behat\Mink\Driver\Selenium2Driver; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\UnsupportedDriverActionException; @@ -127,17 +128,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 +166,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 +227,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..5a43245 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 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 From 0ff07015f8ad267e5f3ad0d7bb5671219cde1ade Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Wed, 21 Feb 2024 15:12:07 +0700 Subject: [PATCH 2/3] Update test. --- tests/behat/features/selenium.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/behat/features/selenium.feature b/tests/behat/features/selenium.feature index 5a43245..27fb2ea 100644 --- a/tests/behat/features/selenium.feature +++ b/tests/behat/features/selenium.feature @@ -14,7 +14,7 @@ Feature: Selenium screenshots And file wildcard "*.selenium.feature_13.html" should exist @phpserver @javascript - Scenario: Capture a screenshot using Selenium driver + 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 From b72f0eade230a670b1b823a3efdf6438c093a90b Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Wed, 21 Feb 2024 16:19:48 +0700 Subject: [PATCH 3/3] Remove unuse namespace --- .../BehatScreenshotExtension/Context/ScreenshotContext.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php index 712de02..d0ac487 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php @@ -11,7 +11,6 @@ use Behat\Behat\Hook\Scope\AfterStepScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Behat\Hook\Scope\BeforeStepScope; -use Behat\Behat\Tester\Exception\PendingException; use Behat\Mink\Driver\Selenium2Driver; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\UnsupportedDriverActionException;