Laravel Dusk Failed Screenshots Send Email

When you are working with Laravel Dusk and running the tests on a CI like Travis or Heroku. It gets tricky to get out of the screenshots of failed tests.

Here is how I configured my Laravel Dusk to send email to failed test screenshots.

Create a Mailable Class

php artisan make:mail DuskFailure

Modify the build function of DuskFailure class to gather all the screenshots and send them to the blade file.

   use Illuminate\Support\Facades\File;
    public function build()
        $view = $this->view('emails.dusk_failure');
        $files = File::allFiles('tests/Browser/screenshots');
        return $this->view('emails.dusk_failure', [
            'files' => $files

Mail Blade File

Create a new blade file named dusk_failure.blade.php inside in the resources > views > emails directory

Livewire Component Library

@forelse ($files as $key => $file)
  <img src="{{ $message->embed($file->getPathname()) }}">
  <p><b>{{$key}}</b> {{$file->getPathname()}}</p>
  No images found

In this blade template, we loop through the files and output the image along with the image name.

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.

Configure Dusk to send Email on Failure

Finally, in your DuskTestCase class, override captureFailuresFor method to send emails

    protected function captureFailuresFor($browsers) {
        Mail::to('')->send(new DuskFailure());

That’s about it. Now whenever you have laravel dusk test failures, You will get an email along with the screenshot of the failed tests.

Site Footer