为watir rspec reporter定制的screeshot

为watir rspec reporter定制的screeshot,rspec,watir,watir-webdriver,Rspec,Watir,Watir Webdriver,我正在从带有rspec的watirwebdriver迁移到watirrspec(不是很大的变化)。但是现在我想利用记者把我拍的截图和截图联系起来。我正在努力解决这个问题,我不想使用标准截图,因为我有另一个助手功能,可以对图像进行一些处理,截图gem允许我获取特定元素的截图 在watir rspec文档中,它声称我们只需添加这三行,但我不确定在何处以及如何更改它以适应我的自定义图像生成 uploaded_file_path = Watir::RSpec.file_path("uploaded.tx

我正在从带有rspec的watirwebdriver迁移到watirrspec(不是很大的变化)。但是现在我想利用记者把我拍的截图和截图联系起来。我正在努力解决这个问题,我不想使用标准截图,因为我有另一个助手功能,可以对图像进行一些处理,截图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.file\u path
方法将文件链接添加到报告中。基本上你:

  • 调用
    file\u path
    方法,该方法告诉报表添加链接并返回所需文件的路径
  • 使用
    file\u path
    返回的路径创建文件,在本例中为屏幕截图
  • 在下面的示例中,After钩子显示了如何使用
    file\u 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
    
    有几个限制:

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

  • 感谢@JustinKo的提示,我成功地覆盖了HtmlFormatter类中的extra_failure_content(exception)方法来修复它。我用一个自定义调用替换了save_屏幕截图调用,以获得自己的屏幕截图,效果非常好。我将它们保存在同一个目录中,以使其更简单

    这里是HtmlFormatter原始代码的链接。

    类MyHtmlFormatter
    您总是想添加屏幕截图还是在测试失败时添加?我想能够将我自己的屏幕截图从另一个文件夹添加到测试用例中。就在测试失败的时候就足够了,我想。文件必须放在另一个文件夹中吗?看起来Watir RSpec希望图像保存在结果文件夹中。对组织来说,这样会更好,是的。但为了让它工作,我会在同一个文件夹上测试,有什么想法吗?我正在查看HtmlFormatter代码,我认为我仍然需要重写一些方法才能使其正常工作。
    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