承接 drevops/behat-screenshot 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drevops/behat-screenshot

Composer 安装命令:

composer require drevops/behat-screenshot

包简介

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

README 文档

README

Behat screenshot logo

Behat extension to create screenshots

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

Total Downloads

Vortex Ecosystem

Features

  • Captures a screenshot using the I save screenshot step.
  • Captures fullscreen screenshots with the I save fullscreen screenshot step.
  • Automatically captures a screenshot when a test fails.
  • Supports both HTML and PNG screenshots.
  • Supports Selenium and Headless drivers.
  • Configurable screenshot directory.
  • Automatically purges screenshots after each test run.
  • Adds additional information to screenshots.
  • Records an animated GIF of a scenario from its per-step 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
      always_fullscreen: 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 can capture fullscreen screenshots:

Given I am on "http://google.com"
Then I save fullscreen screenshot

Fullscreen screenshots work by temporarily resizing the browser window to the full height of the page to capture everything in one screenshot.

You may optionally specify the size of the browser window in the screenshot step:

Then I save 1440 x 900 screenshot
# Or with fullscreen
Then I save fullscreen 1440 x 900 screenshot

or a file name:

Then I save screenshot with name "my_screenshot.png"
# Or with fullscreen
Then I save fullscreen screenshot with name "my_screenshot.png"

To always capture fullscreen screenshots, even without explicitly using the fullscreen keyword, set the always_fullscreen configuration option to true:

default:
  extensions:
    DrevOps\BehatScreenshotExtension:
      always_fullscreen: true

Capturing Screenshots After Every Step

To automatically capture a screenshot after every step, you can either:

  1. Enable globally in configuration:
default:
  extensions:
    DrevOps\BehatScreenshotExtension:
      on_every_step: true
  1. Enable per-scenario using the @screenshots tag:
@screenshots
Scenario: My scenario with automatic screenshots
  Given I am on "http://example.com"
  When I click "Login"
  Then I should see "Welcome"
  # Screenshots will be captured after each of these steps

The @screenshots tag takes precedence over the global configuration, allowing you to enable this feature for specific scenarios even when it's disabled globally.

Note: When both on_every_step and on_failed are enabled, only one screenshot is captured for failed steps (the failed screenshot) to avoid duplicates.

Recording an animated GIF

To record an animated GIF of a scenario from its per-step screenshots, you can either:

  1. Enable globally in configuration:
default:
  extensions:
    DrevOps\BehatScreenshotExtension:
      animation:
        enabled: true
        frame_delay: 500
  1. Enable per-scenario using the @screenshots:animated tag:
@javascript @screenshots:animated
Scenario: My scenario recorded as an animated GIF
  Given I am on "http://example.com"
  When I click "Login"
  Then I should see "Welcome"
  # An animated GIF is written when the scenario finishes.

When animation is enabled, a screenshot is taken after every passed step, and the frames are combined into a single GIF - named after the feature file and scenario line - when the scenario finishes. frame_delay sets the delay between frames in milliseconds.

The @screenshots:animated tag is read at both the scenario and feature level. Animation requires the gd PHP extension and a driver that can capture screenshots (such as a real browser via @javascript); without GD, the animated GIF is skipped while the per-step screenshots are still written.

Options

Name Default value Description
dir %paths.base%/screenshots Path to directory to save screenshots. Directory structure will be created if the directory does not exist. Override with BEHAT_SCREENSHOT_DIR env var.
on_failed true Capture screenshot on failed test.
on_every_step false Automatically capture screenshots after every step. Can be enabled globally via config or per-scenario using the @screenshots tag. Only captures on passed steps to avoid duplicates with on_failed.
animation.enabled false Build an animated GIF per scenario from the per-step screenshots, automatically enabling per-step capture. Can be enabled per-scenario with the @screenshots:animated tag (read at scenario or feature level). Requires the gd PHP extension.
animation.frame_delay 500 Delay between animated GIF frames, in milliseconds.
purge false Remove all files from the screenshots directory on each test run. Useful during debugging of tests.
always_fullscreen false Always use fullscreen screenshot capture for all screenshot steps, including regular screenshot steps. When enabled, all I save screenshot steps will behave like I save fullscreen screenshot.
info_types url, feature, step, datetime Show additional information on screenshots. Comma-separated list of url, feature, step, datetime, or remove to disable. Ordered as listed.
failed_prefix failed_ Prefix failed screenshots with failed_ string. Useful to distinguish failed and intended screenshots.
filename_pattern {datetime:u}.{feature_file}.feature_{step_line}.{ext} File name pattern for successful assertions.
filename_pattern_failed {datetime:u}.{failed_prefix}{feature_file}.feature_{step_line}.{ext} File name pattern for failed assertions.

File name tokens

Token Substituted with Example value(s)
{ext} The extension of the file captured html or png
{failed_prefix} The value of failed_prefix from configuration failed_, error_ (do include the _ suffix, if required)
{url} Full URL http_example_com_mypath_subpath_query_myquery_1_plus_2_fragment
{url_origin} Scheme with domain http_example_com
{url_relative} Path + query + fragment mypath_subpath_query_myquery_1_plus_2_fragment
{url_domain} Domain example_com
{url_path} Path mypath_subpath
{url_query} Query myquery_1_plus_2
{url_fragment} Fragment somefragment
{feature_file} The filename of the .feature file currently being executed, without extension my_example.feature -> my_example
{step_line} Step line number 1, 10, 100
{step_line:%03d} Step line number with leading zeros. Modifiers are from sprintf(). 001, 010, 100
{step_name} Step name without Given/When/Then and lower-cased. i_am_on_the_test_page
{datetime} Current date and time. defaults to Ymd_His format. 20010310_171618
{datetime:U} Current date and time as microtime. Modifiers are from date(). 1697490961192498

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

drevops/behat-screenshot 适用场景与选型建议

drevops/behat-screenshot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.76M 次下载、GitHub Stars 达 23, 最近一次更新时间为 2021 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 drevops/behat-screenshot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drevops/behat-screenshot 我们能提供哪些服务?
定制开发 / 二次开发

基于 drevops/behat-screenshot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.76M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 25
  • 依赖项目数: 16
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 4
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2021-07-27