Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create .env file for test with Laravel Dusk

I'm using Dusk to do a simple login test.

I created a .env.dusk file so that the test uses an alternate database and does not delete the data that was registered on the platform.

Archive .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk
DB_USERNAME=root
DB_PASSWORD=123456

Archive .env.dusk

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk_test
DB_USERNAME=root
DB_PASSWORD=123456

LoginTest.php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class LoginTest extends DuskTestCase
{
    use DatabaseMigrations;
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testLogin()
    {
        $user = factory(\App\User::class)->create(['email' => '[email protected]']);

        $this->browse(function ($browser) use ($user) {
            $browser->visit('/login')
                    ->type('email', $user->email)
                    ->type('password', 'secret')
                    ->press('Login')
                    ->assertPathIs('/home');
        });
    }
}

But when I run the tests it does not change the database and it deletes all data from the database used in the application.

How can I solve this problem?

like image 970
Lucas Lopes Avatar asked Mar 27 '17 12:03

Lucas Lopes


People also ask

How do you run a dusk test?

For laravel dusk to be able to run the browser automation test on your local environment, It needs to know your application url. For this, Go to your environment file . env , and set the APP_URL property to be your application url. This value should match the URL you use to access your application in the browser.

For what do the .env is used in Laravel?

. env file, as its name suggest, is a local where you put all your environment setup, such as database credentials, cache drivers and etc. Everything that is about the server that the project is running, and may have different values for different servers, are setup here.

What is Laravel dusk for?

Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your local computer. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilize any other Selenium compatible driver you wish.


2 Answers

You have to append an environment value, (which matches the environment you'll be initializing Dusk in), to the end of your .env.dusk file name, (e.g. - .env.dusk.local). For reference check the documentation on Dusk Environment Handling.

Update: If you're still having issues per your comments, put the following at the top of your testLogin function and report back what it says dd(env('APP_ENV'));

like image 102
alaric Avatar answered Oct 06 '22 23:10

alaric


@alaric

I changed the .env.dusk.testing file to .env.dusk.local

I ran the php artisan serve and created a new user in the laravel_dusk database.

I ran the php artisan serve again and then the php artisan dusk to run the tests and create a new user with the same email but in the database laravel_dusk_test and it continues registering in laravel_dusk.

like image 33
Lucas Lopes Avatar answered Oct 06 '22 23:10

Lucas Lopes