Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Selenium with Laravel for testing?

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.

like image 917
user1 Avatar asked Nov 16 '16 16:11

user1


People also ask

What is laravel automation?

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.

Can Selenium be used with PHP?

The Selenium automation framework supports many programming languages such as Python, PHP, Perl, Java, C#, and Ruby. But if you are looking for a server-side programming language for automation testing, Selenium WebDriver with PHP is the ideal combination.

What is feature testing in laravel?

Feature testing can be used to test the smallest working part of your application. Let's consider an Blog Application where you have Post Model, A Post has a title and a body. Let's write some feature test with TDD approach to test our Post Model functionality.


1 Answers

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!

like image 86
alariva Avatar answered Sep 20 '22 21:09

alariva