A small trick if you're embedding screenshots with cucumber/selenium on jruby

306 views
Skip to first unread message

Christian MICHON

unread,
Sep 12, 2011, 4:10:13 AM9/12/11
to cu...@googlegroups.com, us...@jruby.codehaus.org
Hi,

I noticed that embedding images in cucumber/selenium html reports was
done in png. Since I've many features to test with screenshots, I
investigated how to make jpeg files instead and embed them. Main
target was to save disk space.

One major difficulty was to figure out why the converted image was
pink-ish. I understood later on that in java you need to convert the
image to RGB.

I've not fully worked out in jruby how to bypass local file creation
(here called 'xshot.jpg'), but here's the code. Someone might share
improvements :-)

require 'base64'
require 'java'
java_import java.awt.image.BufferedImage
java_import java.io.ByteArrayInputStream
java_import javax.imageio.ImageIO
def xshot
img = @browser.screenshot_as(:base64)
pngimage = ImageIO.read(ByteArrayInputStream.new(Base64.decode64(img).to_java_bytes))
rgbimage = BufferedImage.new(pngimage.width, pngimage.height,
BufferedImage::TYPE_INT_RGB)
rgbimage.getGraphics().drawImage(pngimage, 0, 0, nil)
ImageIO.write(rgbimage, "jpeg", Java::JavaIo::File.new('xshot.jpg'))
xshot = File.open('xshot.jpg','rb')
img = Base64.encode64 xshot.read
embed "data:image/jpeg;base64,#{img}",'image/png'
end

This code can be put in your 'env.rb', and later on you can call
'xhost' in your steps as you need them.

I've noticed the screenshots are above the gherkin lines in the html
report: is this normal ?

--
Christian

aslak hellesoy

unread,
Sep 12, 2011, 5:41:38 AM9/12/11
to cu...@googlegroups.com
Awesome tips Christian!
 
I've noticed the screenshots are above the gherkin lines in the html
report: is this normal ?


It's the way it was implemented. The HTML report code has been touched by many people, so it may not always do the most sane thing. Patches welcome!

Aslak
 
--
Christian

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.


Reply all
Reply to author
Forward
0 new messages