1. Filling hidden fields
When testing certain JS components (e.g Autocomplete, Datepickers) It can be strenuous writing actions to simulate DOM clicks/interactions with said components. Since most of these components eventually save the eventual value to a hidden field. It can be more expedient to fill in the hidden field directly with the value. This prevents flaky tests and ensures that we are not testing things we do not own/control (third party components).
While Laravel dusk does not provide us with a method like
$browser->fillHidden($field, $value), we can create it ourselves using Dusk Browser Macros
2. Mocking HTML Geolocation
I once had to test a page that needed to provide Geolocation results to the HTML site so it could display some results. There was no direct mock method available so I had to override the
getCurrentPosition method which would eventually be called by the page.
3. Using XPath selectors
Occasionally I’d run into situations where I’m unable to use CSS selectors to locate an element. These often happen with dynamic tables or in my case with a third party js component which I couldn’t modify. Laravel Dusk, however, doesn’t support XPath selectors directly and often requires accessing the underlying WebDriver instance.
The only slight [inconsequential] annoyance to this approach is that it might require you to break the fluent method calls on the
4. Full Page screenshots
Laravel dusks provide us with screenshots of our failed tests and these can be very helpful to know why our tests don’t pass. However, sometimes the error or the element in question might be below the fold.
To create full page screenshots in Laravel Dusk we have to create a
captureFailuresFor() method in our
tests\DuskTestCase.php which would override the one originally defined in
Now, whenever we call
$browser->screenshot('$shotname') or when there’s an error. we’d get a full page screenshot
5. Accessing browser error logs
Ok. This is not a limitation, just something interesting I found. We can access browser console logs by calling
Be warned though, it doesn’t contain the output of
Thanks for reading this, hopefully, you found one or more tip(s) useful.
p.s Please let me know if you notice any errors or inaccuracies in the post and I’d be happy correct it immediately.