Capture Full Page Chrome Screenshots with Cucumber
In this article
Capturing full-page screenshots in Chrome during automated Cucumber tests allows you to visually verify the entire rendered content of a web page, even content that requires scrolling. This is especially useful for regression testing, documentation, or debugging layout issues across different environments. By integrating Chrome's headless or headed browser capabilities with Cucumber's step definitions, you can programmatically trigger and save full-page screenshots after key interactions. This guide walks you through setting up and executing full-page screenshots using Cucumber and Chrome in a Ruby-based test environment.
What You'll Need
- Ruby installed (version 2.7 or higher recommended)
- Chrome browser (latest stable version)
- ChromeDriver matching your Chrome version
- Cucumber gem (installed via Bundler or gem command)
- Webdrivers gem (automatically manages ChromeDriver)
- Selenium WebDriver gem
- A basic Cucumber project structure (.feature files, step definitions)
- Basic knowledge of Ruby and Cucumber step definitions
Step-by-Step
- Set up a new Ruby project or navigate to your existing Cucumber project. Create a
Gemfileif one doesn’t exist, and add the required gems:
Then rungem 'cucumber' gem 'selenium-webdriver' gem 'webdrivers'bundle installto install dependencies. - Create or edit your Cucumber
env.rbfile located in thefeatures/supportdirectory. This file configures the browser setup before tests run. - Inside
env.rb, configure Selenium to use Chrome with full-page screenshot capability. Use the following code:
This sets up Chrome in headless mode with a fixed window size. The headless option ensures screenshots are consistent across environments.require 'selenium-webdriver' require 'webdrivers' Capybara.register_driver :chrome do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') options.add_argument('--window-size=1920,1080') options.add_argument('--headless') # Remove if you want to see the browser Capybara::Selenium::Driver.new( app, browser: :chrome, options: options ) end Capybara.default_driver = :chrome - In your step definitions file (e.g.,
screenshot_steps.rb), define a step that triggers a full-page screenshot. Add:
This step saves a PNG screenshot of the entire page, including content below the fold.Then('I take a full page screenshot') doEnsure the driver supports full-page screenshots
driver = page.driver.browserTake a full-page screenshot
screenshot_path = "screenshots/full_page_#{Time.now.strftime('%Y%m%d_%H%M%S')}.png" FileUtils.mkdir_p('screenshots') unless File.directory?('screenshots') driver.save_screenshot(screenshot_path)Optional: Embed in Cucumber report
attach(screenshot_path, 'image/png') end - Create a feature file (e.g.,
screenshot.feature) with a scenario that uses this step:
Ensure you have a step definition for navigating to the homepage.Feature: Full Page Screenshot Scenario: Capture full page after loading Given I am on the homepage Then I take a full page screenshot - Run the Cucumber test using the command
cucumberin your terminal. After execution, check thescreenshotsfolder for the generated image. - To verify the screenshot includes all content, open it and confirm it captures from top to bottom without cropping. If content is missing, ensure the page has fully loaded before the screenshot step.
- Optionally, add a wait step before the screenshot to ensure dynamic content (like AJAX-loaded elements) has rendered:
Use this in your scenario before taking the screenshot if needed.And('I wait for {int} seconds') do |seconds| sleep(seconds) end
Common Mistakes
- Forgetting to install ChromeDriver manually — rely on the
webdriversgem instead. - Not waiting for page load — dynamic content may not appear in the screenshot.
- Using
take_screenshotinstead ofsave_screenshot— the latter is required for full-page capture in Chrome. - Running in a small window size — even in headless mode, window size affects layout.
- Overlooking file permissions — ensure the output directory is writable.
Tips
- Name screenshots with timestamps to avoid overwrites during multiple test runs.
- Use
page.current_urlin the filename to associate screenshots with specific pages. - Remove
--headlesstemporarily to debug visual issues during test development. - Automatically clean old screenshots with a script before each test suite run.
- Integrate with CI/CD pipelines by uploading screenshots to artifact storage for review.
Troubleshooting
If screenshots are not captured, first verify Chrome and ChromeDriver versions are compatible. Run chromedriver --version and match it to your Chrome browser version. If the browser fails to launch, check for missing dependencies (like libxcb on Linux). If screenshots are blank or cut off, increase the implicit wait time or use explicit waits for content. If the screenshots folder isn’t created, ensure the directory path logic includes FileUtils.mkdir_p. If Cucumber throws undefined step errors, confirm the step definition file is loaded (usually via features/support or explicit require in env.rb).
Frequently Asked Questions
Does this work in headless Chrome?
Yes, Chrome’s headless mode supports full-page screenshots usingsave_screenshot.
Can I capture only a specific element?
Yes, useelement.save_screenshot('path.png') instead of the driver method.
Why is my screenshot missing bottom content?
Ensure the page has fully loaded. Add a wait step or scroll to the bottom before capturing.How do I make screenshots part of Cucumber reports?
Useattach in step definitions to embed images in HTML or JSON reports.
Do I need to manually manage ChromeDriver?
No, thewebdrivers gem automatically downloads and updates ChromeDriver.