Why Laravel Dusk Tests Fail with Chrome Headless
In this article
Laravel Dusk tests fail when Chrome headless is enabled primarily because the headless browser runs in a different environment than a regular browser, lacking certain display, GPU, and user-agent features that your application code or Dusk's default configuration depends on. The most common culprits are missing ChromeDriver flags, sandbox restrictions in containerized environments, and timing issues caused by headless mode's faster or slower rendering.
What Chrome Headless Actually Changes
Chrome's headless mode (the --headless flag) strips away the graphical user interface, but it also alters several underlying behaviors that can break Dusk tests:
- User agent string – Headless Chrome appends
HeadlessChrometo the user agent, which can trigger different code paths in your app (e.g., feature detection or bot protection). - GPU and rendering – Without a GPU, some CSS animations and transitions behave differently, causing elements to be in unexpected positions or states when Dusk tries to interact with them.
- Window size and viewport – The default headless viewport is 800x600, while non-headless may default to a larger size. Responsive layouts can hide or move elements, leading to "element not visible" errors.
- Network and resource loading – Headless mode may load resources differently, especially if your site relies on lazy loading or IntersectionObserver, which can trigger only when elements are scrolled into view.
How Dusk Launches Chrome
Dusk uses ChromeDriver, which is a separate executable that communicates with Chrome via the WebDriver protocol. When you run php artisan dusk, Dusk starts ChromeDriver with a default set of options. The --headless flag is added automatically when you set DUSK_HEADLESS_DISABLE to false (the default in most setups) or when you use the --headless option in the dusk:chrome command. The actual command line looks something like:
chromedriver --port=9515 --url-base=/wd/hub
Then Dusk sends capabilities to ChromeDriver, which launches Chrome with flags like --headless, --no-sandbox (if you've added it), and --disable-gpu. If any of these flags conflict with your environment, tests fail.
Common Failure Modes and Their Causes
1. "Chrome failed to start: exited abnormally"
This is often caused by running as root in a Docker container without the --no-sandbox flag. Chrome's sandbox requires certain kernel capabilities that are disabled in many containers. Dusk does not add this flag by default, so you must add it manually in your DuskTestCase.php or in a custom ChromeOptions.
2. Element not found or not visible
Headless Chrome may not trigger lazy-loading or may have a different viewport, causing elements to be outside the visible area. Dusk's waitFor methods can help, but if the element is never loaded because the page's JavaScript relies on requestAnimationFrame or scroll events, tests will time out.
3. Timeouts on slow operations
Headless mode can be faster because it skips rendering, but it can also be slower for JavaScript-heavy pages because the CPU is used for both rendering and logic without GPU acceleration. Dusk's default timeout is 5 seconds, which may be too short.
4. Stale element references
When a page re-renders (e.g., after an AJAX call), the element reference becomes invalid. This is more likely in headless because the browser may garbage-collect or reflow differently. Using refresh() or re-querying elements can help.
How to Fix Headless-Specific Failures
Here are the most effective solutions, ordered by frequency of use:
- Add the no-sandbox flag – In your
DuskTestCase.php, modify thedriver()method to include--no-sandboxin the ChromeOptions. This is essential for CI environments and Docker. - Set a consistent window size – Use
$browser->resize(1920, 1080)in your test setup to ensure the viewport matches what you expect. - Disable headless for debugging – Temporarily set
DUSK_HEADLESS_DISABLE=trueto see the browser in action. This often reveals that the issue is not headless-specific but a timing or selector problem. - Increase timeouts – Use
$browser->waitFor('.selector', 10)or set a longer default timeout in theDuskTestCase. - Add the disable-dev-shm-usage flag – In Docker,
/dev/shmis often too small, causing Chrome to crash. Add--disable-dev-shm-usageto the ChromeOptions.
Comparing Headless vs. Headful in Dusk
Headless mode is generally faster and requires no display, making it ideal for CI. However, it can hide issues that only appear with a real GPU or user interaction. Headful mode (with DUSK_HEADLESS_DISABLE=true) is slower but more reliable for debugging. Many teams run headless in CI and headful locally when writing tests. There is also --headless=new (Chrome 112+), which uses the new headless mode that is closer to headful behavior, but it still has a different user agent and may not fix all issues.
Best Practices for Reliable Dusk Tests
- Always use explicit waits (
waitFor,waitForText) instead of implicit sleeps. - Use data attributes (e.g.,
data-testid) for selectors to avoid CSS class churn. - Run tests in a clean database state using
RefreshDatabase. - Keep your Chrome and ChromeDriver versions in sync. Mismatched versions are a leading cause of failures.
- If using CI, cache the ChromeDriver binary to avoid download issues.
Frequently Asked Questions
Why does Dusk work locally but fail in CI with headless?
CI environments often run as root, have limited shared memory, and lack a display. The --no-sandbox and --disable-dev-shm-usage flags are usually required. Also, CI may have a different Chrome version than your local machine, so ensure you're using the same ChromeDriver version.
Does using --headless=new fix all issues?
No. The new headless mode (Chrome 112+) is closer to headful, but it still has a different user agent and may not support all extensions. Some issues, like sandboxing, remain. It's worth trying, but don't expect a silver bullet.
How do I add custom Chrome options in Dusk?
Override the driver() method in your DuskTestCase and use ChromeOptions::addArguments(). For example: $options->addArguments(['--headless', '--no-sandbox', '--disable-dev-shm-usage']);.
Why does my test pass in headful but fail in headless?
This is usually due to viewport size, user agent, or GPU-related rendering differences. Check if your page uses browser detection or media queries. Also, headless may not trigger hover states or focus events the same way.
Should I use Dusk's --headless option or set it manually?
Dusk's --headless option (via php artisan dusk --headless) is a shortcut that adds the flag for you. However, it doesn't add the no-sandbox or other flags. Manual configuration in DuskTestCase gives you more control and is recommended for CI.
Summary
Headless Chrome is not a drop-in replacement for a real browser. The failures you see are almost always environmental (sandbox, viewport) or timing-related. By understanding what headless changes, adding the right flags, and using robust wait strategies, you can make Dusk tests pass reliably in headless mode. If you still see failures, run with DUSK_HEADLESS_DISABLE=true to compare and isolate the cause.