0
0
Laravelframework~10 mins

Browser testing with Dusk in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Browser testing with Dusk
Start Test
Launch Browser
Navigate to URL
Perform Actions
Check Assertions
Close Browser
End Test
This flow shows how a Dusk test starts by opening a browser, navigating, performing actions, checking results, and then closing the browser.
Execution Sample
Laravel
public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
                ->type('email', 'user@example.com')
                ->type('password', 'secret')
                ->press('Login')
                ->assertPathIs('/home');
    });
}
This test opens the login page, fills in email and password, clicks login, and checks if redirected to home.
Execution Table
StepActionBrowser StateResultNotes
1Launch browserBrowser opened, blank pageSuccessBrowser window opens ready for commands
2Visit '/login'Page loads login formSuccessLogin page is displayed
3Type 'email' fieldEmail input filled with 'user@example.com'SuccessEmail typed into form
4Type 'password' fieldPassword input filled with 'secret'SuccessPassword typed into form
5Press 'Login' buttonForm submittedSuccessLogin form submitted
6Assert path is '/home'Current URL is '/home'PassUser redirected to home page
7Close browserBrowser closedSuccessTest ends, browser closes
💡 Test ends after browser closes following successful assertion or failure
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
email inputemptyuser@example.comuser@example.comuser@example.comuser@example.com
password inputemptyemptysecretsecretsecret
current URLabout:blank/login/login/home/home
browser stateclosedopenopenopenclosed
Key Moments - 3 Insights
Why does the test fail if the assertion 'assertPathIs' does not match?
Because the test expects the browser URL to be '/home' after login. If it is different, the assertion fails, stopping the test as shown in step 6 of the execution_table.
What happens if the 'press' method is called before filling the inputs?
The form submits empty fields, likely causing a failed login and assertion failure at step 6, as the URL won't change to '/home'.
Does the browser stay open after the test finishes?
No, the browser closes at the last step (step 7), ensuring no leftover browser windows remain after tests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the browser state after step 4?
ABrowser open with email and password typed
BBrowser closed
CBrowser open but no inputs typed
DBrowser crashed
💡 Hint
Check the 'Browser State' column at step 4 in the execution_table
At which step does the test check if the user is redirected to '/home'?
AStep 5
BStep 6
CStep 3
DStep 7
💡 Hint
Look for the 'Assert path is' action in the execution_table
If the 'type' method for password is skipped, what likely happens?
ATest passes normally
BBrowser closes early
CTest fails at assertion step
DLogin button is disabled
💡 Hint
Refer to key_moments about missing input causing assertion failure
Concept Snapshot
Browser testing with Dusk:
- Use $this->browse() with a closure
- Chain browser actions: visit(), type(), press()
- Use assertions like assertPathIs() to check results
- Browser opens, runs commands, then closes
- Tests simulate real user actions in a browser
Full Transcript
Browser testing with Laravel Dusk involves opening a browser window, navigating to a web page, performing user actions like typing and clicking, then checking if the expected page or element appears. The test starts by launching the browser, visiting the login page, typing the email and password, pressing the login button, and asserting the URL changes to '/home'. If the assertion passes, the browser closes and the test ends successfully. If any step fails, the test stops and reports failure. Variables like input fields and current URL change as the test runs. This process helps ensure the website works correctly from a user's perspective.