Selenium Screenshots in GitHub Actions with Headless Chrome
In this article
This guide shows you how to capture Selenium screenshots inside a GitHub Actions workflow using headless Chrome. You'll end up with a CI job that runs your Selenium tests, takes screenshots on demand (or on failure), and uploads them as artifacts you can download and inspect. This is especially useful for visual regression checks, debugging flaky tests, or documenting UI states automatically.
What You'll Need
- A GitHub repository with a Selenium test project (Python, Java, JavaScript, etc.)
- A GitHub Actions workflow file (e.g.,
.github/workflows/screenshots.yml) - ChromeDriver version matching the Chrome browser version used in the runner
- Basic familiarity with your Selenium setup and how to take screenshots in your language
Step-by-Step
- Set up your Selenium test to accept a headless option. In your test code, configure ChromeOptions to run headless. For example, in Python:
options.add_argument('--headless'). In Java:options.addArguments("--headless"). Also add--no-sandboxand--disable-dev-shm-usageto avoid common issues in containerized environments. - Add screenshot capture logic. Use Selenium's built-in screenshot method. In Python:
driver.save_screenshot('screenshot.png'). In Java:File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);then copy it to a desired location. Save screenshots to a dedicated folder likescreenshots/. - Create a GitHub Actions workflow file. In your repo, create
.github/workflows/screenshots.yml. Start with a basic workflow that triggers on push or pull request. - Specify the runner and system dependencies. Use
ubuntu-latest. You'll need to install Chrome and ChromeDriver. The GitHub-hosted runner already has Chrome installed, but it may be an older version. To ensure compatibility, you can use a setup action likeactions/setup-chromedriveror manually download matching versions. - Install your project dependencies. For Python, use
pip install -r requirements.txt. For Node.js, usenpm install. For Java, use Maven or Gradle. - Run your Selenium tests. Execute your test command, e.g.,
pytest,mvn test, ornpm test. Ensure your tests are written to capture screenshots at the right moments (e.g., after page load, on assertion failure). - Upload screenshots as artifacts. After tests run, use the
actions/upload-artifactaction to upload thescreenshots/folder. Even if tests fail, you want the artifacts, so useif: always(). - Commit and push. Push your workflow file and test code changes to GitHub. The workflow will trigger, and you can view the artifacts in the Actions tab.
Common Mistakes
- Forgetting headless mode: Without
--headless, Chrome tries to open a GUI and fails on the runner. - ChromeDriver version mismatch: If Chrome updates but ChromeDriver doesn't, Selenium throws a session error. Always pin versions or use a setup action that handles it.
- Not adding
--no-sandbox: The runner runs as root, and Chrome's sandbox can cause crashes. Add this flag to avoid it. - Ignoring the
--disable-dev-shm-usageflag: The shared memory space on the runner is small; this flag prevents Chrome from crashing. - Not using
if: always()on the upload step: If tests fail, the workflow stops and artifacts are not uploaded, so you lose the screenshots. - Saving screenshots to a temporary directory: If you save to
/tmp, the artifact upload may not pick it up. Use a folder inside your workspace.
Tips
- Use a setup action for ChromeDriver: For example,
actions/setup-chromedrivercan download a matching version automatically. - Set a viewport size: Use
--window-size=1920,1080to get consistent screenshots. - Take screenshots on failure: In your test code, wrap the screenshot call in a try/except or use a hook that triggers on test failure.
- Name screenshots meaningfully: Include the test name and timestamp to avoid overwriting.
- Compress screenshots: If you have many, consider zipping them before uploading to save space.
- Use a caching strategy: Cache your dependencies (e.g., pip, npm) to speed up the workflow.
Troubleshooting
Workflow fails with "Chrome failed to start: exited abnormally." This usually means the --no-sandbox flag is missing. Add it to your ChromeOptions.
SessionNotCreatedException: This version of ChromeDriver only supports Chrome version X. Update your ChromeDriver to match the Chrome version on the runner. Use a setup action or download the exact version.
Screenshots are blank or black. This can happen if you take the screenshot before the page finishes loading. Use explicit waits (e.g., WebDriverWait) to ensure elements are visible.
Artifacts not showing up. Check the workflow logs. Make sure the upload step ran and the path is correct. Also verify that the screenshots folder exists and contains files.
Tests pass locally but fail in CI. The headless environment differs. Add more robust waits and consider using a fixed window size. Also, ensure your test doesn't rely on a specific screen resolution.
Frequently Asked Questions
Do I need to install Chrome on the GitHub runner?
No, Chrome is pre-installed on ubuntu-latest. However, you may need to install ChromeDriver separately, as it's not always present.
How do I take a screenshot only when a test fails?
In your test framework, use a hook like pytest_runtest_makereport (for pytest) or @After (for JUnit) to check if the test failed, then call the screenshot method.
Can I upload screenshots as part of a pull request comment?
Yes, you can use a GitHub Action like actions/github-script to post a comment with the screenshot images, but it requires additional setup and the images must be publicly accessible (e.g., via a temporary URL).
What if my project uses a different browser, like Firefox?
This guide focuses on Chrome, but the same principles apply. You would install geckodriver and use FirefoxOptions with --headless.
Can I run Selenium tests in a Docker container within GitHub Actions?
Yes, you can use a service container like selenium/standalone-chrome, but you'll need to configure networking. The direct approach described here is simpler.
How do I store screenshots for later comparison?
You can upload them as artifacts, or use a storage service like AWS S3. For visual regression testing, consider using a dedicated tool like Percy or Applitools.