bencarter78@hotmail.com's avatar

Checking multiple checkboxes with Laravel dusk

Hi there

Can anyone tell me how you can check multiple checkboxes with Dusk? I have the following code...

@foreach ($departments as $department)
    <tr>
        <td>
            <input type="checkbox" name="department_id[]" value="{!! $department->id !}}" />
        </td>
        <td>{!! $department->department !!}</td>
    </tr>
@endforeach 

In my test I am trying to select one of those checkboxes...

$browser->visit('my-page')->check("department_id[]");

I get the error invalid selector: An invalid or illegal selector was specified

Can anyone help?

Thanks

0 likes
5 replies
Defrag's avatar

Perhaps try something like:

...->check ("input:checkbox[name='department_id[]'][value='1']")

Or whatever value you need there. Not sure how specific the selectors need to (or can) be, but it's worth a shot.

1 like
Edwin Krause's avatar

This worked on the local DEV environment, but failed in the pipeline -> Invalid selector

bencarter78@hotmail.com's avatar

@Defrag That almost worked I just needed to tweak it

$browser->check("input[name='department_id[]']");

This will select the first one however it got me thinking how to select say the first third and fifth ones.

I think the following should work

// View
@foreach ($departments as $department)
    <tr>
        <td>
            <input type="checkbox" name="department_id[{!! $department->id !}}]" value="{!! $department->id !}}" />
        </td>
        <td>{!! $department->department !!}</td>
    </tr>
@endforeach 

// Test

// You could loop over or be explicit about which ones you want to select 
$browser->check("input[name='department_id[" . $department->id . "]']")
number6's avatar

Not satisfied with this solution, but it works:

        <div
            v-for="(service, idx) in services"
            class="form-check"
          >
            <input class="form-check-input" :name="'my_services[' + idx +']'" type="checkbox" :id="service._id" :value="service._id" v-model="selectedServices">
            <label class="form-check-label">@{{ service.name }}</label>
          </div>

The key is in the :name attribute. Then the laravel dusk piece, clicking the 4th checkbox.

$browser->visit(new SomePage)->check('my_services[3]')

EDIT Sorry for raising from the dead; just noticed that.

Please or to participate in this conversation.