Skip to content

Commit 1a398f5

Browse files
committed
[#31] Added support for fullscreen screenshots.
Signed-off-by: Alex Skrypnyk <alex@drevops.com>
1 parent 41cada4 commit 1a398f5

17 files changed

Lines changed: 1480 additions & 126 deletions

CLAUDE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ composer test # Run PHPUnit tests without coverage
2323
- Coverage reports are generated in .logs/coverage directory
2424

2525
## Recent Improvements
26+
- Added support for fullscreen screenshots with two algorithms:
27+
- Stitch algorithm (default): Takes multiple screenshots while scrolling and stitches them together
28+
- Resize algorithm: Temporarily resizes browser window to capture full page
2629
- Updated autoloader from PSR-0 to PSR-4
2730
- Made constants public as per PHP 8.2+ standards
2831
- Improved error messages for file operations
@@ -34,7 +37,7 @@ composer test # Run PHPUnit tests without coverage
3437
The Behat Screenshot extension provides functionality to capture screenshots during Behat test runs. Its main components are:
3538

3639
1. **BehatScreenshotExtension**: Handles configuration and service container integration
37-
2. **ScreenshotContext**: Provides Behat steps and screenshot capabilities
40+
2. **ScreenshotContext**: Provides Behat steps and screenshot capabilities, including fullscreen screenshot functionality with both stitch and resize algorithms
3841
3. **Tokenizer**: Processes dynamic filename generation with tokens
3942

4043
## Best Practices for Contributing

README.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
## Features
2525

2626
* Captures a screenshot using the `I save screenshot` step.
27+
* Captures fullscreen screenshots with the `I save fullscreen screenshot` step.
2728
* Automatically captures a screenshot when a test fails.
2829
* Supports both HTML and PNG screenshots.
30+
* Supports Selenium and Headless drivers.
2931
* Configurable screenshot directory.
3032
* Automatically purges screenshots after each test run.
31-
* Adds configurable additional information to screenshots.
33+
* Adds additional information to screenshots.
3234

3335
## Installation
3436

@@ -65,6 +67,8 @@ default:
6567
dir: '%paths.base%/screenshots'
6668
on_failed: true
6769
purge: false
70+
always_fullscreen: false
71+
fullscreen_algorithm: stitch # Options: 'stitch' or 'resize'
6872
failed_prefix: 'failed_'
6973
filename_pattern: '{datetime:u}.{feature_file}.feature_{step_line}.{ext}'
7074
filename_pattern_failed: '{datetime:u}.{failed_prefix}{feature_file}.feature_{step_line}.{ext}'
@@ -77,30 +81,73 @@ Given I am on "http://google.com"
7781
Then I save screenshot
7882
```
7983

84+
You can capture fullscreen screenshots:
85+
86+
```gherkin
87+
Given I am on "http://google.com"
88+
Then I save fullscreen screenshot
89+
```
90+
91+
There are two algorithms available for capturing fullscreen screenshots:
92+
93+
1. **Stitch** (default): Takes multiple screenshots while scrolling the page and
94+
stitches them together. This produces high-quality results with proper
95+
content rendering but requires the GD extension.
96+
97+
2. **Resize**: Temporarily resizes the browser window to the full height of the
98+
page to capture everything in one screenshot. This is faster, but may cause
99+
layout issues on some pages.
100+
101+
You can configure which algorithm to use via the `fullscreen_algorithm` option:
102+
103+
```yaml
104+
default:
105+
extensions:
106+
DrevOps\BehatScreenshotExtension:
107+
fullscreen_algorithm: resize # Options: 'stitch' or 'resize'
108+
```
109+
80110
You may optionally specify the size of the browser window in the screenshot
81111
step:
82112
83113
```gherkin
84114
Then I save 1440 x 900 screenshot
115+
# Or with fullscreen
116+
Then I save fullscreen 1440 x 900 screenshot
85117
```
86118

87119
or a file name:
88120

89121
```gherkin
90-
Then I save screenshot to "my_screenshot.png"
122+
Then I save screenshot with name "my_screenshot.png"
123+
# Or with fullscreen
124+
Then I save fullscreen screenshot with name "my_screenshot.png"
125+
```
126+
127+
To always capture fullscreen screenshots, even without explicitly using the
128+
`fullscreen` keyword, set the `always_fullscreen` configuration option to
129+
`true`:
130+
131+
```yaml
132+
default:
133+
extensions:
134+
DrevOps\BehatScreenshotExtension:
135+
always_fullscreen: true
91136
```
92137
93138
## Options
94139
95-
| Name | Default value | Description |
96-
|---------------------------|------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
97-
| `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. |
98-
| `on_failed` | `true` | Capture screenshot on failed test. |
99-
| `purge` | `false` | Remove all files from the screenshots directory on each test run. Useful during debugging of tests. |
100-
| `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. |
101-
| `failed_prefix` | `failed_` | Prefix failed screenshots with `failed_` string. Useful to distinguish failed and intended screenshots. |
102-
| `filename_pattern` | `{datetime:u}.{feature_file}.feature_{step_line}.{ext}` | File name pattern for successful assertions. |
103-
| `filename_pattern_failed` | `{datetime:u}.{failed_prefix}{feature_file}.feature_{step_line}.{ext}` | File name pattern for failed assertions. |
140+
| Name | Default value | Description |
141+
|---------------------------|------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
142+
| `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. |
143+
| `on_failed` | `true` | Capture screenshot on failed test. |
144+
| `purge` | `false` | Remove all files from the screenshots directory on each test run. Useful during debugging of tests. |
145+
| `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`. |
146+
| `fullscreen_algorithm` | `stitch` | Algorithm to use for fullscreen screenshots. Options: `stitch` (captures multiple screenshots while scrolling and stitches them together) or `resize` (temporarily resizes browser window to full page height). The stitch algorithm requires GD extension but produces higher quality results. |
147+
| `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. |
148+
| `failed_prefix` | `failed_` | Prefix failed screenshots with `failed_` string. Useful to distinguish failed and intended screenshots. |
149+
| `filename_pattern` | `{datetime:u}.{feature_file}.feature_{step_line}.{ext}` | File name pattern for successful assertions. |
150+
| `filename_pattern_failed` | `{datetime:u}.{failed_prefix}{feature_file}.feature_{step_line}.{ext}` | File name pattern for failed assertions. |
104151

105152
### File name tokens
106153

@@ -203,9 +250,7 @@ streamlined in the future).
203250
```shell
204251
# Start Chromium in container for Selenium-based tests.
205252
docker run -d -p 4444:4444 -p 9222:9222 selenium/standalone-chromium
206-
```
207253
208-
```shell
209254
# Install Chromium with brew.
210255
brew cask install chromedriver
211256
# Launch Chromium with remote debugging.

behat.yml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ default:
99
dir: %paths.base%/screenshots
1010
on_failed: true
1111
purge: false
12+
always_fullscreen: false
13+
fullscreen_algorithm: stitch # 'stitch' (only if GD ext available) or 'resize'
1214
info_types:
1315
- url
1416
- feature

src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class ScreenshotContextInitializer implements ContextInitializer {
3131
* File name prefix for a failed test.
3232
* @param bool $purge
3333
* Purge dir before start script.
34+
* @param bool $alwaysFullscreen
35+
* Always take fullscreen screenshots.
36+
* @param string $fullscreenAlgorithm
37+
* Algorithm to use for fullscreen screenshots ('stitch' or 'resize').
3438
* @param string $filenamePattern
3539
* File name pattern.
3640
* @param string $filenamePatternFailed
@@ -45,6 +49,8 @@ public function __construct(
4549
protected bool $onFailed,
4650
private readonly string $failedPrefix,
4751
protected bool $purge,
52+
protected bool $alwaysFullscreen,
53+
protected string $fullscreenAlgorithm,
4854
protected string $filenamePattern,
4955
protected string $filenamePatternFailed,
5056
protected array $infoTypes = [],
@@ -70,6 +76,8 @@ public function initializeContext(Context $context): void {
7076
$dir,
7177
$this->onFailed,
7278
$this->failedPrefix,
79+
$this->alwaysFullscreen,
80+
$this->fullscreenAlgorithm,
7381
$this->filenamePattern,
7482
$this->filenamePatternFailed,
7583
$this->infoTypes

src/DrevOps/BehatScreenshotExtension/Context/ScreenshotAwareContextInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ interface ScreenshotAwareContextInterface extends Context {
2020
* Create screenshots on fail.
2121
* @param string $failed_prefix
2222
* File name prefix for a failed test.
23+
* @param bool $always_fullscreen
24+
* Always take fullscreen screenshots.
25+
* @param string $fullscreen_algorithm
26+
* Algorithm to use for fullscreen screenshots ('stitch' or 'resize').
2327
* @param string $filename_pattern
2428
* File name pattern.
2529
* @param string $filename_pattern_failed
@@ -29,15 +33,15 @@ interface ScreenshotAwareContextInterface extends Context {
2933
*
3034
* @return $this
3135
*/
32-
public function setScreenshotParameters(string $dir, bool $on_failed, string $failed_prefix, string $filename_pattern, string $filename_pattern_failed, array $info_types): static;
36+
public function setScreenshotParameters(string $dir, bool $on_failed, string $failed_prefix, bool $always_fullscreen, string $fullscreen_algorithm, string $filename_pattern, string $filename_pattern_failed, array $info_types): static;
3337

3438
/**
3539
* Save screenshot content into a file.
3640
*
3741
* @param array<string,mixed> $options
3842
* Contextual options.
3943
*/
40-
public function iSaveScreenshot(array $options): void;
44+
public function screenshot(array $options): void;
4145

4246
/**
4347
* Adds information to context.

0 commit comments

Comments
 (0)