14

Both Laravel and Selenium use PHPUnit's assertions. In both Laravel and Selenium you write code to make tests (instead of choosing things to do in GUI; e.g. open: google.com, write in: name->queryInput text:"test search", click: name->searchButton). Both Laravel and Selenium are able to visit webpages, submit forms and check the results. You can automate tests both in Laravel and Selenium by adding PHPUnit command to cron.

So why should I use Selenium for testing in Laravel?

The only thing I could think of is that Selenium allows you to choose a browser in which the pages will be opened. So if your test passes using only Laravel testing it may fail for some browsers, like, say, Internet Explorer.

In this Laracast the author says at 1:00:

One problem with that [testing in Laravel] is, it doesn't include JavaScript support. So instead, we're faking the request, we're getting the response, we're inspecting it but no browser or JavaScript engine is involved in that process.

But what are the cons of faking the requests? How would Selenium help? An example would be perfect.

1 Answer 1

16

You actually already got the answer. What you state is correct.

Laravel integrated tests will internally emulate the requests and may use some advantages (like Disabling Middleware, Mocking, Spying, etc) for making sure that you are isolating a particular problem (Test Case). The idea is to test the application without introducing third party components side effects into the battlefield, this would be: the client browsers, external services, etc. These type of tests are really fast and lightweight. Also very suitable for testing API calls.

So Selenium is exactly for covering all those cases, in wich you actually want to cover those scenarios affected by third party components side effects, like JavaScript in either Chrome, IE, Firefox, etc, even in the different versions of those. You can understand this like an attempt to be as close as possible to the real world scenario, where the client browser actually may interfere with the expected behavior of your application. Also, it's possible to trigger screenshots if you want to visually validate CSS or interactive components. It is important to mention that because of this browser hook, these tests are way slower to execute.

The takeaway of this should be that you don't need to either use one or the other exclusively. They work similarly, but in the end, they provide different capabilities. You can have a set of Laravel integration tests, and a set of Selenium tests for those things that matter to you. I suggest you this Laracast

I may provide you an example in my project, not that relevant, but at least a way to show that both test types can coexist in the same project.

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. So it's worth installing all the Selenium components because it then imitates real world scenario as you said. But then it seems that you should always use Selenium since that's what you aim for. I watched the Laracast and imo the same tests could have been written in Selenium and would then be more from real world. So could you give an example where browser doesn't matter at all so I know exactly when to use Laravel's integrated testing and when Selenium? I know that extends my question but it would be nice to have a full picture.
Well, I did not mention something very important. Selenium tests have a drawback, they are way slower, because of the integration with browsers, plus, they depend on havng a concrete browser instance as dependency. In my particular case, I prefer using mostly Laravel integrated tests, and Selenium for just those tests where I need to verify a particular scenario (for example involving JS, or page rendering in a particular browser for a very particular reason).
@mr_jigsaw Id say that the browser does not matter at all as long as you focus on the application logic primarly (more of -say- backend) And it makes sense to use Selenium when you are a bit more focused on UI/UX and browser-specific capabilities, which could be just a few use-case scenarios that you decide to test (more of -say- frontend)
what about laravel dusk?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.