In this episode, we dive into improving the reliability of our tests by introducing custom Dusk selectors. First, we take a look at why tests can become fragile—like when a button label or a bit of CSS changes, your entire test might break. Not ideal! So, we address this by not tying our tests to UI text or styling that could easily change over time.
We start by revisiting our register test and make sure all our input elements (like name, email, password, etc.) have proper name
attributes. This lets us target them directly, instead of relying on IDs or classes, which can be less stable.
Next, we focus on that pesky register button, which can be a source of problems if its label ever changes. Here, we introduce the idea of Dusk selectors—special attributes like dusk="register-button"
that you add to your HTML. This is a really clean way to select elements in your tests, no matter what changes in the UI.
We talk through the pros and cons of this method. Sure, you're technically adding a testing-only attribute to your code, but it massively improves the stability and clarity of your tests. You'll see how this makes your tests easier to write and maintain, and stops minor UI tweaks from breaking everything.
By the end of the episode, you've learned how to use custom Dusk selectors to target elements in your tests in a way that's both reliable and easy to manage, giving you more confidence in your test suite as your app evolves.