diff --git a/src/Api/Concerns/InteractsWithScreen.php b/src/Api/Concerns/InteractsWithScreen.php index 8fe49f7b..b208782a 100644 --- a/src/Api/Concerns/InteractsWithScreen.php +++ b/src/Api/Concerns/InteractsWithScreen.php @@ -9,11 +9,11 @@ trait InteractsWithScreen /** * Performs a screenshot of the current page and saves it to the given path. */ - public function screenshot(bool $fullPage = true, ?string $filename = null): self + public function screenshot(bool $fullPage = true, ?string $filename = null, ?string $scale = null): self { $filename = $this->getFilename($filename); - $this->page->screenshot($fullPage, $filename); + $this->page->screenshot($fullPage, $filename, $scale); return $this; } @@ -21,11 +21,11 @@ public function screenshot(bool $fullPage = true, ?string $filename = null): sel /** * Performs a screenshot of an element and saves it to the given path. */ - public function screenshotElement(string $selector, ?string $filename = null): self + public function screenshotElement(string $selector, ?string $filename = null, ?string $scale = null): self { $filename = $this->getFilename($filename); - $this->page->screenshotElement($selector, $filename); + $this->page->screenshotElement($selector, $filename, $scale); return $this; } diff --git a/src/Playwright/Page.php b/src/Playwright/Page.php index d97f3db3..3271e183 100644 --- a/src/Playwright/Page.php +++ b/src/Playwright/Page.php @@ -427,9 +427,9 @@ public function reload(): self /** * Make screenshot of the page. */ - public function screenshot(bool $fullPage = true, ?string $filename = null): ?string + public function screenshot(bool $fullPage = true, ?string $filename = null, ?string $scale = null): ?string { - $binary = $this->screenshotBinary($fullPage); + $binary = $this->screenshotBinary($fullPage, $scale); if ($binary === null) { return null; @@ -441,10 +441,12 @@ public function screenshot(bool $fullPage = true, ?string $filename = null): ?st /** * Make screenshot of a specific element. */ - public function screenshotElement(string $selector, ?string $filename = null): string + public function screenshotElement(string $selector, ?string $filename = null, ?string $scale = null): string { $locator = $this->locator($selector); - $binary = $locator->screenshot(); + $binary = $locator->screenshot([ + 'scale' => $scale ?? 'css', + ]); return Screenshot::save($binary, $filename); } @@ -594,12 +596,12 @@ public function isClosed(): bool /** * Screenshots the page and returns the binary data. */ - private function screenshotBinary(bool $fullPage = true): ?string + private function screenshotBinary(bool $fullPage = true, ?string $scale = null): ?string { $response = Client::instance()->execute( $this->guid, 'screenshot', - $this->screenshotOptions($fullPage) + $this->screenshotOptions($fullPage, $scale) ); /** @var array{result: array{binary: string|null}} $message */ @@ -651,14 +653,14 @@ private function isPageLevelOperation(string $method): bool /** * @return array */ - private function screenshotOptions(bool $fullPage = true): array + private function screenshotOptions(bool $fullPage = true, ?string $scale = null): array { return [ 'type' => 'png', 'fullPage' => $fullPage, 'caret' => 'hide', 'animations' => 'disabled', - 'scale' => 'css', + 'scale' => $scale ?? 'css', // 'css' or 'device' ]; } diff --git a/tests/Browser/Webpage/ScreenshotTest.php b/tests/Browser/Webpage/ScreenshotTest.php index cc15fceb..fda6fa50 100644 --- a/tests/Browser/Webpage/ScreenshotTest.php +++ b/tests/Browser/Webpage/ScreenshotTest.php @@ -29,6 +29,17 @@ ->toBeTrue(); }); +it('captures a screenshot with device scale', function (): void { + Route::get('/', fn (): string => 'Hello World'); + + $page = visit('/'); + + $page->screenshot(filename: 'device-scale-screenshot.png', scale: 'device'); + + expect(file_exists(Screenshot::path('device-scale-screenshot.png'))) + ->toBeTrue(); +}); + it('captures a screenshot of an specific element', function (): void { Route::get('/', fn (): string => '

Text outside of screenshot element

@@ -42,3 +53,17 @@ expect(file_exists(Screenshot::path('element-screenshot.png'))) ->toBeTrue(); }); + +it('captures a screenshot of an element with device scale', function (): void { + Route::get('/', fn (): string => '
+

Text outside of screenshot element

+
Text inside of screenshot element
+
'); + + $page = visit('/'); + + $page->screenshotElement('#screenshot-element', 'element-device-scale-screenshot.png', scale: 'device'); + + expect(file_exists(Screenshot::path('element-device-scale-screenshot.png'))) + ->toBeTrue(); +});