spatie/laravel-screenshot
Driver-based Laravel package for taking web page screenshots with great defaults. Use Browsershot (Chromium) or Cloudflare Browser Rendering, customize viewport/format/quality, save to files, and easily fake/assert screenshots in tests.
In your test, you can call the fake() method on the Screenshot facade to fake the screenshot generation. Because the screenshot generation is faked, your tests will run much faster.
// in your test
use Spatie\LaravelScreenshot\Facades\Screenshot;
beforeEach(function () {
Screenshot::fake();
});
You can use the assertSaved method to assert that a screenshot was saved. You can pass a string path or a callable.
use Spatie\LaravelScreenshot\Facades\Screenshot;
Screenshot::assertSaved('screenshots/homepage.png');
With a callable for more detailed assertions:
use Spatie\LaravelScreenshot\Facades\Screenshot;
Screenshot::assertSaved(function ($screenshot, string $path) {
return $path === 'screenshots/homepage.png'
&& $screenshot->url === 'https://example.com';
});
You can use the assertUrlIs method to assert that a screenshot was taken of a specific URL:
Screenshot::assertUrlIs('https://example.com');
You can use the assertHtmlContains method to assert that a screenshot was taken from HTML containing a given string:
Screenshot::assertHtmlContains('Hello World');
You can use the assertQueued method to assert that a screenshot was queued for generation. You can pass a string path or a callable.
Screenshot::assertQueued('screenshots/homepage.png');
With a callable for more detailed assertions:
Screenshot::assertQueued(function ($screenshot, string $path) {
return $path === 'screenshots/homepage.png'
&& $screenshot->fullPage === true;
});
You can use the assertNotQueued method to assert that no screenshots were queued, or that a specific path was not queued.
// Assert nothing was queued
Screenshot::assertNotQueued();
// Assert a specific path was not queued
Screenshot::assertNotQueued('screenshots/other.png');
How can I help you explore Laravel packages today?