0
0
Laravelframework~20 mins

Browser testing with Dusk in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dusk Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Dusk test do?
Consider this Laravel Dusk test code snippet:
public function testUserLogin() {
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
                ->type('email', 'user@example.com')
                ->type('password', 'secret')
                ->press('Login')
                ->assertPathIs('/home');
    });
}

What is the main behavior this test checks?
Laravel
public function testUserLogin() {
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
                ->type('email', 'user@example.com')
                ->type('password', 'secret')
                ->press('Login')
                ->assertPathIs('/home');
    });
}
AIt checks that a user can log in successfully and is redirected to the home page.
BIt verifies that the login page loads without errors but does not submit the form.
CIt tests that the login form shows validation errors when fields are empty.
DIt confirms that the user is logged out and redirected to the login page.
Attempts:
2 left
💡 Hint
Look at the sequence of actions: visit, type, press, then assert the path.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Dusk test code
Look at this Dusk test snippet:
$this->browse(function (Browser $browser) {
    $browser->visit('/dashboard')
            ->click('button#save')
            ->assertSee('Saved successfully')
});

What is the syntax error here?
Laravel
$this->browse(function (Browser $browser) {
    $browser->visit('/dashboard')
            ->click('button#save')
            ->assertSee('Saved successfully')
});
AThe function keyword is missing before the closure.
BMissing semicolon after the last method call chain.
CIncorrect method name 'click' should be 'press'.
DThe 'assertSee' method requires a second argument.
Attempts:
2 left
💡 Hint
Check the end of the method chain inside the closure.
state_output
advanced
2:00remaining
What is the value of $count after this Dusk test runs?
Given this Dusk test code:
public function testAddItem() {
    $count = 0;
    $this->browse(function (Browser $browser) use (&$count) {
        $browser->visit('/items')
                ->click('@add-item')
                ->waitForText('Item added')
                ->assertSee('Item added');
        $count++;
    });
    return $count;
}

What is the value returned by testAddItem()?
Laravel
public function testAddItem() {
    $count = 0;
    $this->browse(function (Browser $browser) use (&$count) {
        $browser->visit('/items')
                ->click('@add-item')
                ->waitForText('Item added')
                ->assertSee('Item added');
        $count++;
    });
    return $count;
}
Anull
B0
CThrows an error because $count is not accessible
D1
Attempts:
2 left
💡 Hint
Notice the use of 'use (&$count)' and where $count++ happens.
🔧 Debug
advanced
2:00remaining
Why does this Dusk test fail with a timeout error?
This Dusk test fails with a timeout:
$this->browse(function (Browser $browser) {
    $browser->visit('/profile')
            ->click('@save-button')
            ->waitForText('Profile updated', 2)
            ->assertSee('Profile updated');
});

What is the most likely reason for the timeout?
Laravel
$this->browse(function (Browser $browser) {
    $browser->visit('/profile')
            ->click('@save-button')
            ->waitForText('Profile updated', 2)
            ->assertSee('Profile updated');
});
AThe text 'Profile updated' does not appear within 2 seconds after clicking save.
BThe selector '@save-button' is invalid and causes the click to fail silently.
CThe visit method is missing a trailing slash causing a 404 error.
DThe assertSee method is called before the page finishes loading.
Attempts:
2 left
💡 Hint
Check the waitForText timeout duration and the expected text presence.
🧠 Conceptual
expert
2:00remaining
Which option best describes the role of the Browser class in Laravel Dusk?
In Laravel Dusk, what is the primary purpose of the Browser class?
AIt compiles and bundles JavaScript assets for the Laravel application.
BIt handles server-side rendering of views before sending them to the browser.
CIt provides methods to simulate user interactions with the browser like clicking, typing, and navigating pages.
DIt manages database transactions during browser tests to rollback changes automatically.
Attempts:
2 left
💡 Hint
Think about what actions a user performs in a browser and how tests simulate them.