|
| 1 | +# Laravel Visual Diff |
| 2 | + |
| 3 | +[](https://packagist.org/packages/beyondcode/laravel-visual-diff) |
| 4 | +[](https://travis-ci.org/beyondcode/laravel-visual-diff) |
| 5 | +[](https://scrutinizer-ci.com/g/beyondcode/laravel-visual-diff) |
| 6 | +[](https://packagist.org/packages/beyondcode/laravel-visual-diff) |
| 7 | + |
| 8 | +This package can create a visual diff of two screenshots of your Laravel application. It works for both - regular HTTP tests, as well as tests using Laravel Dusk. |
| 9 | +Behind the scenes, it uses [Pixelmatch](https://github.com/mapbox/pixelmatch) to diff the two images. |
| 10 | + |
| 11 | +Here are two basic examples of how the package works: |
| 12 | + |
| 13 | +Using Laravel Dusk: |
| 14 | + |
| 15 | +```php |
| 16 | + |
| 17 | +class ExampleTest extends DuskTestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * A basic browser test example. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function testBasicExample() |
| 25 | + { |
| 26 | + $this->browse(function (Browser $browser) { |
| 27 | + $browser->visit('/') |
| 28 | + ->visualDiff(); |
| 29 | + }); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +Using Laravel HTTP Testing: |
| 36 | + |
| 37 | +```php |
| 38 | + |
| 39 | +class ExampleTest extends TestCase |
| 40 | +{ |
| 41 | + /** |
| 42 | + * A basic test example. |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function testBasicExample() |
| 47 | + { |
| 48 | + $this->get('/') |
| 49 | + ->visualDiff(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +## Requirements |
| 56 | + |
| 57 | +This package requires node 7.6.0 or higher and the Pixelmatch Node library. |
| 58 | + |
| 59 | +You can install the Pixelmatch on MacOS via NPM: |
| 60 | + |
| 61 | +```bash |
| 62 | +npm install pixelmatch |
| 63 | +``` |
| 64 | + |
| 65 | +Or you could install it globally |
| 66 | + |
| 67 | +```bash |
| 68 | +npm install pixelmatch --global |
| 69 | +``` |
| 70 | + |
| 71 | +### Custom node and npm binaries |
| 72 | + |
| 73 | +Depending on your setup, node or npm might be not directly available to VisualDiff. |
| 74 | +If you need to manually set these binary paths, you can do this by setting the `npm_binary` and `node_binary` config settings in the `visualdiff.php` configuration file. |
| 75 | + |
| 76 | +## Installation |
| 77 | + |
| 78 | +This package can be installed through Composer. |
| 79 | + |
| 80 | +```bash |
| 81 | +composer require beyondcode/laravel-visual-diff |
| 82 | +``` |
| 83 | + |
| 84 | +## Usage |
| 85 | + |
| 86 | +The package will automatically register the `VisualDiffServiceProvider` with your Laravel application. |
| 87 | + |
| 88 | +The package adds a new method to either the `TestResponse` or the `Browser` class which is called `visualDiff`. |
| 89 | +Depending on how you want to create visual diffs, follow the usage guidelines for the Laravel Dusk integration or the HTTP testing integration. |
| 90 | + |
| 91 | +The `visualDiff` method accepts a name, that will be used for the screenshot generation. If you do not provide a name, the package will try and guess the test name. If this does not work for you, please provide a name manually instead. |
| 92 | + |
| 93 | +### Dusk Integration |
| 94 | + |
| 95 | +Just call the `visualDiff()` method on the Laravel Dusk `Browser` instance that you use for testing: |
| 96 | + |
| 97 | +```php |
| 98 | +$this->browse(function (Browser $browser) { |
| 99 | + $browser->visit('/') |
| 100 | + ->visualDiff(); |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +This will automatically create a screenshot in all provided resolutions and create a diff, if a previous screenshot is available. |
| 105 | + |
| 106 | +### HTTP Testing Integration |
| 107 | + |
| 108 | +Just call the `visualDiff()` method on the `TestResponse` instance that you use for testing: |
| 109 | + |
| 110 | +```php |
| 111 | +$this->get('/') |
| 112 | + ->visualDiff(); |
| 113 | +``` |
| 114 | + |
| 115 | +This will automatically create a screenshot in all provided resolutions and create a diff, if a previous screenshot is available. |
| 116 | + |
| 117 | +### Specify multiple resolutions |
| 118 | + |
| 119 | +Creating diffs for multiple resolutions can be very useful - especially if you want to test responsive websites and applications. |
| 120 | + |
| 121 | +You can define all possible resolutions in your `visualdiff.php` configuration file: |
| 122 | + |
| 123 | +```php |
| 124 | +/** |
| 125 | + * Define all different resolutions that you want to use when performing |
| 126 | + * the regression tests. |
| 127 | + */ |
| 128 | +'resolutions' => [ |
| 129 | + [ |
| 130 | + 'width' => 1920, |
| 131 | + 'height'=> 1080 |
| 132 | + ] |
| 133 | +] |
| 134 | +``` |
| 135 | + |
| 136 | +Alternatively, you may want to only create one specific diff in multiple resolutions. |
| 137 | +You can do this using the `visualDiffForResolutions` method and provide an array with the resolutions to test. |
| 138 | + |
| 139 | +```php |
| 140 | +$this->get('/') |
| 141 | + ->visualDiffForResolutions([ |
| 142 | + [ |
| 143 | + 'width' => 1920, |
| 144 | + 'height' => 1080 |
| 145 | + ], |
| 146 | + [ |
| 147 | + 'width' => 640, |
| 148 | + 'height' => 480 |
| 149 | + ], |
| 150 | + ]); |
| 151 | +``` |
| 152 | + |
| 153 | +### See diff results in your terminal window |
| 154 | + |
| 155 | +If you use [iTerm2](https://www.iterm2.com/) as your terminal of choice, you will see an image representation of the diff when you run your tests. |
| 156 | + |
| 157 | +--SCREENSHOT-- |
| 158 | + |
| 159 | +### Changelog |
| 160 | + |
| 161 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 162 | + |
| 163 | +## Contributing |
| 164 | + |
| 165 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 166 | + |
| 167 | +### Security |
| 168 | + |
| 169 | +If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. |
| 170 | + |
| 171 | +## Credits |
| 172 | + |
| 173 | +- [Marcel Pociot](https://github.com/mpociot) |
| 174 | +- [All Contributors](../../contributors) |
| 175 | + |
| 176 | +## License |
| 177 | + |
| 178 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments