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?
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.
. 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.
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.
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'));
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With