Browser testing with Laravel Dusk (Part 1)

Tram Ho

1 Introduction

Automation testing is an automated process that runs the steps of performing a test case by machine, which reduces errors, time and boredom with manual testing. Especially at the end of the project, the number of test cases is much, the regression test will take a lot of time. Moreover, when the system has many different languages, it will be difficult for the tester and prone to errors.

As a developer, usually we only write unit tests in the project, but unit tests only help us cover the lines of codes (line of codes), the inputs and outputs of the functions that we have written but not covered. gain user actions and actions on the system. And in version 5.4, laravel has launched a new feature called laravel dusk to help us do this.

2 installation

2.1 Project

The first step is to prepare a project to run the test, here I have prepared a sample project with the functions to log in, register, and add dirty correction to serve the test. You can clone here

2.2 Installing the Laravel dusk package

Add the laravel dusk package with the following command:

And install

After running the above command, Laravel will create a tests / Browser directory containing the sample test cases, download reports, and install chrome drivers. In case of running an error due to chrome incompatibility, you can try some of the following:

  • Update google chrome to the latest version and then run it again

  • Check the current chrome version on your device (go to google chrome -> Help -> About Google Chrome) and install chrome dirver with the corresponding version by command, eg chrome 79.0.3945.88

2.3 Configuration

Let’s take a look at the driver method in the DuskTestCase.php file

$ options is a set of options when running tests, by default there will be --headless options that do not enable the browser to simulate when running the test, --start-maximized enable chrome with full screen size to simulate When running the test, you can refer to the other options here . Here I will remove the --headless option to enable the browser to simulate when running the test. My code will be as follows

2.3 File env

By default, Laravel dusk will use environment variables in the project’s .env file, to manage the environment variables specifically for laravel dusk, we copy the .env.example file to a file called .env.dusk. In this article I ran the test at localhost, so I will bark the variable APP_URL as follows:

.env.dusk

3 Write a test

Now we will write a test. First I will delete the ExampleTest.php file in the tests / Browser folder because in this example I will test the registration function of this project.

With four field names, email, pasword, password confirmation and corresponding html code as follows:

Create a test function registration file with the command

Laravel dusk will generate a RegisterTest.php file in the tests / Browser directory with the following content:

I will delete the test example and replace it with my test cases

  • The test goes to the Register page and checks to see if there are elements

  • Test registration function

And run the following command to run the test case

And results

Test result

4 Result

Above are the installation steps and a rough example of Laravel dusk, in the next section I will introduce other parts such as how to interact with elements, how to structure the directory … Thank you for your interest. The monitoring center, see you later in the following sections ?

Refer

Share the news now

Source : Viblo