drevops/behat-screenshot

Behat extension and step definitions to create HTML and image screenshots on demand or when tests fail

Fund package maintenance!
drevops
Patreon


README

Behat screenshot logo

Behat Screenshot Extension

GitHub Issues GitHub Pull Requests Test codecov GitHub release (latest by date) LICENSE Renovate

Total Downloads

Behat extension and step definitions to create HTML and image screenshots on demand or when tests fail.

Features

  • Create a screenshot using I save screenshot or save screenshot step definition.
  • Create a screenshot when test fails.
  • Screenshot is saved as HTML page for Goutte driver.
  • Screenshot is saved as both HTML and PNG image for Selenium driver.
  • Screenshot directory can be specified through environment variable BEHAT_SCREENSHOT_DIR (useful for CI systems to override values in behat.yml).
  • Screenshots can be purged after every test run by setting purge: true ( useful during test debugging) or setting environment variable BEHAT_SCREENSHOT_PURGE=1.

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'
      fail: true
      fail_prefix: 'failed_'
      purge: false
      filenamePattern: '{datetime:u}.{feature_file}.feature_{step_line}.{ext}'
      filenamePatternFailed: '{datetime:u}.{fail_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 size of browser window in the screenshot step:

  Then I save 1440 x 900 screenshot
And I save 800 x 600 screenshot

Options

Supported tokens

Maintenance

Local development setup

Install dependencies.

composer install

Lint code

composer lint

Fix code style

composer lint-fix

Run tests

Unit tests

composer test-unit # Run unit tests.

BDD tests

We have 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