Filling Form Data with Laravel Dusk

Dusk provides a variety of wrapper methods around webdriver to easily interact with form elements. In this article we will review these methods.

Filling Text Fields

To type value in the text field, you can make use of type method.

$browser->type('firstname' , 'John');

This method will simulate the action of user typing value John in the input field named firstname with the keyboard.

If you are looking to append value in the field, you can make use append method.

$browser->append('firstname' , 'Mayor');

If you are looking to clear out any already filled value from the fields, make use of clear method


Selecting Dropdowns

Dusk provides a straightforward method select to work with select boxes.

To select any random value from the dropdown


This will select any random state value from the state dropdown.

If you want to select any specific value from the dropdown, you can pass in at second parameter in the select method.

$browser->select('state', 'NC');

Selecting Checkboxes

To select the checkbox you can make use of check method



Selecting Radio Buttons

To select the radio button you can make use of radio method, radio method does not give you can option to select radm radio button from a group.

Thus you have to pass the value of which button you are looking to select

$browser->radio('gender', 'Male);

Laravel Dusk Form Example

Let’s go over an example. I have created a sample form which contains all different types of form fields. Our Form looks like this

laravel dusk form example

And this is our dusk test to fill form data.


namespace Tests\Browser;

use Illuminate\Foundation\Testing\WithFaker;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class registrationTest extends DuskTestCase

    /** @test */
    public function dusk_fill_form_fields(){

        $this->browse(function (Browser $browser) {
                ->type('firstname', 'Taylor')
                ->type('lastname', 'Otwell')
                ->type('email', '')
                ->type('password', 'secret')
                ->type('address1', 'Dreamland')
                ->type('address2', 'House Number 42')
                ->type('city', 'Chicago')
                ->type('zip', '34423')
                ->radio('gender', 'male')
                ->press('Sign Up')
                ->assertSee('You are now registered');



Related Articles

Udemy Laravel Dusk Course

Laravel Dusk Beginner to Advanced

Video Course on Automation Testing, Web Scraping and Creating Bots using Dusk

> 60% OFF

Limited Time Offer

Learn More

Site Footer