Laravel Dusk assertSelected()

I’m using Laravel Dusk to do some automated testing. It’s great!

I found a “gotcha” today.

When using $browser->assertSelected($field, $value), if your $value variable is not a string, it will always fail. This is because the MakesAssertions trait’s ->selected($field, $value) function uses the triple equals (===) ensuring the values AND TYPES are the same for the two variables in question. And the value found in the selector will always be a string.

The easy workaround is just to cast your value as a string. Like so:

$value = 1;
$field = ‘selector_id’;
$browser->assertSelected($field, (string)$value);