【问题标题】:Custom screeshot for watir-rspec reporterwatir-rspec 记者的自定义截图
【发布时间】:2015-05-13 04:01:18
【问题描述】:

我正在从带有 rspec 的 watir-webdriver 迁移到 watir-rspec(变化不大)。但现在我想利用记者将我拍摄的截图与截图宝石链接起来。我正在为如何做到这一点而苦苦挣扎,我不想使用标准屏幕截图,因为我有另一个辅助函数可以对图像进行一些处理,并且屏幕截图 gem 允许我获取特定元素的屏幕截图。

在 watir-rspec 文档中,它声称我们只需要添加这三行,但我不确定在哪里以及如何更改它以适应我的自定义图像生成。

uploaded_file_path = Watir::RSpec.file_path("uploaded.txt")
File.open(uploaded_file_path, "w") {|file| file.write "Generated File Input"}
file_field(:name => "upload-file").set uploaded_file_path

【问题讨论】:

  • 你是一直想添加截图还是只在测试失败时添加?
  • 我希望能够将我自己的屏幕截图从另一个文件夹添加到测试用例中。我猜,只要测试失败就足够了。
  • 文件是否必须在另一个文件夹中? Watir-RSpec 似乎希望将图像保存在结果文件夹中。
  • 对于组织来说会更好,是的。但为了让它工作,我会在同一个文件夹上测试,有什么想法吗?我正在查看 HtmlFormatter 代码,我想我仍然需要重写一些方法才能使其工作。

标签: rspec watir watir-webdriver


【解决方案1】:

向报告添加文件链接是通过使用Watir::RSpec.file_path 方法完成的。基本上你:

  1. 调用file_path 方法,该方法告诉报告添加链接并返回预期文件的路径。
  2. 使用file_path返回的路径创建文件,在本例中为屏幕截图。

在以下示例中,After 挂钩显示了如何使用 file_path 方法添加链接:

require_relative "spec_helper"

describe "Google" do
  before { goto "http://google.com" }

  it "allows to search" do
    text_field(:name => "q").set "watir"
    button(:id => "gbqfb").click # This will fail to locate the element
    results = div(:id => "ires")
    results.should be_present.within(2)
    results.lis(:class => "g").map(&:text).should be_any { |text| text =~ /watir/ }
    results.should be_present.during(1)
  end

  after do
    # Call Watir::RSpec.file_path to:
    #  1. Tell the report to add a link
    #  2. Determine the file path/name the report will link to
    screenshot_file_path = Watir::RSpec.file_path("custom_screenshot.jpg")
    #=> "C:/Scripts/Misc/Programming/watir-rspec/spec/tmp/spec-results/custom_screenshot_104027_1_1.jpg"

    # Create the screenshot to the path specified in screenshot_file_path
    # This would be dependent on your screenshot gem
  end
end

有几个限制:

  1. 链接的图像应位于结果文件夹中。
  2. 链接的图像应具有特定的生成名称。
  3. 虽然您始终可以创建屏幕截图,但只有在测试失败时才会链接到报告。
  4. 默认的截图链接也会存在。

【讨论】:

    【解决方案2】:

    感谢@JustinKo 的提示,我设法通过覆盖 HtmlFormatter 类的 extra_failure_content(exception) 方法来修复它。我用自定义调用替换了 save_screenshot 调用来获取我自己的屏幕截图,效果很好。我将它们保存在同一目录中以使其更容易。

    这里是 HtmlFormatter 原始代码的链接。 https://github.com/watir/watir-rspec/blob/master/lib/watir/rspec/html_formatter.rb

    class MyHtmlFormatter < Watir::RSpec::HtmlFormatter
     def my_custom_function(description)
      file_name = file_path("your_file.png", description)
      old = File.absolute_path("your_file.png") 
      File.rename(old, file_name)
      file_name
     end
    
     def extra_failure_content(exception)
        browser = example_group.before_all_ivars[:@browser] || $browser
        return super unless browser && browser.exists?
        my_custom_function args
        save_html browser
        (...)
     end
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2016-01-25
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多