From 7f146d3c465c14984876bf51945971e28b73628b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sun, 22 Jun 2014 13:34:23 +0200 Subject: [PATCH 1/2] Support embedding images directly in html files --- lib/cucumber/formatter/html.rb | 3 +++ spec/cucumber/formatter/html_spec.rb | 33 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb index ef5d6b05e5..7209a557fb 100644 --- a/lib/cucumber/formatter/html.rb +++ b/lib/cucumber/formatter/html.rb @@ -28,6 +28,9 @@ def initialize(runtime, path_or_io, options) def embed(src, mime_type, label) case(mime_type) when /^image\/(png|gif|jpg|jpeg)/ + unless File.file?(src) or src =~ /^data:image\/(png|gif|jpg|jpeg);base64,/ + src = "data:" + mime_type + ";base64," + src + end embed_image(src, label) end end diff --git a/spec/cucumber/formatter/html_spec.rb b/spec/cucumber/formatter/html_spec.rb index 15b7dfb2a8..5b76113138 100644 --- a/spec/cucumber/formatter/html_spec.rb +++ b/spec/cucumber/formatter/html_spec.rb @@ -235,7 +235,10 @@ module Formatter describe "with a step that embeds a snapshot" do define_steps do - Given(/snap/) { embed('snapshot.jpeg', 'image/jpeg') } + Given(/snap/) { + File.should_receive(:file?).with('snapshot.jpeg').and_return(true) + embed('snapshot.jpeg', 'image/jpeg') + } end define_feature(<<-FEATURE) @@ -247,6 +250,34 @@ module Formatter it { @doc.css('.embed img').first.attributes['src'].to_s.should == "snapshot.jpeg" } end + describe "with a step that embeds a snapshot content manually" do + define_steps do + Given(/snap/) { embed('data:image/png;base64,YWJj', 'image/png') } + end + + define_feature(<<-FEATURE) + Feature: + Scenario: + Given snap + FEATURE + + it { @doc.css('.embed img').first.attributes['src'].to_s.should == "data:image/png;base64,YWJj" } + end + + describe "with a step that embeds a snapshot content" do + define_steps do + Given(/snap/) { embed('YWJj', 'image/png') } + end + + define_feature(<<-FEATURE) + Feature: + Scenario: + Given snap + FEATURE + + it { @doc.css('.embed img').first.attributes['src'].to_s.should == "data:image/png;base64,YWJj" } + end + describe "with an undefined Given step then an undefined And step" do define_feature(<<-FEATURE) Feature: From f39bf4eddaea1c0aa1618556a88a8859f9ccc846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sun, 22 Jun 2014 19:55:58 +0200 Subject: [PATCH 2/2] Prefer encoding included in mime-type when embedding images direcly --- lib/cucumber/formatter/html.rb | 3 ++- spec/cucumber/formatter/html_spec.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb index 7209a557fb..850e875e7a 100644 --- a/lib/cucumber/formatter/html.rb +++ b/lib/cucumber/formatter/html.rb @@ -29,7 +29,8 @@ def embed(src, mime_type, label) case(mime_type) when /^image\/(png|gif|jpg|jpeg)/ unless File.file?(src) or src =~ /^data:image\/(png|gif|jpg|jpeg);base64,/ - src = "data:" + mime_type + ";base64," + src + type = mime_type =~ /;base[0-9]+$/ ? mime_type : mime_type + ";base64" + src = "data:" + type + "," + src end embed_image(src, label) end diff --git a/spec/cucumber/formatter/html_spec.rb b/spec/cucumber/formatter/html_spec.rb index 5b76113138..0de8ebbd67 100644 --- a/spec/cucumber/formatter/html_spec.rb +++ b/spec/cucumber/formatter/html_spec.rb @@ -266,7 +266,7 @@ module Formatter describe "with a step that embeds a snapshot content" do define_steps do - Given(/snap/) { embed('YWJj', 'image/png') } + Given(/snap/) { embed('YWJj', 'image/png;base64') } end define_feature(<<-FEATURE)