Playwright tests are failing in GitHub Actions because the xvfb service, which provides a virtual framebuffer for GUI applications in headless environments, is not running or not accessible by Playwright.
Common Causes and Fixes
-
xvfbService Not Started or Crashed-
Diagnosis: Check the GitHub Actions logs for messages indicating
xvfbfailed to start or exited with an error. You can also try to manually startxvfbin a similar environment to see its output. -
Fix: Ensure
xvfbis properly installed and started as a service in your GitHub Actions workflow. A common way to do this is by using a dedicated GitHub Action forxvfb.- name: Start xvfb uses: cypress-io/github-action@v4 # This action can also start xvfb with: # ... other options xvfb: trueThis works because the
cypress-io/github-actionimplicitly handles the installation and startup ofxvfbon the runner, making it available for Playwright. -
Diagnosis: If you’re not using a dedicated action, check your
runsteps. You might have a command likexvfb-run --auto-servernum --server-num=99 ...but thexvfbserver itself wasn’t initiated. -
Fix: Explicitly start
xvfbbefore running your tests.# In your workflow file's run step sudo apt-get update && sudo apt-get install -y xvfb Xvfb :99 -screen 0 1024x768x24 & export DISPLAY=:99 # Now run your Playwright tests npx playwright testThis explicitly installs
xvfb, starts a server on display 99, and sets theDISPLAYenvironment variable so Playwright can connect to it.
-
-
Incorrect
DISPLAYEnvironment Variable-
Diagnosis: Playwright needs to know which X server to connect to. If the
DISPLAYenvironment variable is not set or points to the wrong server, Playwright won’t be able to render its browser. Check your workflow logs for errors related to "cannot open display" or "no X server found." -
Fix: Ensure the
DISPLAYenvironment variable is correctly set to the runningxvfbinstance.- name: Run Playwright tests run: | echo "DISPLAY=:99" >> $GITHUB_ENV # Or use your xvfb server number npx playwright testSetting
DISPLAYin$GITHUB_ENVmakes the variable available to all subsequent steps in the job, ensuring Playwright can find the X server.
-
-
xvfbNot Installed on the Runner-
Diagnosis: The GitHub Actions runner might not have
xvfbinstalled by default, especially if you’re using a custom Docker image or a minimal runner environment. Look for "command not found: xvfb" or similar errors. -
Fix: Install
xvfbas part of your workflow setup.- name: Install dependencies run: | sudo apt-get update sudo apt-get install -y xvfbThis ensures that the
xvfbexecutable is present on the runner’s filesystem before you attempt to use it.
-
-
Permissions Issues with
xvfb-
Diagnosis: In some environments,
xvfbmight fail to start due to insufficient permissions, especially if it’s not run withsudoor if the user context doesn’t allow it. -
Fix: Run
xvfbcommands withsudoor ensure the user executing the tests has the necessary privileges.# In your workflow file's run step sudo xvfb-run --auto-servernum --server-num=99 -- npm run test:e2eUsing
sudograntsxvfb-runthe elevated privileges it might need to create and manage the virtual display server.
-
-
Port Conflicts or
xvfbServer Already Running-
Diagnosis: If multiple processes try to use the same
xvfbserver number, or if a previousxvfbinstance didn’t shut down cleanly, you might encounter "Address already in use" errors. -
Fix: Use
xvfb-run --auto-servernumwhich dynamically assigns an available server number, or explicitly kill any lingeringxvfbprocesses before starting a new one.# In your workflow file's run step pkill -f Xvfb || true # Kill any existing Xvfb processes, ignore errors if none exist xvfb-run --auto-servernum -- npm run test:e2eThis
pkillcommand ensures a clean slate by terminating anyXvfbprocesses that might be holding onto a port, thenxvfb-run --auto-servernumfinds a free display to use.
-
-
Playwright Browser Binary Issues
-
Diagnosis: While less directly related to
xvfb, if the Playwright browser binaries themselves are corrupted or not downloaded correctly, it can manifest as test failures that seem like display issues. Check Playwright’s own logs for errors during browser launch. -
Fix: Ensure Playwright browsers are installed correctly within your CI environment.
- name: Install Playwright browsers run: npx playwright installThis command downloads the necessary browser binaries (Chromium, Firefox, WebKit) for Playwright to use, ensuring they are available on the runner.
-
The next error you’ll likely hit after resolving xvfb issues is related to network connectivity or timeouts if your tests are trying to access external resources that aren’t available in the CI environment.