Testing screenshots | laravel-screenshot | Spatie

 SPATIE

  Laravel Screenshot
=====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-screenshot](https://spatie.be/docs/laravel-screenshot/v1)  Basic-usage  Testing screenshots

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-screenshot/v1)

- [ Introduction ](https://spatie.be/docs/laravel-screenshot/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-screenshot/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-screenshot/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-screenshot/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-screenshot/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-screenshot/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-screenshot/v1/about-us)

Basic usage
-----------

- [ Taking screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/taking-screenshots)
- [ Customizing screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/customizing-screenshots)
- [ Saving screenshots to disks ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/saving-screenshots-to-disks)
- [ Queued screenshot generation ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/queued-screenshot-generation)
- [ Testing screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/testing-screenshots)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-screenshot/v1/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-screenshot/v1/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-screenshot/v1/drivers/using-the-cloudflare-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-screenshot/v1/drivers/custom-drivers)

Advanced usage
--------------

- [ Extending with macros ](https://spatie.be/docs/laravel-screenshot/v1/advanced-usage/extending-with-macros)

 Testing screenshots
===================

###  On this page

1. [ assertSaved ](#content-assertsaved)
2. [ assertUrlIs ](#content-asserturlis)
3. [ assertHtmlContains ](#content-asserthtmlcontains)
4. [ Queued screenshot assertions ](#content-queued-screenshot-assertions)

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();
});
```

assertSaved
-----------------------------------------------------------------------------------------

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';
});
```

assertUrlIs
-----------------------------------------------------------------------------------------

You can use the `assertUrlIs` method to assert that a screenshot was taken of a specific URL:

```
Screenshot::assertUrlIs('https://example.com');
```

assertHtmlContains
--------------------------------------------------------------------------------------------------------------

You can use the `assertHtmlContains` method to assert that a screenshot was taken from HTML containing a given string:

```
Screenshot::assertHtmlContains('Hello World');
```

Queued screenshot assertions
--------------------------------------------------------------------------------------------------------------------------------------------

### assertQueued

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;
});
```

### assertNotQueued

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');
```
