Skip to content

Instantly share code, notes, and snippets.

@bfaloona
Created December 21, 2009 21:14
Show Gist options
  • Save bfaloona/261241 to your computer and use it in GitHub Desktop.
Save bfaloona/261241 to your computer and use it in GitHub Desktop.
cucumber formatter html embed spec and patch
# spec/cucumber/formatter/html_spec.rb
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/spec_helper'
require 'cucumber/formatter/html'
require 'nokogiri'
require 'cucumber/rb_support/rb_language'
module Cucumber
module Formatter
describe Html do
extend SpecHelperDsl
include SpecHelper
Spec::Matchers.define :have_css_node do |css, regexp|
match do |doc|
nodes = doc.css(css)
nodes.detect{ |node| node.text =~ regexp }
end
end
before(:each) do
@out = StringIO.new
@formatter = Html.new(step_mother, @out, {})
end
it "should not raise an error when visiting a blank feature name" do
lambda { @formatter.feature_name("") }.should_not raise_error
end
describe "given a single feature" do
before(:each) do
run_defined_feature
@doc = Nokogiri.HTML(@out.string)
end
# existing specs here
# new spec
describe "with a step that embeds a snapshot" do
define_steps do
Given(/snap/) { embed('snapshot.jpeg', 'image/jpeg') }
end
define_feature(<<-FEATURE)
Scenario:
Given snap
FEATURE
it { @doc.should have_css_node('.embed', /snap/) }
end
end
end
end
end
# lib/cucumber/formatter/html.rb
module Cucumber
module Formatter
class Html
include ERB::Util # for the #h method
include Duration
include Io
# initialize method untouched
def embed(file, mime_type)
case(mime_type)
when /^image\/(png|gif|jpg|jpeg)/
embed_image(file)
end
end
def embed_image(file)
id = file.hash
@builder.span(:class => 'embed') do |pre|
pre << %{<a href="" onclick="img=document.getElementById('#{id}'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false">Screenshot</a><br>&nbsp;
<img id="#{id}" style="display: none" src="#{file}"/>}
end
end
# file continues
end
end
end
@nalbion
Copy link

nalbion commented Jul 12, 2012

Can you add an option to set the size of the image (either by setting height/width directly, or by adding class="embed_image" and applying style by CSS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment