跳至内容 跳至搜索

系统测试的屏幕截图帮助器。

方法
T

实例公共方法

take_failed_screenshot()

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

take_failed_screenshot 在系统测试清理期间调用。

# File actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb, line 44
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 环境变量来控制输出。可能的值为

# File actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb, line 31
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