laravel / dusk

Laravel Dusk provides simple end-to-end testing and browser automation.
https://laravel.com/docs/dusk
MIT License
1.87k stars 320 forks source link

Add `component()` method to get scoped Browser instance #1087

Closed ziadoz closed 6 months ago

ziadoz commented 6 months ago

This PR adds a new component() method that returns a browser instance scoped to a component. It works identically to with(), only it doesn't require a closure, which can be more readable with many components on a page:

// Before
$browser->with(new DatePickerComponent, function ($browser) {
    $browser->typeDate('2021-02-03 04:05:06');
});

$browser->with(new PrimaryCategoryPickerComponent, function ($browser) {
    $browser->typeReference('FOO-123');
    $browser->assertPickerSelected('FOO-123');
});

$browser->with(new SecondaryCategoryPickerComponent, function ($browser) {
    $browser->typeReference('FOO-456');
    $browser->assertPickerSelected('FOO-456');
});

// After
$datePicker = $browser->component(new DatePickerComponent);
$datePicker->typeDate('2021-02-03 04:05:06');

$primaryCategoryPicker = $browser->component(new PrimaryCategoryPickerComponent);
$primaryCategoryPicker->typeReference('FOO-123');
$primaryCategoryPicker->assertPickerSelected('FOO-123');

$secondaryCategoryPicker = $browser->component(new SecondaryCategoryPickerComponent);
$secondaryCategoryPicker->typeReference('FOO-456');
$secondaryCategoryPicker->assertPickerSelected('FOO-456');
github-actions[bot] commented 6 months ago

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

crynobone commented 6 months ago
$browser->with(new DatePickerComponent, function ($browser) {
    $browser->typeDate('2021-02-03 04:05:06');
})->with(new PrimaryCategoryPickerComponent, function ($browser) {
    $browser->typeReference('FOO-123');
    $browser->assertPickerSelected('FOO-123');
})->with(new SecondaryCategoryPickerComponent, function ($browser) {
    $browser->typeReference('FOO-456');
    $browser->assertPickerSelected('FOO-456');
});

Will this usage still be valid after this PR?

ziadoz commented 6 months ago

@crynobone Yes chaining like that should still work, and I've added a test for it.