Laravel Dusk Testing Laravel Dusk Testing

February 14, 2022

laravel

I do like testing websites, both as I code them, after they are running, but also to extract the odd snippet of data.

Laravel has a Browser testing environment called Dusk

It uses the chrome browser to test a website as a human would use it.

It can also make screenshots at different sizes to mirror different size screens.

Having moved to a new machine, copied over the code base, the dusk tests were failing. To fix it i needed to run

apt-get install chromium

php artisan dusk:install

I have added these to the ansible setup which I will blog about in the future.

Whilst writing the tests, i wanted a way to just run the single test rather than the entire suite. This calls just the one test:

php artisan dusk tests/Browser/blog404Test.php

What i wanted to test for was that a page isnt found and returns a 404 error page. Dusk cant do that directly, so the dusk test looks for '404' to appear in the page. But dusk tests can also run vanilla phpunit tests which can assert the status is 404.

        $this->browse(function (Browser $browser) {
            $browser->visit('https://allotmentandy.github.io/missingPage/')
                    ->assertSee('404');

            $response = $this->get('https://allotmentandy.github.io/missingPage/')
                        ->assertStatus(404);
        }

I also wanted to test that there was no errors in the console;

            $consoleLog = ($browser->driver->manage()->getLog('browser'));
            $this->assertEqualsCanonicalizing($consoleLog, []);

This creates an Array of the console output and asserts it is a blank array. Simples!


If you would like to contact me with this form on londinium.com, ilminster.net or via Twitter @andylondon