gpt4 book ai didi

ruby - cucumber 嵌入屏幕截图未链接到屏幕截图

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:33 24 4
gpt4 key购买 nike

从 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.7watir-webdriver (0.2.4) cucumber (0.10.3)

最佳答案

阿斯拉克:

Try this:

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

亚当:

Aslak was able to see the embedded image in the file that I emailed him, while I was still unable to do so in IE 8. I tried it out in Firefox 3.6 and the image appears as expected. The problem may have originally been with the embedding method itself (or rather, my use of it), but using Aslak's base64 solution it only fails to work in the Internet Explorer browser.

阿斯拉克:

I believe Base64-encoding of images in HTML pages [1] works in all decent browsers (sorry, IE is not one of them). However, it should work in IE: http://dean.edwards.name/weblog/2005/06/base64-ie/ (but maybe they broke it in IE8, or maybe it only works with gifs, or maybe IE needs a special kind of base64 encoding, or maybe you should just ditch IE)

If being able to read cucumber html reports with screenshots in IE is really important to you, you could always write each image to disk:

 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')

Obviously you have to make sure the relative path you pass to embed is right (depending on where you write the html itself)

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

HTH, Aslak

关于ruby - cucumber 嵌入屏幕截图未链接到屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6229071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com