Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Browser testing with Dusk
📖 Scenario: You are building a simple Laravel web application with a homepage that has a welcome message and a button to visit the about page.You want to write browser tests using Laravel Dusk to check that the homepage loads correctly and the navigation works.
🎯 Goal: Create a Laravel Dusk browser test that opens the homepage, checks the welcome message, clicks the about page link, and verifies the about page content.
📋 What You'll Learn
Create a Dusk test class named HomePageTest
Set up the test to visit the homepage URL /
Check that the homepage contains the text Welcome to Laravel
Click the link with text About Us
Verify the about page contains the text About Laravel
💡 Why This Matters
🌍 Real World
Browser testing with Laravel Dusk helps ensure your web pages load correctly and user interactions work as expected before deploying your app.
💼 Career
Many Laravel developer jobs require writing automated browser tests to maintain high-quality, bug-free web applications.
Progress0 / 4 steps
1
Create the Dusk test class
Create a Dusk test class named HomePageTest inside the tests/Browser directory that extends Laravel\Dusk\TestCase.
Laravel
Hint
Use the php artisan dusk:make HomePageTest command or create the file manually with the correct namespace and class declaration.
2
Add the test method to visit homepage
Inside the HomePageTest class, add a public method named test_homepage_shows_welcome that uses $this->browse with a closure receiving a Browser instance named $browser.
Laravel
Hint
Define a public method starting with test_ and call $this->browse passing a closure with Browser $browser.
3
Visit homepage and check welcome text
Inside the closure in test_homepage_shows_welcome, use $browser->visit('/') to open the homepage, then use assertSee('Welcome to Laravel') to check the welcome message.
Laravel
Hint
Chain the visit and assertSee methods on the $browser instance.
4
Click About Us link and verify about page
Extend the browser chain by adding clickLink('About Us') to click the about page link, then add assertSee('About Laravel') to verify the about page content.
Laravel
Hint
Chain the clickLink and assertSee methods after the previous assertions.
Practice
(1/5)
1. What is the primary purpose of Laravel Dusk in web development?
easy
A. To optimize website images for faster loading
B. To automate browser testing by simulating user actions
C. To generate API documentation
D. To create database migrations automatically
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 B
Quick 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
A. $browser->navigate('/home')->expectText('Welcome');
B. $browser->open('/home')->checkText('Welcome');
C. $browser->goTo('/home')->verifyText('Welcome');
D. $browser->visit('/home')->assertSee('Welcome');
Solution
Step 1: Recall Laravel Dusk syntax for visiting pages
The correct method to visit a page is visit(), and to check text is assertSee().
Step 2: Identify correct method names
Options B, C, and D use incorrect method names like open(), goTo(), and navigate(), which do not exist in Dusk.
Final Answer:
$browser->visit('/home')->assertSee('Welcome'); -> Option D
Quick 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?
A. The method click() should be press() to click buttons
B. The type() method cannot fill text inputs
C. assertSee() cannot check text on the page
D. visit() method requires a full URL, not a path
Solution
Step 1: Review Dusk methods for clicking buttons
In Laravel Dusk, to click a button by its text, the correct method is press(), not click().
Step 2: Validate other methods
type() fills inputs, assertSee() checks text, and visit() accepts paths, so these are correct.
Final Answer:
The method click() should be press() to click buttons -> Option A
Quick 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
A. $browser->pick('color', 'Green')->press('Submit');
B. $browser->choose('color', 'Green')->press('Submit');
C. $browser->select('color', 'Green')->press('Submit');
D. $browser->set('color', 'Green')->press('Submit');
Solution
Step 1: Recall the method to select dropdown options in Dusk
The correct method to select an option from a dropdown is select().
Step 2: Verify other options are invalid
Methods like choose(), pick(), and set() do not exist or are incorrect for dropdown selection in Dusk.
Final Answer:
$browser->select('color', 'Green')->press('Submit'); -> Option C
Quick Check:
Use select() for dropdowns in Dusk [OK]
Hint: Use select() to pick dropdown options before pressing buttons [OK]