Module: ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper

Included in:
ActionDispatch::SystemTestCase
Defined in:
lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb

Overview

Screenshot helper for system testing.

Instance Method Summary collapse

Instance Method Details

#take_failed_screenshotObject

Takes a screenshot of the current page in the browser if the test failed.

‘take_failed_screenshot` is called during system test teardown.



53
54
55
56
57
58
# File 'lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb', line 53

def take_failed_screenshot
  return unless failed? && supports_screenshot? && Capybara::Session.instance_created?

  take_screenshot
  [:failure_screenshot_path] = relative_image_path if Minitest::Runnable.method_defined?(:metadata)
end

#take_screenshot(html: false, screenshot: nil) ⇒ Object

Takes a screenshot of the current page in the browser.

‘take_screenshot` can be used at any point in your system tests to take a screenshot of the current state. This can be useful for debugging or automating visual testing. You can take multiple screenshots per test to investigate changes at different points during your test. These will be named with a sequential prefix (or ’failed’ for failing tests)

The default screenshots directory is ‘tmp/screenshots` but you can set a different one with `Capybara.save_path`

You can use the ‘html` argument or set the `RAILS_SYSTEM_TESTING_SCREENSHOT_HTML` environment variable to save the HTML from the page that is being screenshotted so you can investigate the elements on the page at the time of the screenshot

You can use the ‘screenshot` argument or set the `RAILS_SYSTEM_TESTING_SCREENSHOT` environment variable to control the output. Possible values are:

`simple` (default)

: Only displays the screenshot path. This is the default value.

`inline`

: Display the screenshot in the terminal using the iTerm image protocol

    (https://iterm2.com/documentation-images.html).

`artifact`

: Display the screenshot in the terminal, using the terminal artifact

format (https://buildkite.github.io/terminal-to-html/inline-images/).


41
42
43
44
45
46
47
48
# File 'lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb', line 41

def take_screenshot(html: false, screenshot: nil)
  showing_html = html || html_from_env?

  increment_unique
  save_html if showing_html
  save_image
  show display_image(html: showing_html, screenshot_output: screenshot)
end