You should be able to check if the attribute exists using assertSourceHas
Documentation: https://laravel.com/docs/5.8/dusk#assert-source-has
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi Folks!
I am using Laravel 5.8 in combination with Laravel Dusk.
How can I check if an element has an attribute?
For example I would like to check if an input field has the readonly attribute or not.
I have tried to use assertSee but it does not work?
I double checked if the attribute isset in the HTML code using a pause and opened the console - its there!?
This is the HTML rendered by Laravel:
<section id="email-form-section" class="field" wfd-id="32"><label for="i-email" class="label " wfd-id="37"><span wfd-id="39">E-Mail Address</span> <span class="is-mandatory" wfd-id="38">*</span></label> <div class="columns is-multiline is-vcentered" style="margin-bottom: 0px;" wfd-id="33"><div class="column" wfd-id="34"><div class="control has-icons-left" wfd-id="35">
<input name="email" dusk="email" id="i-email" type="email" maxlength="255" value="[email protected]" required="required" readonly="readonly" class="input " wfd-id="115">
<span class="icon is-small is-left" wfd-id="36"><i class="far fa-envelope"></i></span></div></div></div></section>
And this is the test case related to this issue:
namespace Tests\Browser;
use App\User;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Spatie\Permission\Traits\HasRoles;
use Symfony\Component\Debug\Tests\Fixtures\TraitWithInternalMethod;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ProfileTest extends DuskTestCase
{
use DatabaseMigrations;
/** @test */
public function its_profile_form_is_up()
{
$user = factory('App\User')->create([
'first_name' => "Aegon",
'last_name' => "Tagaryen",
'academic_title' => "King",
'function' => "Torture",
'phone' => "+414444444",
'mobile' => "+41788888888",
'location_id' => 1,
'email' => "[email protected]",
'password' => bcrypt('secret')
]);
$this->browse(function (Browser $browser) use ($user) {
$browser->loginAs($user)
->visit('/profile')
->assertValue('@first_name', 'Aegon');
$browser->with('@email', function ($emailInput) {
$emailInput->assertSee('readonly');
});
});
}
}
And finaly the console output:
Time: 21.51 seconds, Memory: 26.00 MB
There was 1 failure:
1) Tests\Browser\ProfileTest::its_profile_form_is_up
Did not see expected text [readonly] within element [body [dusk="email"]].
Failed asserting that false is true.
I have tried to use another selector e.g. #i-email but it didn't worked out.
Does somebody have an idea what I do wrong?
You should be able to check if the attribute exists using assertSourceHas
Documentation: https://laravel.com/docs/5.8/dusk#assert-source-has
Please or to participate in this conversation.