Cucumber embed for screenshots not linking to screenshot

微笑、不失礼 提交于 2019-12-03 17:44:55
adam reed

Aslak:

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

Adam:

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.

Aslak:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!