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

Start Chrome container.

docker run -d -p 4444:4444 selenium/standalone-chromium:130.0

Lint code

composer lint

Fix code style

composer lint-fix

Run tests

composer test-unit # Run unit tests.
composer test-bdd  # Run BDD tests.

Repository created using https://getscaffold.dev/ project scaffold template