Taking Screenshots In Selenium Using Ruby

I LOVE being able to take screenshots of my Selenium tests as they are running!

Here’s how I take screenshots of my selenium tests running, written in Ruby:

  • Find the variable referencing the selenium driver. Might look something like this: <pre lang="ruby">@selenium_driver = Selenium::Client::Driver.new(…..</pre>

  • Wait for condition to take screenshot. Example: <pre lang="ruby">page.wait_for_element(“//h1”, {:timeout_in_seconds => 10})</pre>

  • Take screenshot. Example: <pre lang="ruby">@selenium_driver.capture_entire_page_screenshot(File.expand_path(File.dirname(FILE)) + ‘screenshot1.png’, ‘’)</pre>

    or something simpler like:

    @selenium_driver.capture_entire_page_screenshot('~/Desktop/screenshot1.png', '')

Selenium documentation for using the capture screenshot function.