integratedexperts / behat-screenshot
Behat extension and step definitions to create HTML and image screenshots on demand or when tests fail
Installs: 447 420
Dependents: 7
Suggesters: 0
Security: 0
Stars: 22
Watchers: 5
Forks: 8
Open Issues: 3
Requires
- php: >=8.2
- behat/behat: ^3.13.0
- friends-of-behat/mink-extension: ^2.7
- symfony/finder: ^6.4 || ^7.0
- symfony/http-client: ^6.0 || ^7.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- behat/mink-browserkit-driver: ^2.2
- dantleech/gherkin-lint: ^0.2.3
- dealerdirect/phpcodesniffer-composer-installer: ^1
- dmore/behat-chrome-extension: ^1.4
- drevops/behat-phpserver: ^2.0
- drupal/coder: ^8.3
- dvdoug/behat-code-coverage: ^5.3
- ergebnis/composer-normalize: ^2.44
- escapestudios/symfony2-coding-standard: ^3
- lullabot/mink-selenium2-driver: ^1.7
- lullabot/php-webdriver: ^2.0.4
- mikey179/vfsstream: ^1.6
- opis/closure: ^4.0
- phpstan/phpstan: ^2
- phpunit/phpunit: ^11
- rector/rector: ^2
- symfony/process: ^6.4 || ^7.0
- dev-main
- 2.0.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.8.0
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-feature/update-params
- dev-feature/67-improve-language
- dev-feature/132-better-name-cleanup
- dev-feature/43-compat-non-selenium
- dev-feature/extend-info
- dev-feature/improve-code
- dev-renovate/drevops-behat-phpserver-2.x
This package is auto-updated.
Last update: 2025-01-19 02:04:24 UTC
README
Behat extension to create screenshots
Features
- Captures a screenshot using the
I save screenshot
step. - Automatically captures a screenshot when a test fails.
- Supports both HTML and PNG screenshots.
- Configurable screenshot directory.
- Automatically purges screenshots after each test run.
- Adds configurable additional information to screenshots.
Installation
composer require --dev drevops/behat-screenshot
Usage
Example behat.yml
with default parameters:
default: suites: default: contexts: - DrevOps\BehatScreenshotExtension\Context\ScreenshotContext - FeatureContext extensions: DrevOps\BehatScreenshotExtension: ~
or with parameters:
default: suites: default: contexts: - DrevOps\BehatScreenshotExtension\Context\ScreenshotContext - FeatureContext extensions: DrevOps\BehatScreenshotExtension: dir: '%paths.base%/screenshots' on_failed: true purge: false failed_prefix: 'failed_' filename_pattern: '{datetime:u}.{feature_file}.feature_{step_line}.{ext}' filename_pattern_failed: '{datetime:u}.{failed_prefix}{feature_file}.feature_{step_line}.{ext}'
In your feature:
Given I am on "http://google.com" Then I save screenshot
You may optionally specify the size of the browser window in the screenshot step:
Then I save 1440 x 900 screenshot
or a file name:
Then I save screenshot to "my_screenshot.png"
Options
File name tokens
Auto-purge
By default, the purge
option is disabled. This means that the screenshot
directory will not be cleared after each test run. This is useful when you want
to keep the screenshots for debugging purposes.
If you want to clear the directory after each test run, you can enable the
purge
option in the configuration.
default: extensions: DrevOps\BehatScreenshotExtension: purge: true
Alternatively, you can use BEHAT_SCREENSHOT_PURGE
environment variable to
enable the auto-purge feature for a specific test run.
BEHAT_SCREENSHOT_PURGE=1 vendor/bin/behat
Additional information on screenshots
You can enable additional information on screenshots by setting info_types
in
the configuration. The order of the types will be the order of the information
displayed on the screenshot.
By default, the information displayed is the URL, feature file name, step line:
Current URL: http://example.com<br/> Feature: My feature<br/> Step: I save screenshot (line 8)<br/> Datetime: 2025-01-19 00:01:10 <hr/> <!DOCTYPE html> <html> ... </html>
More information can be added by setting the info_types
configuration option
and using addInfo()
method in your context class.
/** * @BeforeScenario */ public function beforeScenarioUpdateBaseUrl(BeforeScenarioScope $scope): void { $environment = $scope->getEnvironment(); if ($environment instanceof InitializedContextEnvironment) { foreach ($environment->getContexts() as $context) { if ($context instanceof ScreenshotContext) { $context->addInfo('Custom info', 'My custom info'); } } } }
Maintenance
composer install composer lint composer lint-fix composer test-unit composer test-bdd
BDD tests
There are tests for Selenium and Headless drivers. Selenium requires a Docker container and headless requires a Chromium browser (we will make this more streamlined in the future).
# Start Chromium in container for Selenium-based tests.
docker run -d -p 4444:4444 -p 9222:9222 selenium/standalone-chromium
# Install Chromium with brew. brew cask install chromedriver # Launch Chromium with remote debugging. /opt/homebrew/Caskroom/chromium/latest/chromium.wrapper.sh --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222
composer test-bdd # Run BDD tests. BEHAT_CLI_DEBUG=1 composer test-bdd # Run BDD tests with debug output.
Repository created using https://getscaffold.dev/ project scaffold template