Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for media features #890

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions bin/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ const callChrome = async pup => {
await page.emulateMediaType(request.options.emulateMedia);
}

if (request.options && request.options.emulateMediaFeatures) {
await page.emulateMediaFeatures(JSON.parse(request.options.emulateMediaFeatures));
}

if (request.options && request.options.viewport) {
await page.setViewport(request.options.viewport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ Browsershot::url('https://example.com')
->savePdf($pathToPdf);
```

You can also emulate [media features](https://www.w3.org/TR/mediaqueries-5/), such as dark mode or reduced motion.

```php
Browsershot::url('https://example.com')
->emulateMediaFeatures([
['name' => 'prefers-color-scheme', 'value' => 'dark']
])
->save($pathToImage);
```
5 changes: 5 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ public function emulateMedia(?string $media): static
return $this->setOption('emulateMedia', $media);
}

public function emulateMediaFeatures(array $features): static
{
return $this->setOption('emulateMediaFeatures', json_encode($features));
}

public function newHeadless(): self
{
return $this->setOption('newHeadless', true);
Expand Down
23 changes: 23 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,29 @@
], $command);
});

it('can set emulate media features', function () {
$command = Browsershot::url('https://example.com')
->emulateMediaFeatures([
['name' => 'prefers-color-scheme', 'value' => 'dark']
])
->createScreenshotCommand('screenshot.png');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'screenshot',
'options' => [
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'emulateMediaFeatures' => '[{"name":"prefers-color-scheme","value":"dark"}]',
'args' => [],
'type' => 'png',
],
], $command);
});

it('can use pipe', function () {
$command = Browsershot::url('https://example.com')
->usePipe()
Expand Down