User Agents

Web browsers and everyday web tech, explained

Capture Full Page Chrome Screenshots with Cucumber

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Ensure the driver supports full-page screenshots
  4. Take a full-page screenshot
  5. Optional: Embed in Cucumber report
  6. Common Mistakes
  7. Tips
  8. Troubleshooting
  9. Frequently Asked Questions

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

  1. Set up a new Ruby project or navigate to your existing Cucumber project. Create a Gemfile if one doesn’t exist, and add the required gems:
    gem 'cucumber'
    gem 'selenium-webdriver'
    gem 'webdrivers'
    Then run bundle install to install dependencies.
  2. Create or edit your Cucumber env.rb file located in the features/support directory. This file configures the browser setup before tests run.
  3. Inside env.rb, configure Selenium to use Chrome with full-page screenshot capability. Use the following code:
    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
    This sets up Chrome in headless mode with a fixed window size. The headless option ensures screenshots are consistent across environments.
  4. In your step definitions file (e.g., screenshot_steps.rb), define a step that triggers a full-page screenshot. Add:
    Then('I take a full page screenshot') do
    

    Ensure the driver supports full-page screenshots

    driver = page.driver.browser

    Take 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
    This step saves a PNG screenshot of the entire page, including content below the fold.
  5. Create a feature file (e.g., screenshot.feature) with a scenario that uses this step:
    Feature: Full Page Screenshot
      Scenario: Capture full page after loading
        Given I am on the homepage
        Then I take a full page screenshot
    Ensure you have a step definition for navigating to the homepage.
  6. Run the Cucumber test using the command cucumber in your terminal. After execution, check the screenshots folder for the generated image.
  7. 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.
  8. Optionally, add a wait step before the screenshot to ensure dynamic content (like AJAX-loaded elements) has rendered:
    And('I wait for {int} seconds') do |seconds|
      sleep(seconds)
    end
    Use this in your scenario before taking the screenshot if needed.

Common Mistakes

  • Forgetting to install ChromeDriver manually — rely on the webdrivers gem instead.
  • Not waiting for page load — dynamic content may not appear in the screenshot.
  • Using take_screenshot instead of save_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_url in the filename to associate screenshots with specific pages.
  • Remove --headless temporarily 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 using save_screenshot.

Can I capture only a specific element?

Yes, use element.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?

Use attach in step definitions to embed images in HTML or JSON reports.

Do I need to manually manage ChromeDriver?

No, the webdrivers gem automatically downloads and updates ChromeDriver.

Can I run this on Linux servers without GUI?

Yes, headless Chrome works perfectly in server environments like CI/CD pipelines.