---
title: "Laravel Dusk: painless browser automation"
url: "https://aaronfrancis.com/videos/laravel-dusk-painless-browser-automation"
path: "/videos/laravel-dusk-painless-browser-automation"
type: video
author: Aaron Francis
published: 2024-07-19
topic: "Laravel & PHP"
keywords: [laravel dusk, browser automation, testing, laravel, automated testing, browser testing, end-to-end testing, headless browser, macros, multiple browser testing, test setup, quality assurance]
youtube_url: "https://www.youtube.com/watch?v=2M23skx2TK8"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Laravel Dusk: painless browser automation

## Chapters

- What is Laravel Dusk?
- Set Up Laravel Dusk
- Review Default Test
- Turn Off Headless Browser
- Create Own Test
- Clean Up Code with Macros
- Finish Writing Test
- Two Browser Test
- Other Use Cases for Dusk

## Transcript

**Aaron Francis:** Testing your application in the browser is one of the best ways to ensure that your entire application actually works, not just individual pieces. But historically, it has been very complicated to set up. That is, until now. Laravel Dusk is a first-party package provided by the Laravel team, which gives you a super nice way of interacting with

**Aaron Francis:** a browser, usually headless, but it doesn't have to be headless, and it doesn't require Selenium server or anything like that. It uses Facebook's, uh, Remote WebDriver. And so it's pretty lightweight compared to traditional browser testing, and Dusk is going to automate downloading the right Chrome binary for your system.

**Aaron Francis:** And so what we're gonna do is we're gonna install Dusk here, and then we're gonna generate a test, and then I'm gonna walk you through some of the stuff that you can do with Dusk. Okay, I have a standard Laravel application set up here, and I'm going to require Laravel Dusk, but I am going to pass this dev flag here because I don't want this to end up in my production application. That would be a massive security risk.

**Aaron Francis:** So, um, I am gonna pass dev to make sure that that doesn't happen. And then you'll notice we also get this PHP WebDriver, which is the thing I was talking about earlier from Facebook. Then what we're gonna do is we're gonna run PHP Artisan Dusk install, and we'll see what that does. There's the good stuff right there. Downloading the Chrome driver binaries.

**Aaron Francis:** So it looked, Dusk looked at my system, determined it was a Mac, determined what version of Chrome I have on my Mac, and then downloaded the Chrome driver binaries. So if we were to open ... I think I might already have it open. If we were to go over to PHP Storm here, and we'll check the vendor Laravel Dusk and come down here to the bin.

**Aaron Francis:** We see, there you go. That is the Chrome driver that Laravel installed for me. If you find that this is the wrong one, I would be very surprised, and two, you can install your own, which we're not gonna do right now. What we are gonna do right now is we're gonna look at the test that was created for us, and then we're gonna create our own test, and we'll dive into navigating around the page, copy-pasting, selecting elements, all kinds of good stuff.

**Aaron Francis:** Checking our tests directory, you'll see we have a new directory here named browser, and if we come down here, we'll see example test, and it looks pretty clean. So we call this browse, and we pass through a callable, and we ask for an instance of a browser, and then we get back the f- a, an actual browser, and we can visit the homepage and assert that we see Laravel.

**Aaron Francis:** Honestly, pretty useful test because if your homepage doesn't load, something has gone terribly wrong. So I, I like having this test. Let's run this test. So we'll do PHP Artisan, which I've aliased to AR. We're just gonna run Dusk. And there you go. It opened a browser presumably and checked that.

**Aaron Francis:** Uh, well, that was kinda hard to see. So let's take a look at extends Dusk test case to see if we can turn off the headless browser part. So you'll see up here in the prepare, if we're not running in Sail, which is a Docker, lightweight Docker, it's gonna start the Chrome driver. If you were using Selenium, this is where you would come in and you'd set up your own, um, you'd set up your own Chrome driver or Selenium server or whatever.

**Aaron Francis:** I'm not using that, so we're just gonna keep that there. Then you'll see down here, this is the driver which returns that Remote Rep- WebDriver, which is PHP WebDriver here, and you see it is a Facebook thing. Thanks, Facebook. And down here, headless. Has headless disabled. So has ... We wanna turn headless disabled, uh, on just for this test 'cause I wanna show you that it works.

**Aaron Francis:** So coming in here to our ENV, we'll come all the way down to the bottom. Headless disabled is true, and if we were to run this again ... Oh, there it was. It kinda worked. We, we, we at least saw the browser. So let's come back over here and do the example test. We'll assert, and then we will sleep for, let's sleep for three seconds, and then we can pop back out here and see if we can actually

**Aaron Francis:** see it work. There we go. Loaded Dusk.test. It sees Laravel, and then it closes itself out. That's very cool to me. Honestly, with very little time and very little effort, we are automating a Chrome browser, and we're able to make assertions about it in our test case. Let's go ahead and make our own new test case instead of using the example one, but before we do, let's use a browser to navigate to bentonow.com and scroll down a little bit, and look at that.

**Aaron Francis:** Hey, who is that guy? Bento is the email service provider that I use for everything. If you've ever signed up for anything by me, your email address is in Bento. Thank you for signing up. I love, love, love Bento, primarily because it's an indie maker whose support is incredibly good. When I was launching High Performance SQLite, I accidentally or on purpose ignored the help text in Bento.

**Aaron Francis:** I sent too many emails too quickly, and Gmail put us in the penalty box. I should have listened to Bento, but regardless, even though it was my fault, the founder reached out to me in the DMs and was like, "Hey, you messed up." He didn't say that. "But here's some stuff you can do." He was helping me even though I made a huge mistake, so please go check out Bento. Tell Jesse that Aaron sent you.

**Aaron Francis:** Now, let's get back to writing our own registration test. Let's create our own test case and carry it a little further. So we'll say PHP Artisan, I've aliased it to AR, Dusk make, and we'll do registration test. So- Hopping back here, registration test. You'll see it has filled it out with that stub, which is not exactly what we're after.

**Aaron Francis:** Um, if we were to say herd open and see what we're working with here, you'll see that is what we're working with. We have a registration page, and I kinda wanna, like, I kinda wanna type some stuff. So let's see if we can figure out how to type some stuff. Over here, what was that URL? That URL was /register, which is good.

**Aaron Francis:** You can say /register, and that's probably a good way to do it. Let's see what else is available. Visit route. Route is kinda cool. So you can pass through a Laravel route that you want to visit, and it will determine the URL for you. We're not gonna do that right now. Um, so we're going to visit register, and then let's just do...

**Aaron Francis:** Let's just add this at the bottom so we can see what we're doing as we do it. So we'll sleep for five seconds. We're gonna come out here, and we're going to delete the example test, close this down, and say AR Dusk. So we visit the register page. That looks really good to me. Now let's type some stuff in.

**Aaron Francis:** So let's say browser type, and we need to figure out, um, we need to figure out what this field is. Now, there are a couple different ways you can target a field on the page, or rather any element on the page. One way is you can add a special attribute. You can add a Dusk attribute, and then inside of your Dusk test, you can target it that way.

**Aaron Francis:** We'll look at that in a second. When you're typing, um, by default, Laravel is going to look for an input with that name. Let's try that one, 'cause that seems a little bit more straightforward. If we look in the register.blade and we scroll up here, the name of the name field is name. Say name again. So coming back over here, we can say name is Aaron.

**Aaron Francis:** And let's just run that and see if that works. So popping that open. Oh, that worked, and it worked really quickly. In fact, it worked too quickly. That's not what a human would do. Let's try, uh, type slowly. Amazing, the affordances that Dusk provides you. So looking at this, oh, wow, that looks a lot more realistic.

**Aaron Francis:** Does that matter? I don't know. Is it cool? Yeah. It's super cool. Um, in fact, you can even... I think you can change if you pass through here. The default, uh, milliseconds are one hundred, but if you want to type slowly, you can pass three hundred milliseconds in, and you're good there. Now, so that is one way we can fill a form.

**Aaron Francis:** Let's go ahead and do something else. Well, we don't need to be slow anymore. Let's type, um, let's type email as Aaron at example. You thought I was gonna reveal it. I'm not revealing it this time. And let's see if that works. And boom, that works. Now, I want to try something.

**Aaron Francis:** I don't wanna type my email into the confirm email, uh, box. Honestly, this shouldn't even be there. That confirm email shouldn't even be there. You know what I wanna do? I wanna copy out of the email field, and I wanna paste into the confirm email field, 'cause that's what a user is probably gonna do, and I wanna make sure that that works.

**Aaron Francis:** And we can do that using Dusk. Let me show you. To pull this off, we are gonna go one level deeper. So we're gonna go browser with keyboard, and we're gonna open up this callback, and we're gonna say, "Give me a keyboard." Give me a keyboard where I've got a little bit more control here, and I'm gonna say keyboard type. And in here, I'm first gonna check if we're on a Mac operating system by saying on Mac.

**Aaron Francis:** Thanks. That's very cool that that exists. And if we are on a Mac, I'm gonna say, all right, let's send a Meta key. Otherwise, let's send a Control key. So for all yous Windows users out there, I got you. You're good. For Linux, I don't know. This probably works. I don't really know. Um, all right. After that, we are going to send the A key.

**Aaron Francis:** So we're gonna select everything in the field. Let's just stop here for a second and see if that works. So, hey, look at that. That is pretty cool. All right. Um, that's gonna freak out 'cause... Oh, no, it didn't. I thought because I closed the browser it was gonna complain, but it did not care. Now, we're gonna come up here and say, uh, let's go ahead and copy everything in there.

**Aaron Francis:** So this is going to be our copy function, and it will get a little bit nicer. I'm not gonna lie. It will get a little bit nicer, so stay tuned. Next thing I need to do is I need to click into the email confirmation, uh, field. So I'm gonna say click, but I can't just say email confirmation here, because it's gonna complain that it doesn't know, like, it doesn't know how to find that.

**Aaron Francis:** Unable to locate element. That's fair. Um, let's take a look and see what it's called. It is called confirm email. So I'm gonna take this, and I'm just gonna use a CSS selector. So I'm gonna say input, where the name equals that. This is a place where you could add a Dusk selector, which I'll show you here in a second.

**Aaron Francis:** So let's run this again and see if we get our click. Boom. We're in the right field. Off to a good start. Let's go ahead and kill that and come back over here. If you wanted to, you could say on here, you could say Dusk equals email. I'm gonna change the name a little bit so that, um, we're not confused. So I'm just gonna call this one email2.

**Aaron Francis:** If we come back here, now you have access to email2 using this special @ sign. That is going to look in the, uh... There you go. That's gonna look in the source for anything that has the Dusk attribute set to email2. That can be a really, really nice way, um, especially if your CSS is changing and the targets are all over the place.

**Aaron Francis:** That can be a really robust way to test these elements. That is how Laravel Nova does it. You'll see these dusk attribute selectors littered basically everywhere. So now we're in the right spot, and let's copy this out and come down here. And instead of doing that, we'll just say, why don't you paste it in there? And I think this is gonna work.

**Aaron Francis:** I think this is gonna work first shot. And boom, look at that. So now we have discovered that we have low-level access to the control, and we can pass in any sort of modifier keys or anything like that. And we can do everything that you would normally do in a browser, which copy/paste. So when some product guy comes along and says, "You need to disable paste for security," and somebody else does it, your test is gonna say, "Nah, we don't do that here." We need to make sure that we're able to paste that in because that's how real users work.

**Aaron Francis:** Let's clean it up just a little bit. Like most components in the Laravel ecosystem, the keyboard is macro-able. So we can come over here, and let's, uh, let's say keyboard macro, and we're gonna call this copy all. And we can just paste that guy in there. And in fact, let's go ahead and pluck this one out. We're gonna pluck that one out and make it its own copy.

**Aaron Francis:** So if we pull this out and we come down here and make this copy, then that is copy. We change both of these to this, and we'll return, so we're being good citizens here. So copy all is going to... In fact, that's wrong. We need to then copy.

**Aaron Francis:** So we can return that. Copy all is gonna select everything and then copy, and copy is just going to copy. And for good measure, let's put paste in here as well. And we can now clean all of this up. And in fact, let's make this a short closure because we don't have much to do here anymore.

**Aaron Francis:** We can say, uh, keyboard copy all, and we'll come down here, get rid of you, and say keyboard paste. And with any luck, now it's a little bit cleaner, and it still works, which is... That's, you know, that's what we're after.

**Aaron Francis:** All right. I'm gonna burn through the rest of this test, and then I'm gonna show you a test that I have fully written that opens two browsers at the same time and watches the interactions between the two. It's very cool. But let's finish this login test or this register test first. So just moving along really quickly, what I'm gonna say is let's type into password ASDF, and let's type into the password confirmation field ASDF as well.

**Aaron Francis:** And then let us press the button, which I have given a, a dusk selector on, but I don't remember what I called it. Dusk equals register. So we're gonna push the register button, and then we're gonna assert that we see, uh, eight characters on the page because that is our password rule, must be longer than eight characters.

**Aaron Francis:** So if we run this again, we should see it fill out the entire form. Password field must be at least eight characters, and our test passed because that assertion was there. Now, if I show you this again with eight characters, you'll see that the test failed because we just went ahead and registered.

**Aaron Francis:** It failed asserting that false is true. So our assertion worked. Now, there are a lot of assertions in here. There are, you know, dozens and dozens and dozens of assertions. I've got them all right here. We're not gonna go over it. You can assert things about what is seen in the source. You can assert things about what you see in Vue.js or what route you're on.

**Aaron Francis:** So if you have a successful registration, you do wanna confirm that you have been, uh, redirected to the dashboard. So you can assert the URL. You can assert all kinds of stuff. Um, it's incredibly powerful, and there are way too many to cover. But I wanna show you a super cool test that opens two browsers side by side and clicks on some stuff and watches both of them.

**Aaron Francis:** This is the page that we're going to be opening. This is Laravel Reverb, and if you click here, it does that little heart thing. In fact, if you click here, it does it in every browser that is on the page. And that is what I want to show you. So we're gonna test, we're gonna test somebody else's website.

**Aaron Francis:** We're not even gonna test our own deal. This is the test. So we have a Reverb test, and let me walk you through it. You can open two browsers. You can open as many browsers as you want just by changing this function signature. So here I've opened two different browsers because we're gonna open them and put them side by side. Um, just for giggles, I have resized and moved the browsers around so we can see both of them.

**Aaron Francis:** Normally, you're testing in a headless environment, so you're not gonna do this. But, hey, it makes better video to see the stuff happen. Then for both, we're gonna visit Reverb. We're gonna wait for the thing to show up in the browser, and then we're gonna scroll that into view. Then we're gonna sleep for one second, and then we're just gonna run this arbitrary script.

**Aaron Francis:** We're gonna run this script and assert that the value is five, meaning that is our starting point, and then we're gonna do a bunch of stuff. We're gonna sleep, and then we're gonna click on the heart a bunch of times. So we're gonna sleep, click, sleep, click, sleep, click. And then, uh, down finally at the bottom, notice all this is happening on browser one.

**Aaron Francis:** So browser one is the one doing all the clicks. And then in browser two, we're gonna say, "Hey, let's run that again, and let's just assert that it's greater than five." Let's just make sure that hearts are showing up on both sides. Hopefully this works. All right, coming over here, we can clear that out, and instead of filter register, we're gonna filter on reverb.

**Aaron Francis:** So here we go. Two browsers side by side, scrolling down. Click on the left, shows up on the right. Boom. There we go. That was awesome. That took me 45 minutes to do. We're gonna watch it again. Let's run it again. Opens two browsers, boom. One on the left, one on the right, reverb, reverb, scroll. Click on the left, shows up on the right, and our assertions passed.

**Aaron Francis:** It looks like our WebSockets are working. Stuff that happens in one browser is being sent to another browser. How cool is that? We have barely scratched the surface of Laravel Dusk. You can take screenshots and store them. You can rerun failures only. You can create page components, and so if you have a page where you're doing stuff often, you can create a component out of that that contains its own selectors and its own assertions.

**Aaron Francis:** You can brute force log in using ID without having to go through the browser at all. There's a really special way that Dusk lets you authenticate that user. You can have separate environment files just for your Dusk tests. There's so much more that you can do. What are the reasons that you might not use Dusk?

**Aaron Francis:** Well, it can be kind of finicky in CI to get Chrome up and running. If you're going to do that, I would recommend maybe looking at ChipperCI. They have, um, they are built specifically for Laravel, and Dusk is just turnkey. You don't have to do anything. Um, it should work in CI, but headless browsing is kind of finicky.

**Aaron Francis:** You might not be doing browser tests. That would be a pretty good reason not to use Dusk. I would encourage you to do browser tests. It's gonna test the entirety of the system and not just the components in isolation, so I find that pretty powerful. Um, but you might not be using it, and that's fine. There is a way to use Dusk to just do scraping.

**Aaron Francis:** You don't have to test. You can actually just scrape. I'm gonna make a lot more videos about that, 'cause I've been wasting a lot of time doing that. If you wanna see more What Is Laravel videos, there's a whole playlist. I will pin one here about Laravel Octane, and until the next time, see ya.

---

## Related on aaronfrancis.com

- [Testing](/podcast/testing-699b73b6) *(Podcast · 2023-01-17 · shared: testing, laravel dusk, browser automation)*
- [Laravel's higher order collection proxies](/writing/laravels-higher-order-collection-proxies) *(Article · 2024-09-01 · shared: laravel dusk, browser automation)*
- [Developer to Business Owner](/podcast/developer-to-business-owner-3adb2856) *(Podcast · 2024-08-06 · shared: laravel, browser automation)*
- [Back in the Saddle](/podcast/back-in-the-saddle-9836e11b) *(Podcast · 2021-10-22 · shared: laravel, testing)*
- [How I hacked Laravel to ignore missing route bindings](/videos/how-i-hacked-laravel-to-ignore-missing-route-bindings) *(Video · 2025-07-30 · shared: laravel, macros)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `laravel dusk` ([K-L](/llms/keywords/k-l.txt)), `browser automation` ([A-B](/llms/keywords/a-b.txt)), `testing` ([T-Z](/llms/keywords/t-z.txt)), `end-to-end testing` ([D-E](/llms/keywords/d-e.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
