Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,28 @@ protected function getScreenshotFullscreenWithResize(): string {
$session->getDriver();

// Store original window size to restore it later.
$original_width = 0;
$original_height = 0;
// Default to the standard size set in beforeScenarioInit().
$original_width = 1440;
$original_height = 900;

// Get the current window size.
if (method_exists($session, 'getWindowSize')) {
$size = $session->getWindowSize();
$original_width = $size['width'];
$original_height = $size['height'];
// Get the current window size using JavaScript.
try {
$original_dimensions = $session->evaluateScript("
return {
width: window.outerWidth,
height: window.outerHeight
};
");

if (!empty($original_dimensions) && is_array($original_dimensions)) {
$original_width = isset($original_dimensions['width']) && is_numeric($original_dimensions['width'])
? (int) $original_dimensions['width'] : 1440;
$original_height = isset($original_dimensions['height']) && is_numeric($original_dimensions['height'])
? (int) $original_dimensions['height'] : 900;
}
}
catch (\Exception) {
// Use default dimensions if JavaScript evaluation fails.
}

$dimensions = $session->evaluateScript("
Expand Down Expand Up @@ -482,17 +496,19 @@ protected function getScreenshotFullscreenWithResize(): string {
// Resize the window to capture the full page height.
$session->resizeWindow($fullscreen_width, $fullscreen_height, 'current');

// Add a small delay to ensure the resize completes before taking
// screenshot.
usleep(100000);

// Take the screenshot.
$screenshot = $this->getScreenshot();

// Restore the original window size if we have dimensions.
if ($original_width > 0 && $original_height > 0) {
try {
$session->resizeWindow($original_width, $original_height, 'current');
}
catch (\Exception) {
// Ignore errors during restoration.
}
// Always restore the original window size.
try {
$session->resizeWindow($original_width, $original_height, 'current');
}
catch (\Exception) {
// Ignore errors during restoration - best effort attempt.
}

return $screenshot;
Expand Down
66 changes: 51 additions & 15 deletions tests/phpunit/Unit/ScreenshotContextResizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,42 @@ public function testGetScreenshotFullscreenWithResize(): void {
$session = $this->createMock(Session::class);
$driver = $this->createMock(Selenium2Driver::class);

// Mock the JavaScript evaluation to return document dimensions.
// Mock the JavaScript evaluation to return both original and document
// dimensions.
$session->method('evaluateScript')
->willReturn([
'scrollWidth' => 1440,
'scrollHeight' => 2000,
]);

// Cannot mock getWindowSize in PHPUnit 11, so we'll rely on default values.
$session->expects($this->once())
->willReturnOnConsecutiveCalls(
// First call: get original window dimensions.
[
'width' => 1440,
'height' => 900,
],
// Second call: get document scroll dimensions.
[
'scrollWidth' => 1440,
'scrollHeight' => 2000,
]
);

// Expect resize to be called twice: once to expand, once to restore.
$session->expects($this->exactly(2))
->method('resizeWindow')
->with(1440, 2200, 'current');
->willReturnCallback(function ($width, $height, $name): void {
static $call_count = 0;
$call_count++;

if ($call_count === 1) {
// First call: resize to fullscreen.
$this->assertEquals(1440, $width);
$this->assertEquals(2200, $height);
$this->assertEquals('current', $name);
}
elseif ($call_count === 2) {
// Second call: restore to original.
$this->assertEquals(1440, $width);
$this->assertEquals(900, $height);
$this->assertEquals('current', $name);
}
});

$session->method('getDriver')->willReturn($driver);
$screenshot_context->method('getSession')->willReturn($session);
Expand Down Expand Up @@ -71,13 +96,24 @@ public function testGetScreenshotFullscreenWithResizeInvalidDimensions(): void {
$session = $this->createMock(Session::class);
$driver = $this->createMock(Selenium2Driver::class);

// Mock the JavaScript evaluation to return invalid dimensions.
// Mock the JavaScript evaluation to return invalid dimensions for both
// calls.
$session->method('evaluateScript')
->willReturn([
'scrollWidth' => 0,
'scrollHeight' => 0,
]);

->willReturnOnConsecutiveCalls(
// First call: get original window dimensions.
[
'width' => 1440,
'height' => 900,
],
// Second call: get document scroll dimensions (invalid).
[
'scrollWidth' => 0,
'scrollHeight' => 0,
]
);

// Should not resize when dimensions are invalid, but returns regular
// screenshot.
$session->expects($this->never())->method('resizeWindow');

$session->method('getDriver')->willReturn($driver);
Expand Down