3

来自Cukes Google Group 的交叉发布:

我尝试了多种保存屏幕截图的方法,但最终选择了内置于 watir-webdriver 中的方法。无论我使用哪种方法,我都无法在 Cucumber HTML 报告中成功嵌入指向该图像的链接。

在 c:\ruby\cucumber\project_name\features\support\hooks.rb 中,我正在使用:

After do |scenario|
   if scenario.failed?
      @browser.driver.save_screenshot("screenshot.png")
      embed("screenshot.png", "image/png")
   end
end

带有文本“屏幕截图”的链接已添加到报告中,但 URL 是项目目录路径("c:\ruby\cucumber\project_name"),而不是文件的直接链接("c:\ruby\cucumber\project_name\screenshot.png")。我使用 Dir.pwd 尝试了许多不同的图像格式和直接路径,每次都得到相同的结果。

我错过了什么?

谢谢

Windows XP Ruby 1.8.7 watir-webdriver (0.2.4) 黄瓜 (0.10.3)

4

1 回答 1

4

阿斯拉克:

尝试这个:

After do |scenario|
  if scenario.failed?
    encoded_img = @browser.driver.screenshot_as(:base64)
    embed("data:image/png;base64,#{encoded_img}",'image/png')
  end
end

阿斯拉克

亚当:

Aslak 能够在我通过电子邮件发送给他的文件中看到嵌入的图像,而我在 IE 8 中仍然无法这样做。我在 Firefox 3.6 中进行了尝试,图像按预期显示。问题最初可能出在嵌入方法本身(或者更确切地说,我使用它),但使用 Aslak 的 base64 解决方案,它只能在 Internet Explorer 浏览器中工作。

阿斯拉克:

我相信 HTML 页面 [1] 中图像的 Base64 编码适用于所有体面的浏览器(抱歉,IE 不是其中之一)。但是,它应该在 IE 中工作:http: //dean.edwards.name/weblog/2005/06/base64-ie/ (但也许他们在 IE8 中破坏了它,或者它只适用于 gif,或者 IE 需要一个特殊的base64编码,或者你应该放弃IE)

如果能够在 IE 中读取带有屏幕截图的黄瓜 html 报告对您来说真的很重要,您可以随时将每个图像写入磁盘:

 png = @browser.driver.screenshot_as(:png)
 path = (0..16).to_a.map{|a| rand(16).to_s(16)}.join + '.png' # Or use some GUID library to make a unique filename - scenario names are not  guaranteed to be unique.
 File.open(path, 'wb') {|io| io.write(png)}
 embed(path, 'image/png')

显然,您必须确保传递给 embed 的相对路径是正确的(取决于您编写 html 本身的位置)

[1] http://en.wikipedia.org/wiki/Data_URI_scheme

HTH,阿斯拉克

于 2011-06-07T18:30:17.240 回答