Browser testing with Dusk helps you check if your website works correctly by simulating real user actions in a browser.
Browser testing with Dusk in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Laravel
use Laravel\Dusk\Browser; use Tests\DuskTestCase; class ExampleTest extends DuskTestCase { public function testBasicExample() { $this->browse(function (Browser $browser) { $browser->visit('/') ->assertSee('Laravel'); }); } }
Tests extend DuskTestCase to use browser features.
The browse method runs browser actions inside a closure.
Examples
Laravel
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->visit('/login')
->type('email', 'user@example.com')
->type('password', 'secret')
->press('Login')
->assertPathIs('/home');
});
}Laravel
public function testClickLink()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->clickLink('About Us')
->assertSee('Our Story');
});
}Sample Program
This simple Dusk test opens the home page and checks if the text "Welcome to Laravel" is visible.
Laravel
<?php namespace Tests\Browser; use Laravel\Dusk\Browser; use Tests\DuskTestCase; class HomePageTest extends DuskTestCase { public function testHomePageLoads() { $this->browse(function (Browser $browser) { $browser->visit('/') ->assertSee('Welcome to Laravel'); }); } }
Important Notes
Make sure to run php artisan dusk:install once to set up Dusk.
Run tests with php artisan dusk to see browser actions live.
Dusk uses ChromeDriver, so Chrome must be installed on your machine.
Summary
Dusk lets you test your website by controlling a real browser automatically.
Write tests to visit pages, fill forms, click buttons, and check results.
Run tests to catch problems early and keep your site working well.
Practice
1. What is the primary purpose of Laravel Dusk in web development?
easy
Solution
Step 1: Understand Laravel Dusk's role
Laravel Dusk is designed to automate browser testing by controlling a real browser to simulate user actions like clicking and typing.Step 2: Compare options with Dusk's purpose
Options A, B, and C relate to optimization, database, and documentation, which are unrelated to browser testing.Final Answer:
To automate browser testing by simulating user actions -> Option BQuick Check:
Browser automation = To automate browser testing by simulating user actions [OK]
Hint: Remember: Dusk controls browsers to test user actions [OK]
Common Mistakes:
- Confusing Dusk with database tools
- Thinking Dusk generates documentation
- Assuming Dusk optimizes assets
2. Which of the following is the correct way to start a browser test with Laravel Dusk?
easy
Solution
Step 1: Recall Laravel Dusk syntax for visiting pages
The correct method to visit a page isvisit(), and to check text isassertSee().Step 2: Identify correct method names
Options B, C, and D use incorrect method names likeopen(),goTo(), andnavigate(), which do not exist in Dusk.Final Answer:
$browser->visit('/home')->assertSee('Welcome'); -> Option DQuick Check:
Use visit() and assertSee() in Dusk [OK]
Hint: Use visit() to open page and assertSee() to check text [OK]
Common Mistakes:
- Using non-existent methods like open() or goTo()
- Confusing assertSee() with verifyText()
- Mixing up method chaining order
3. Given the following Dusk test code, what will be the result?
$browser->visit('/login')
->type('email', 'user@example.com')
->type('password', 'secret')
->press('Login')
->assertPathIs('/dashboard');medium
Solution
Step 1: Analyze the test steps
The test visits the login page, fills email and password fields, presses the Login button, then checks if the browser path is /dashboard.Step 2: Understand Dusk behavior
Thetype()method fills inputs correctly, andassertPathIs()verifies the current URL path. If login redirects to /dashboard, the test passes.Final Answer:
The test will pass if login redirects to /dashboard -> Option AQuick Check:
Correct input and path check = The test will pass if login redirects to /dashboard [OK]
Hint: type() fills inputs; assertPathIs() checks URL path [OK]
Common Mistakes:
- Thinking type() cannot fill inputs
- Assuming assertPathIs() ignores path
- Confusing syntax errors with runtime failures
4. Identify the error in this Dusk test code snippet:
$browser->visit('/profile')
->type('name', 'John Doe')
->click('Save')
->assertSee('Profile updated');medium
Solution
Step 1: Review Dusk methods for clicking buttons
In Laravel Dusk, to click a button by its text, the correct method ispress(), notclick().Step 2: Validate other methods
type()fills inputs,assertSee()checks text, andvisit()accepts paths, so these are correct.Final Answer:
The method click() should be press() to click buttons -> Option AQuick Check:
Use press() for buttons, not click() [OK]
Hint: Use press() to click buttons, not click() [OK]
Common Mistakes:
- Using click() instead of press() for buttons
- Thinking type() can't fill inputs
- Believing visit() needs full URL
5. You want to test a form that has a dropdown with options 'Red', 'Green', and 'Blue'. Which Dusk method correctly selects 'Green' before submitting the form?
hard
Solution
Step 1: Recall the method to select dropdown options in Dusk
The correct method to select an option from a dropdown isselect().Step 2: Verify other options are invalid
Methods likechoose(),pick(), andset()do not exist or are incorrect for dropdown selection in Dusk.Final Answer:
$browser->select('color', 'Green')->press('Submit'); -> Option CQuick Check:
Use select() for dropdowns in Dusk [OK]
Hint: Use select() to pick dropdown options before pressing buttons [OK]
Common Mistakes:
- Using choose() or pick() instead of select()
- Trying to set dropdown value with set()
- Forgetting to press the submit button
