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

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 :slight_smile:

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