跳至内容 跳至搜索

系统测试的屏幕截图助手。

方法
T

实例公共方法

take_failed_screenshot()

如果测试失败,则截取浏览器中当前页面的屏幕截图。

take_failed_screenshot 在系统测试拆卸期间被调用。

# File actionpack/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
  metadata[:failure_screenshot_path] = relative_image_path if Minitest::Runnable.method_defined?(:metadata)
end

take_screenshot(html: false, screenshot: nil)

截取浏览器中当前页面的屏幕截图。

take_screenshot 可在系统测试中的任何时间点使用,以截取当前状态的屏幕截图。这对于调试或自动化视觉测试非常有用。您可以在每个测试中截取多个屏幕截图,以在测试的不同阶段调查更改。这些屏幕截图将命名为顺序前缀(或失败测试的“failed”)。

默认的屏幕截图目录是 tmp/screenshots,但您可以使用 Capybara.save_path 设置一个不同的目录。

您可以使用 html 参数或设置 RAILS_SYSTEM_TESTING_SCREENSHOT_HTML 环境变量来保存正在截屏的页面的 HTML,以便您可以检查截屏时页面上的元素。

您可以使用 screenshot 参数或设置 RAILS_SYSTEM_TESTING_SCREENSHOT 环境变量来控制输出。可能的值是: simple (默认) :只显示屏幕截图路径。这是默认值。

`inline`

:使用 iTerm 图像协议 (iterm2.com/documentation-images.html) 在终端中显示屏幕截图。

`artifact`

:使用终端伪像格式 (buildkite.github.io/terminal-to-html/inline-images/) 在终端中显示屏幕截图。

# File actionpack/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