Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/Api/Concerns/InteractsWithScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ 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;
}

/**
* 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;
}
Expand Down
18 changes: 10 additions & 8 deletions src/Playwright/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -651,14 +653,14 @@ private function isPageLevelOperation(string $method): bool
/**
* @return array<string, mixed>
*/
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'
];
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Browser/Webpage/ScreenshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => '<div>
<h1>Text outside of screenshot element</h1>
Expand All @@ -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 => '<div>
<h1>Text outside of screenshot element</h1>
<div id="screenshot-element">Text inside of screenshot element</div>
</div>');

$page = visit('/');

$page->screenshotElement('#screenshot-element', 'element-device-scale-screenshot.png', scale: 'device');

expect(file_exists(Screenshot::path('element-device-scale-screenshot.png')))
->toBeTrue();
});