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.
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
Next up, Learn How to Choose Random Radio Option, Checkbox Option and Dropdown Option