Dusk Screenshots to S3 of Failing tests

Posted: 2018-06-26 13:17:34

I can be really annoying to troubleshoot a failed test in Dusk when using CI systems. What I ended up doing was setting up my project to send these files to S3 on fail.

Here are the steps

Setup You App

This is just S3 storage so make sure you have a bucket and a folder in the bucket to write to. Basically for AWS you make an IAM with a key and secret and let it read/write to this folder. Might look like this policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::your-bucket"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "*"
            ],
            "Resource": [
                "arn:aws:s3:::your-bucket/*"
            ],
            "Effect": "Allow"
        }
    ]
}

or limited it just to the folder screenshots

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:HeadBucket"
            ],
            "Resource": "*",
            "Effect": "Allow",
            "Sid": "VisualEditor1"
        },
        {
            "Action": [
                "s3:ListBucket",
                "s3:GetBucketLocation"
            ],
            "Resource": [
                "arn:aws:s3:::your-bucket",
                "arn:aws:s3:::your-bucket/*"
            ],
            "Effect": "Allow",
            "Sid": "VisualEditor0"
        },
        {
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::your-bucket/screenshots",
                "arn:aws:s3:::your-bucket/screenshots/*"
            ],
            "Effect": "Allow",
            "Sid": "VisualEditor2"
        }
    ]
}

Now in your tests/DuskTestCase.php file add this:

    public function tearDown()
    {
        if (env("TRAVIS")) {
        $files = \File::files(base_path("tests/Browser/screenshots"));

        if ($files) {
            foreach ($files as $file) {
                Storage::disk('travis_fails')
                    ->put($file->getFileName(), $file->getContents());
            }
        }
        }
        parent::tearDown();
    }

This will run on Travis based tests saving files to the bucket.

I added to my config/filesystems.php file this:

    'disks' => [
        'travis_fails' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => "eu-west-1",
            'root' => "screenshots",
            'bucket' => 'foo-bucket',
            'url' => env('AWS_URL'),
        ],
                /// 

Now for TravisCI

Setup TravisCI

Then in Travis under Settings -> Environment Variables we make sure to add our key and secret with the proper key format:

AWS_SECRET_ACCESS_KEY=foo
AWS_ACCESS_KEY_ID=bar

Now You are Ready to Fail!

Ok so on your next fail you will see in your s3 bucket some of those super handy screenshots.