Skip to content

Commit 59faa49

Browse files
committed
[#166] Fixed triggering a full screen screenshot in the test will not resize the window back to the original size.
1 parent 139260a commit 59faa49

2 files changed

Lines changed: 82 additions & 30 deletions

File tree

src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,28 @@ protected function getScreenshotFullscreenWithResize(): string {
437437
$session->getDriver();
438438

439439
// Store original window size to restore it later.
440-
$original_width = 0;
441-
$original_height = 0;
440+
// Default to the standard size set in beforeScenarioInit().
441+
$original_width = 1440;
442+
$original_height = 900;
442443

443-
// Get the current window size.
444-
if (method_exists($session, 'getWindowSize')) {
445-
$size = $session->getWindowSize();
446-
$original_width = $size['width'];
447-
$original_height = $size['height'];
444+
// Get the current window size using JavaScript.
445+
try {
446+
$original_dimensions = $session->evaluateScript("
447+
return {
448+
width: window.outerWidth,
449+
height: window.outerHeight
450+
};
451+
");
452+
453+
if (!empty($original_dimensions) && is_array($original_dimensions)) {
454+
$original_width = isset($original_dimensions['width']) && is_numeric($original_dimensions['width'])
455+
? (int) $original_dimensions['width'] : 1440;
456+
$original_height = isset($original_dimensions['height']) && is_numeric($original_dimensions['height'])
457+
? (int) $original_dimensions['height'] : 900;
458+
}
459+
}
460+
catch (\Exception) {
461+
// Use default dimensions if JavaScript evaluation fails.
448462
}
449463

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

499+
// Add a small delay to ensure the resize completes before taking
500+
// screenshot.
501+
usleep(100000);
502+
485503
// Take the screenshot.
486504
$screenshot = $this->getScreenshot();
487505

488-
// Restore the original window size if we have dimensions.
489-
if ($original_width > 0 && $original_height > 0) {
490-
try {
491-
$session->resizeWindow($original_width, $original_height, 'current');
492-
}
493-
catch (\Exception) {
494-
// Ignore errors during restoration.
495-
}
506+
// Always restore the original window size.
507+
try {
508+
$session->resizeWindow($original_width, $original_height, 'current');
509+
}
510+
catch (\Exception) {
511+
// Ignore errors during restoration - best effort attempt.
496512
}
497513

498514
return $screenshot;

tests/phpunit/Unit/ScreenshotContextResizeTest.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,42 @@ public function testGetScreenshotFullscreenWithResize(): void {
3232
$session = $this->createMock(Session::class);
3333
$driver = $this->createMock(Selenium2Driver::class);
3434

35-
// Mock the JavaScript evaluation to return document dimensions.
35+
// Mock the JavaScript evaluation to return both original and document
36+
// dimensions.
3637
$session->method('evaluateScript')
37-
->willReturn([
38-
'scrollWidth' => 1440,
39-
'scrollHeight' => 2000,
40-
]);
41-
42-
// Cannot mock getWindowSize in PHPUnit 11, so we'll rely on default values.
43-
$session->expects($this->once())
38+
->willReturnOnConsecutiveCalls(
39+
// First call: get original window dimensions.
40+
[
41+
'width' => 1440,
42+
'height' => 900,
43+
],
44+
// Second call: get document scroll dimensions.
45+
[
46+
'scrollWidth' => 1440,
47+
'scrollHeight' => 2000,
48+
]
49+
);
50+
51+
// Expect resize to be called twice: once to expand, once to restore.
52+
$session->expects($this->exactly(2))
4453
->method('resizeWindow')
45-
->with(1440, 2200, 'current');
54+
->willReturnCallback(function ($width, $height, $name): void {
55+
static $call_count = 0;
56+
$call_count++;
57+
58+
if ($call_count === 1) {
59+
// First call: resize to fullscreen.
60+
$this->assertEquals(1440, $width);
61+
$this->assertEquals(2200, $height);
62+
$this->assertEquals('current', $name);
63+
}
64+
elseif ($call_count === 2) {
65+
// Second call: restore to original.
66+
$this->assertEquals(1440, $width);
67+
$this->assertEquals(900, $height);
68+
$this->assertEquals('current', $name);
69+
}
70+
});
4671

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

74-
// Mock the JavaScript evaluation to return invalid dimensions.
99+
// Mock the JavaScript evaluation to return invalid dimensions for both
100+
// calls.
75101
$session->method('evaluateScript')
76-
->willReturn([
77-
'scrollWidth' => 0,
78-
'scrollHeight' => 0,
79-
]);
80-
102+
->willReturnOnConsecutiveCalls(
103+
// First call: get original window dimensions.
104+
[
105+
'width' => 1440,
106+
'height' => 900,
107+
],
108+
// Second call: get document scroll dimensions (invalid).
109+
[
110+
'scrollWidth' => 0,
111+
'scrollHeight' => 0,
112+
]
113+
);
114+
115+
// Should not resize when dimensions are invalid, but returns regular
116+
// screenshot.
81117
$session->expects($this->never())->method('resizeWindow');
82118

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

0 commit comments

Comments
 (0)