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

$browser->clear('email');

Selecting Dropdowns

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

To select any random value from the dropdown

$browser->select('state');

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

$browser->check('terms');

$browser->uncheck('terms');

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

And this is our dusk test to fill form data.

<?php

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) {
            $browser->visit('/register')
                ->assertSee('Register')
                ->type('firstname', 'Taylor')
                ->type('lastname', 'Otwell')
                ->type('email', 'taylor.otwell@laravel.com')
                ->type('password', 'secret')
                ->type('address1', 'Dreamland')
                ->type('address2', 'House Number 42')
                ->type('city', 'Chicago')
                ->select('state')
                ->type('zip', '34423')
                ->radio('gender', 'male')
                ->check('terms')
                ->pause(2000)
                ->press('Sign Up')
                ->pause(1000)
                ->assertSee('You are now registered');
        });
    }


}

 

tgugnani: Web Stuff Enthusiast.