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

laravel dusk form example

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');
        });
    }


}

 

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

2 comments On Filling Form Data with Laravel Dusk

  • Hi,
    After clearing the text by using clear method. How can I assert that the textarea is cleared. I meant the textarea isEmpty.

    Thanks

  • 1. How to ‘uncheck’ the checked or selected radio button, how to assert it for the same.

    2. With the ‘check’ method, it is not selecting or clicking on the checkbox.
    Please give me the suggestion.

Leave a reply:

Your email address will not be published.

Site Footer