Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sirhxalot's avatar

Check if element has attribute with Laravel Dusk

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?

0 likes
4 replies
sirhxalot's avatar

Hey @bobbybouwmann

Thank you so much for your hint - it worked out as expected.

The final assertion looks like this:

$browser->assertSourceHas('<input name="email" dusk="email" id="i-email" type="email" maxlength="255" value="[email protected]" required="required" readonly="readonly" class="input ">');

What a mess!

I am not happy with this solution since it doesn't subscribe what I am expecting and its difficult to get the expected source code (had to use pause within the test open chrome console, search the element and copied the outer HTML - too much steps anyhow ;).

In a perfect world I would like to have something like the following:

$browser->assertHasAttribute($field, $expectedAttribute);

If I have time I maybe going to open issue (enhancement request) for this.

Anyhow: Bedankt!

1 like
MarkTierney's avatar

You can do something like this:

$this->assertEquals('true', $browser->attribute('#fieldID', 'readonly'));
1 like

Please or to participate in this conversation.