Browser testing with Laravel Dusk (Database migration and Authentication)

Tram Ho

1. Introduction

In this article (link) I have guided the basic installation steps. This article shows how you can work with your database when testing Laravel Dusk.

The first is to create a separate database for testing, to avoid affecting the original data of the Laravel app. We will configure in the .env.dusk file as follows.

Here I have created a database called laravel_dusk and use the Mysql database management system

2. Use DatabaseMigations in Dusk

Default Laravel provides three traits is DatabaseMigrations , DatabaseTransactions and RefreshDatabase to work with the database in testing. However, when testing with dusk we can only use DatabaseMigrations because DatabaseTransaction not applied in the browser test (HTTP Request) and RefreshDatabase is built on DatabaseTransaction .

You only need to import DatabaseMigrations in the files containing test cases, it will run all the up() methods before each test and then run all the down() methods after each test. It means that it will create tables, add / edit columns before testing and then drop them all after the test is finished.

Methods up() and down() when creating a table

To use DatabaseMigrations just import from IlluminateFoundationTestingDatabaseMigrations and use in class to define test cases as follows.

DatabaseMigations does this by running the php artisan migrate command in the setUp () method before running the test and then running the php artisan migrate:rollback after running each test in the tearDown() . See also setUp and tearDown methods

As you can see in the above example, we can also use factory methods to create test data in the database, then use this data in the dusk test.

2.1 Create sample data before running the Dusk Test.

After creating the database tables using migrate, we can create some necessary data into the sample database to serve the Laravel app by seeding using the command

You can also do this by setUp() the setUp() method again and running the above command right in this method

This ensures that the sample data will be created before testing.

2.2 Disadvantages of using DatabaseMigrations

Using DatabaseMigations will take a lot of time to test. In case of large database with lots of tables, DatabaseMigations will create all tables before testing and will destroy them after testing, this process will take a lot of time.

So instead of using DatabaseMigations you can manually run the migrate and db: seed commands before testing and in case you just want to delete the data after testing but keep the tables unchanged, you can use the truncate method. to delete data in tables in tearDown () method


3.1 Login and Logout in Laravel Dusk

When you want to test a function that requires users to log in first, you can use the loginAs method provided by laravel dusk

This will help us to be able to login right away without having to go from login page and save a lot of time. The loginAs method loginAs parameters that are objects of the User class or a user id or even an email

In contrast, when you want to log out, just call the logOut()

3.2 Authentication test

Laravel dusk provides a number of methods for you to test the authentication functions. To check when the user is using the web as a guest, we can use the assertGuest()

And vice versa you can also check whether the user has successfully logged in or not based on the assertAuthenticated()

Or, check the user logged in with a specific user using assertAuthenticatedAs()

4. Result

Above is an introduction to DatabaseMigations and ways to work with authentication in laravel dusk. Thank you for your interest in tracking, see you in the following section 😄

Share the news now

Source : Viblo