使用黄瓜将文件附加到 allure 报告失败并出现 'Closed Stream' 错误

Attaching files to allure report using cucumber failing with a 'Closed Stream' error

过去几周,我开始无法生成 Allure 报告。我在我的 hooks.rb 文件中的以下行中收到 'Closed Stream' 错误,我用它来截取场景失败的屏幕截图。以下是 ruby 文件中的一些相关行-

def take_screenshot(scenario_name)
    screenshots_path= 'artifacts/screenshots/'
    unless File.directory?(screenshots_path)
        FileUtils.mkdir_p(screenshots_path)
    end
    puts "scenario name is  #{scenario_name}"
    page.driver.browser.save_screenshot "#{screenshots_path}/#{scenario_name}.png"
end
*******
    if scenario.failed?
        path = take_screenshot(scenario.name)
        puts "path to attach #{path}"
        AllureCucumber::DSL.attach_file("#{scenario.name}.png -FAILURE Screenshot", path) if ENV['ALLURE']=='true'
    end

由于最后一行,魅力报告生成失败

AllureCucumber::DSL.attach_file("#{scenario.name}.png -FAILURE Screenshot", path) if ENV['ALLURE']=='true'

将屏幕截图文件附加到报告中说 "IOError Closed Stream"。这里出了什么问题?

我做了更多调查,发现那行-

AllureCucumber::DSL.attach_file("#{scenario.name}.png -FAILURE Screenshot", path) if ENV['ALLURE']=='true'

实际上是指水豚中的attach_file方法gem -

[https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb][1]

失败了。

我用撬和调试得到这个-

[1] pry(#)> AllureCucumber::DSL.attach_file("#{scenario.name} -FAILURE Screenshot", path) #if ENV['ALLURE']=='true' IOError: closed stream from /Users/rv/.rvm/gems/ruby-2.2.0/gems/capybara-2.5.0/lib/capybara/node/actions.rb:154:in each' [2] pry(#)> attach_file("#{scenario.name} -FAILURE Screenshot", path) #if ENV['ALLURE']=='true' IOError: closed stream from /Users/rv/.rvm/gems/ruby-2.2.0/gems/capybara-2.5.0/lib/capybara/node/actions.rb:154:ineach'

最终发现这是因为其中一个步骤定义文件包含了一个覆盖 attach_file 方法的水豚库。