diff --git a/History.md b/History.md index 00ed1c1707..1742221569 100644 --- a/History.md +++ b/History.md @@ -2,6 +2,7 @@ ### New Features + * Let the HTML formatter store the relative path to files in the report ([697](https://github.com/cucumber/cucumber/pull/697) @idstein, @brasmusson) * Allow cucumber.yml to parse % erb code lines ([755](https://github.com/cucumber/cucumber/pull/755) @snowe2010) * Give each step definition a unique copy of argument objects ([760](https://github.com/cucumber/cucumber/pull/760) @tooky) diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb index a0523d324e..85a0ecca5e 100644 --- a/lib/cucumber/formatter/html.rb +++ b/lib/cucumber/formatter/html.rb @@ -2,6 +2,7 @@ require 'builder' require 'cucumber/formatter/duration' require 'cucumber/formatter/io' +require 'pathname' module Cucumber module Formatter @@ -50,6 +51,10 @@ def embed(src, mime_type, label) def embed_image(src, label) id = "img_#{@img_id}" @img_id += 1 + 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 eea2fec9a1..eecedcefeb 100644 --- a/spec/cucumber/formatter/html_spec.rb +++ b/spec/cucumber/formatter/html_spec.rb @@ -29,6 +29,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