Laravel Dusk Guide: Mastering Browser Testing in Web Development

When creating a website or web app, one of the primary things to ensure is that response on different devices and even different browsers is consistent. It makes sure the users have a good experience on your website. For that, you have to conduct browser testing on the website.

But with massive Laravel websites and web apps, manual testing can be quite tedious. That’s where Laravel Dusk comes in. It’s a game-changer tool that simplifies and streamlines automated browser testing for your sites and apps.

Laravel development services use Dusk to write expressive, readable tests that mimic how real users interact with your app. We’ll cover this tool in detail through this blog and show how you can use it properly.

What is Laravel Dusk?

Laravel Dusk is a powerful and expressive browser automation and testing framework built specifically for Laravel applications. It allows you to write automated tests that mimic how real users interact with your web app, eliminating the need for tedious manual testing.

Think of it as a robot that can click buttons, fill out forms, and navigate your application, just like a human user. But unlike a human, Dusk never gets bored or makes mistakes. It runs your tests consistently and reliably, ensuring that your application is always working as intended.

Its user-friendly API makes it easy to interact with the browser, and it simplifies the testing process for JavaScript-driven applications. Overall, Dusk is a valuable asset used by Laravel experts to ensure the smooth functionality and user experience of Laravel applications in various scenarios.

Some other powerful tools for Laravel applications include Laravel Socialite, Laravel Horizon, etc.

Why Use Automated Browser Testing?

Automated browser testing refers to the process of using automated tools to simulate user interactions with a web application in a real web browser. This type of testing is crucial for various reasons:

  • Efficiency and Speed: Automated tests can be executed much faster than manual tests. They allow for quick and efficient testing of large and complex web applications, enabling faster release cycles.
  • Repeatability: Automated tests can be repeated as often as necessary without any additional cost. This ensures that the same set of tests is executed consistently, reducing the risk of overlooking critical issues during development.
  • Regression Testing: As software evolves, new features and changes are introduced. Automated browser tests help in detecting regressions, ensuring that existing functionality continues to work as expected after each code change.
  • Cross-Browser Compatibility: Automated browser testing allows testing on multiple browsers and browser versions. This is crucial for ensuring that a web application works correctly and consistently across different platforms and devices.
  • Scalability: As web applications grow in complexity, the number of test cases increases. Automated testing provides a scalable solution to handle a large number of test cases, making it feasible to maintain the quality of the application as it expands.
  • Cost-Effective: While there is an initial investment in setting up automated tests, the long-term benefits include time and cost savings. Automated tests reduce the need for manual testing efforts, allowing testing teams to focus on more complex and creative aspects of testing.
  • Continuous Integration and Continuous Deployment (CI/CD): Automated browser tests integrate well with CI/CD pipelines. They can be triggered automatically after each code change, providing rapid feedback to development teams and ensuring that only stable and thoroughly tested code is deployed to production.
  • Consistency: Automated tests execute the same set of steps and verifications consistently, eliminating the variability introduced by human testers. This consistency improves the reliability of the testing process.
  • Parallel Testing: Automated testing tools can run tests in parallel on multiple machines or browsers simultaneously. This helps in reducing the overall testing time, especially when dealing with a large test suite.
  • Early Detection of Issues: Automated tests can be executed early in the development process, helping in the early detection and resolution of issues. This minimizes the chances of critical defects reaching the production environment.

Automated browser testing is a valuable practice in modern software development, contributing to increased efficiency, reliability, and overall software quality. Web developers use this tactic to complement manual testing efforts, and it is an integral part of a comprehensive testing strategy.

What are the Benefits of Using Laravel Dusk?

Laravel Dusk is an end-to-end testing tool designed specifically for Laravel applications. It provides a convenient and expressive way to write browser tests for web applications. Here are some of the benefits of using Laravel Dusk:

  • Integration with Laravel Ecosystem: Laravel Dusk is built specifically for Laravel applications, making it seamlessly integrated with the Laravel framework. This integration simplifies the process of setting up and running browser tests for Laravel applications.
  • Expressive Syntax: Dusk provides an expressive syntax for writing browser tests. The code is written in PHP and feels natural for developers who are already familiar with Laravel’s syntax. This makes it easy to write and maintain tests.
  • Browser Automation: Dusk allows you to automate browser interactions, such as clicking buttons, filling out forms, and navigating through pages. This enables end-to-end testing that closely simulates user interactions with the application.
  • Headless and Real Browsers: Dusk supports both headless and real browsers for testing. Headless browsers are useful for faster test execution in environments without a graphical interface, while real browsers provide a more realistic simulation of user interactions.
  • Database Transactions: Dusk wraps each test case in a database transaction, which is rolled back after the test completes. This ensures that database changes made during the test do not affect the actual database, providing a clean slate for each test.
  • Artisan Commands: Dusk comes with Artisan commands that make it easy to run tests from the command line. This is particularly useful for integrating browser tests into continuous integration (CI) pipelines.
  • Screenshots and Console Output: Dusk automatically captures screenshots of the browser at various points during the test, allowing you to visually inspect the state of the application. It also captures console output, making it easier to debug issues.
  • Parallel Test Execution: Dusk supports parallel test execution, which can significantly reduce the overall test execution time. This is valuable when dealing with a large test suite or when aiming for faster feedback in a CI/CD environment.
  • User Authentication: Dusk simplifies the process of testing user authentication by providing convenient methods for logging in and out of the application. This is particularly useful for testing features that require authenticated user access.
  • Continuous Integration: Dusk is designed to work well with continuous integration systems. It integrates seamlessly with tools like PHPUnit and can be easily incorporated into CI/CD pipelines for automated testing.

Laravel Dusk offers a powerful and Laravel-centric solution for end-to-end testing. It provides web experts with the tools needed to ensure the functionality and reliability of their Laravel applications. It contributes to a comprehensive testing strategy, helping catch issues early in the development process.

How to Set Up Laravel Dusk?

Now that you understand the benefits of Laravel Dusk for automated browser testing let’s see how you can set it up on your website or web app:

Install Laravel Dusk

Installing Laravel Dusk involves using Composer, the dependency manager for PHP, to add the Dusk package to your Laravel project. Here’s how the installation process goes:

Step 1: Create a Laravel Project (If Not Already Created)

If you don’t have a Laravel project, you can create one using the following Composer command. Replace “your-project-name” with the desired name for your project:

composer create-project --prefer-dist laravel/laravel your-project-name

Navigate to your project directory:

cd your-project-name

Step 2: Install Laravel Dusk Using Composer

Once your Laravel project is set up, you can install Laravel Dusk as a development dependency. Open a terminal and run the following command:

composer require --dev laravel/dusk

The ‘–dev’ flag indicates that Laravel Dusk is a development-only dependency, and it won’t be installed in a production environment.

Step 3: Configure Laravel Dusk

After installing Dusk, you need to configure it. Laravel Dusk comes with a handy Artisan command to help with the configuration process. Run the following command:

php artisan dusk:install

This command publishes the DuskServiceProvider to your Laravel application. It creates a ‘dusk.php’ configuration file in the ‘config’ directory.

Step 4: Configure Browser Drivers

By default, Laravel Dusk uses the Chrome browser for testing. Ensure you have Chrome installed on your machine. If you want to use Firefox or other browsers, additional configuration is required.

  • Headless Chrome (Default): No additional setup is needed.
  • Firefox: Install the geckodriver. You can download it from Mozilla’s Geckodriver releases.

Dusk supports other browsers as well. Refer to the Laravel Dusk documentation for specific instructions on configuring drivers for browsers other than Chrome and Firefox.

Step 5: Verify Configuration

Review the dusk.php configuration file located in the config directory to ensure that the default settings meet your requirements. You can customize the base URL, set screenshot options, and configure other aspects of Dusk’s behavior in this file.

Step 6: Run Initial Dusk Test

After installation and configuration, it’s a good practice to run a basic Dusk test to ensure everything is set up correctly. Use the following Artisan command:

php artisan dusk

This command will run the default example test provided by Dusk, opening a browser window (or running headlessly) and performing some basic interactions. If the test passes, you have successfully installed and configured Laravel Dusk.

Configure Dusk Environment

This stage of the process involves making changes to the Laravel application to enable the use of Laravel Dusk for browser testing. Here are the steps for the same:

Step 1: Add Dusk Service Provider and Facade

Open the config/app.php file in your Laravel project and add the Dusk service provider and facade to the respective arrays.

In the providers array, add:
'providers' => [
    // ...

In the aliases array, add:

'aliases' => [
    // ...
    'Dusk' => Laravel\Dusk\DuskFacade::class,

This registers the Dusk service provider and facade, allowing you to use Dusk within your Laravel application.

After that, we have already published the Dusk configuration and configured the browser drivers. So, let’s move to the next step.

Step 2: Configure Dusk Environment Variables

The Dusk configuration file (config/dusk.php) includes environment variables that control various aspects of Dusk’s behavior. Open the .env file in your Laravel project and add or modify the following variables:

  • DUSK_DRIVER: The browser driver to use (e.g., “chrome” or “firefox”).
  • DUSK_DRIVER_BINARY: Path to the browser binary (null uses the default path).
  • DUSK_HEADLESS: Whether to run the browser in headless mode (true or false).
  • DUSK_DISABLE_DISPLAY: Whether to disable displaying the browser window (true or false).
  • DUSK_PORT: The port on which the browser driver listens.
  • DUSK_HOST: The host on which the browser driver runs.
  • DUSK_URL: The base URL for the application being tested.

Step 3: Configure Environment-Specific Settings (Optional)

For Dusk-specific environment settings, you can create a .env.dusk.local file in your Laravel project. This file will be loaded only when running Dusk tests. Define any environment-specific variables or overrides in this file.

Step 4: Verify Configuration

Before proceeding further, verify that the Dusk configuration is accurate by checking the values in the dusk.php configuration file and your .env file. Ensure that the browser driver, headless mode, and other settings match your testing requirements.

Create Browser Tests

Creating browser tests with Laravel Dusk involves using the Dusk API to simulate user interactions with your web application. Here are detailed steps for creating browser tests in Laravel Dusk:

Step 1: Generate a Dusk Test

Use the following Artisan command to generate a new Dusk test file:

php artisan dusk:make YourTestName

Replace “YourTestName” with a meaningful name for your test. This command will create a new test file in the tests/Browser directory of your Laravel application.

Step 2: Write Your Browser Test

Open the generated test file (located at tests/Browser/YourTestName.php) in a code editor, and you’ll find a basic template for a Dusk test. Customize the test according to your application’s requirements.

Here’s a simple example of a Dusk test:

namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class YourTestName extends DuskTestCase
    public function testExample()
        $this->browse(function (Browser $browser) {

In this example:

  • YourTestName: Replace this with a descriptive name for your test class.
  • testExample(): This is the actual test method. Inside the browse closure, you define the actions that the browser should perform.

Step 3: Customize Your Test

Customize the test according to the specific functionality you want to test. Here are some common actions you can perform using the Dusk API:

Visit a URL:


Click a Link or Button:

$browser->clickLink('Click Me');
// or

Fill a Form:

$browser->type('email', '')
        ->type('password', 'secret')

Assert Text or Element Presence:

$browser->assertSee('Welcome to the Dashboard');

Take Screenshots:


Perform Assertions:


Step 4: Run Your Dusk Tests

Ensure your Laravel application is running:

php artisan serve

Run your Dusk tests using the following Artisan command:

php artisan dusk

This command will execute your Dusk tests, launching a browser window (or running headlessly) and performing the actions defined in your test methods.

Step 5: Review Test Results

After the test run is completed, Dusk will provide feedback on whether the tests passed or failed. It will also generate screenshots and a video recording of the test, which can be found in the tests/Browser/screenshots and tests/Browser/videos directories, respectively.

Step 6: Iterate and Enhance Tests

As you enhance your web application or add new features, iterate on your Dusk tests. Create additional tests to cover different scenarios and ensure comprehensive test coverage.

By following these steps, you can create effective browser tests using Laravel Dusk to validate the functionality and user interactions in your Laravel application.

That concludes the setup of Laravel Dusk for your website or app. If you find it hard to follow or implement, I suggest you consult with a Laravel development agency for the best results.

FAQs on Laravel Dusk

How does Laravel Dusk work?
Laravel Dusk uses a headless version of Google Chrome to run your tests. It offers a simple and familiar syntax that allows developers to write tests using Laravel's built-in testing infrastructure. These tests can be run through artisan commands or in a CI/CD environment.
How does Laravel Dusk compare to other automated browser testing tools?
Laravel Dusk is specifically designed for Laravel applications, making it a great choice for developers who are already familiar with the Laravel framework. It offers a simpler and more intuitive syntax compared to other testing tools, and it allows for parallel testing, making it faster and more efficient.
Are there any resources available for learning Laravel Dusk?
Yes, there are various resources available for learning Laravel Dusk, such as the official Laravel documentation, video tutorials, and online courses. Additionally, the Laravel community is very active and provides support and guidance for those learning and using Dusk for automated browser testing.


As modern web applications become more complex, the need for reliable and efficient testing mechanisms grows. Laravel Dusk addresses this demand by providing developers with a seamless way to perform end-to-end testing, simulating user interactions with their applications.

The benefits of Laravel Dusk are manifold. Its integration with the Laravel framework ensures a smooth and expressive testing experience, leveraging the familiarity of Laravel’s syntax. With the ability to automate browser interactions, developers can efficiently test various scenarios, from form submissions to complex user journeys, ensuring the robustness of their applications.

So, if you want this tool or others on your website or web app, connect with our experts today!

Mayur Upadhyay is a tech professional with expertise in Shopify, WordPress, Drupal, Frameworks, jQuery, and more. With a proven track record in web development and eCommerce development.

Leave a comment