diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb
index 6439e3d006..4d7754b759 100644
--- a/lib/cucumber/formatter/html.rb
+++ b/lib/cucumber/formatter/html.rb
@@ -44,9 +44,10 @@ def embed(src, mime_type, label)
def embed_image(src, label)
id = "img_#{@img_id}"
@img_id += 1
- if @io.respond_to?(:absolute_path) and File.exist?(src)
- src = Pathname.new(File.absolute_path(src)).relative_path_from(@io.absolute_path)
- end
+ if @io.respond_to?(:path) and File.file?(src)
+ out_dir = Pathname.new(File.dirname(File.absolute_path(@io.path)))
+ src = Pathname.new(File.absolute_path(src)).relative_path_from(out_dir)
+ end
@builder.span(:class => 'embed') do |pre|
pre << %{#{label}
}
diff --git a/spec/cucumber/formatter/html_spec.rb b/spec/cucumber/formatter/html_spec.rb
index eff3b85fb0..b3e566a516 100644
--- a/spec/cucumber/formatter/html_spec.rb
+++ b/spec/cucumber/formatter/html_spec.rb
@@ -28,6 +28,35 @@ module Formatter
}).not_to raise_error
end
+ describe "when writing the report to a file" do
+ before(:each) do
+ allow(@out).to receive(:respond_to?).with(:path, false).and_return(true)
+ expect(@out).to receive(:respond_to?).with(:path).and_return(true)
+ expect(@out).to receive(:path).and_return('out/file.html')
+ run_defined_feature
+ @doc = Nokogiri.HTML(@out.string)
+ end
+
+ describe "with a step that embeds a snapshot" do
+ define_steps do
+ Given(/snap/) {
+ RSpec::Mocks.allow_message(File, :file?) { true }
+ embed('out/snapshot.jpeg', 'image/jpeg')
+ }
+ end
+
+ define_feature(<<-FEATURE)
+ Feature:
+ Scenario:
+ Given snap
+ FEATURE
+
+ it "converts the snapshot path to a relative path" do
+ expect(@doc.css('.embed img').first.attributes['src'].to_s).to eq "snapshot.jpeg"
+ end
+ end
+ end
+
describe "given a single feature" do
before(:each) do
run_defined_feature