Automated Browser Testing with Laravel Dusk and BrowserStack
Laravel Dusk is a convenient helper for testing Laravel web applications. It is basically a wrapper around the Selenium SDK which provides an easy-to-use API. By default, Laravel Dusk uses ChromeDriver to run the tests within Chrome.
Unfortunately, it is not sufficient to run tests in Chrome only. The service BrowserStack allows it to run automated test on a variety of browsers (Firefox, Edge, IE 😱…) and devices (Windows, iOS, Android…).
In the following I will show my approach to use Laravel Dusk with BrowserStack. After you have installed Dusk, you will find a
DuskTestCase.php in the tests directory. To use BrowserStack instead of ChromeDriver, you can adjust the base test case like this:
It uses the
RunsWebServer trait (by inxilpro) to start a local web server on port 8515 of the application using
artisan serve :
RunsBrowserStackLocal Trait is responsible for launching BrowserStack local, a service which makes sure that the BrowserStack devices can access your local testing environment on localhost.
This requires the correspondending composer package:
composer require-dev browserstack/browserstack-local
Store your BrowserStack username and key in
.env.testing. Also make sure that
APP_URL points correctly to
A test case could look like this:
Finally, if you run your tests using phpunit, it will automatically start the local web server, launch BrowserStack local and run your test in Internet Explorer:
phpunit --configuration phpunit.xml Browser\LoginTest