Using faker library to feed form inputs in Laravel Dusk

Randomizing your data is always a good approach in Automation Testing. When using Laravel Dusk we can make use of the faker library to randomize the input fields.

Let’s dive into the example straightaway

Using Faker Library with Laravel Dusk

We will make use of Faker library to put random fake data into our registration form which has different input fields.

registration form laravel dusk

 

Create a new dusk test

Let’s create a new dusk test to automate registration form sign-up process

php artisan dusk:make registrationTest

This will create a new test class named registerTest.php in your tests > Browser directory.

Import faker in dusk test

To use the faker library, import the faker library into the test class using

use withFaker;

Also include the class into your testclass namespace

use Illuminate\Foundation\Testing\WithFaker;

Using faker in dusk test

We can now make use of faker library to generate dummy data. Here is how the complete tests looks

<?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
{

    public function setUp(){
        parent::setUp();
    }

    use withFaker;

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

        $this->browse(function (Browser $browser) {
            $browser->visit('/register')
                    ->assertSee('Register')
                    ->type('firstname', $this->faker->firstName)
                    ->type('lastname', $this->faker->firstName)
                    ->type('email', $this->faker->email)
                    ->type('password', $this->faker->password)
                    ->type('address1', $this->faker->streetAddress)
                    ->type('address2', $this->faker->secondaryAddress)
                    ->type('city', $this->faker->city)
                    ->select('state')
                    ->type('zip', $this->faker->postcode)
                    ->press('Sign Up')
                    ->assertSee('You are now registered');
        });
    }


}

We are making use of different faker data to fill out the form info.

Running Test

To run your dusk test , go to your terminal / command prompt. Navigate to the project directory and run

php artisan dusk --filter it_asserts_that_registration_works

laravel dusk using faker

Next up, Learn How to Choose Random Radio Option, Checkbox Option and Dropdown Option

Related Articles

If you like our content, please consider buying me a coffee.

Thank you for your support!

Buy me a coffeeBuy me a coffee
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 Using faker library to feed form inputs in Laravel Dusk

  • Hi,
    Could you please explain how we can use the Faker library to put random fake data in the Pages.

    Thanks

  • Hi,

    If ‘Clicking Links and buttons’ topic is clicked it reloads the page but does not open the linked page. Could you please check on this.

    Thanks,

Leave a reply:

Your email address will not be published.

Site Footer