Laravel: PHPUnit and interacting with JavaScript
I see this is a very old question, but just for documentations sake:
If you would like to test with browser functionality you can have a look at Laravel Dusk:
https://laravel.com/docs/5.8/dusk
Dusk uses a Chrome driver to visit your project on your local machine for example. So it will execute the javascript too in case you are using a framework like Vue.js for example.
This is what a test looks like:
public function test_careers_page_shows_vacancies()
{
$this->browse(function (Browser $browser) {
$career = \App\Career::first();
$browser->visit("/careers")
->assertSee("Join our team!")
->pause(1000) // Pause for a second
->waitForText("Careers loaded") // Or wait for text
->assertSee($career->title);
});
}
Note that Dusk will use your local
environment unlike phpunit which might use a testing
environment. For example, I use an sqlite environment for phpunit. But Dusk browses to "http://myproject.test/" which uses a different database. You can solve this by setting up a testing database on you local machine.
You're not doing anything wrong. In this case, PHPUnit is not aware of CSS and JavaScript. I was checking Laravel Testing module source code (which extends PHPUnit functionality) and it just uses a crawler. So, it doesn't run any client-side script. Actually, it doesn't even render the page.
I didn't use it yet, but you may give a try to phpunit-spiderling.