Skip to content

Commit ad7d562

Browse files
committed
Merge pull request #51 from MehdiK/self-contained-html-report
makes html report self-contained: fixes #44 & #49
2 parents 15cf289 + 1dd69c1 commit ad7d562

8 files changed

Lines changed: 65 additions & 41 deletions

File tree

TestStack.BDDfy/Processors/Reporters/Html/HtmlReportBuilder.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,8 @@ private void HtmlHead()
4040
using(OpenTag(HtmlTag.head))
4141
{
4242
AddLine("<meta charset='utf-8'/>");
43-
AddLine(string.Format("<link href='BDDfy.css' rel='stylesheet'/>"));
44-
if(_viewModel.UseCustomStylesheet)
45-
AddLine(string.Format("<link href='BDDfyCustom.css' rel='stylesheet'/>"));
46-
47-
AddLine("<script type='text/javascript' src='jquery-1.7.1.min.js'></script>");
48-
AddLine(string.Format("<script type='text/javascript' src='BDDfy.js'></script>"));
49-
if (_viewModel.UseCustomJavascript)
50-
AddLine(string.Format("<link href='BDDfyCustom.js' rel='stylesheet'/>"));
43+
EmbedCssFile(HtmlReportResources.BDDfy_css);
44+
EmbedCssFile(_viewModel.CustomStylesheet, HtmlReportResources.CustomStylesheetComment);
5145

5246
AddLine(string.Format("<title>BDDfy Test Result {0}</title>", DateTime.Now.ToShortDateString()));
5347
}
@@ -142,11 +136,15 @@ private void ResultDetails()
142136
private void Footer()
143137
{
144138
AddLine("<div class='footer'>Powered by <a href='https://github.com/TestStack/TestStack.BDDfy'>BDDfy</a> framework</div>");
139+
140+
AddLine("<script type='text/javascript' src='https://code.jquery.com/jquery-1.11.0.min.js'></script>");
141+
EmbedJavascriptFile(HtmlReportResources.BDDfy_js);
142+
EmbedJavascriptFile(_viewModel.CustomJavascript, HtmlReportResources.CustomJavascriptComment);
145143
}
146144

147145
private void AddStory(Story story)
148146
{
149-
var scenariosInGroup = story.Scenarios;
147+
var scenariosInGroup = story.Scenarios.ToList();
150148
var storyResult = (StepExecutionResult)scenariosInGroup.Max(s => (int)s.Result);
151149

152150
using (OpenTag(HtmlTag.li))
@@ -265,5 +263,32 @@ private void AddLine(string line)
265263
int tabWidth = _tabCount * TabIndentation;
266264
_html.AppendLine(string.Empty.PadLeft(tabWidth) + line);
267265
}
266+
267+
private void EmbedCssFile(string cssContent, string htmlComment = null)
268+
{
269+
using (OpenTag("<style type='text/css'>", HtmlTag.style))
270+
{
271+
AddHtmlComment(htmlComment);
272+
_html.AppendLine(cssContent);
273+
}
274+
}
275+
276+
private void EmbedJavascriptFile(string javascriptContent, string htmlComment = null)
277+
{
278+
using (OpenTag(HtmlTag.script))
279+
{
280+
AddHtmlComment(htmlComment);
281+
_html.AppendLine(javascriptContent);
282+
}
283+
}
284+
285+
private void AddHtmlComment(string htmlComment)
286+
{
287+
if (string.IsNullOrWhiteSpace(htmlComment))
288+
return;
289+
290+
_html.AppendFormat("/*{0}*/", htmlComment);
291+
_html.AppendLine();
292+
}
268293
}
269294
}

TestStack.BDDfy/Processors/Reporters/Html/HtmlReportResources.Designer.cs

Lines changed: 15 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TestStack.BDDfy/Processors/Reporters/Html/HtmlReportResources.resx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@
124124
<data name="BDDfy_js" type="System.Resources.ResXFileRef, System.Windows.Forms">
125125
<value>BDDfy.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
126126
</data>
127-
<data name="jquery_1_7_1_min" type="System.Resources.ResXFileRef, System.Windows.Forms">
128-
<value>jquery-1.7.1.min.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
127+
<data name="CustomJavascriptComment" xml:space="preserve">
128+
<value>If you drop a custom Javascript named BDDfyCustom.js in your output folder it gets embedded here. This way you can apply some custom Javascript logic to your html report.</value>
129+
</data>
130+
<data name="CustomStylesheetComment" xml:space="preserve">
131+
<value>If you drop a custom stylesheet named BDDfyCustom.css in your output folder it gets embedded here. This way you can apply some custom styles over your html report.</value>
129132
</data>
130133
</root>

TestStack.BDDfy/Processors/Reporters/Html/HtmlReportViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public HtmlReportViewModel(IHtmlReportConfiguration configuration, IEnumerable<S
1111
Configuration = configuration;
1212
}
1313

14-
public bool UseCustomStylesheet { get; set; }
15-
public bool UseCustomJavascript { get; set; }
14+
public string CustomStylesheet { get; set; }
15+
public string CustomJavascript { get; set; }
1616

1717
public IHtmlReportConfiguration Configuration { get; private set; }
1818
}

TestStack.BDDfy/Processors/Reporters/Html/HtmlReporter.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ public HtmlReporter(IHtmlReportConfiguration configuration, IReportBuilder build
2424

2525
public void Process(IEnumerable<Story> stories)
2626
{
27-
WriteOutScriptFiles();
28-
2927
var allowedStories = stories.Where(s => _configuration.RunsOn(s)).ToList();
3028
WriteOutHtmlReport(allowedStories);
3129
}
3230

33-
3431
void WriteOutHtmlReport(IEnumerable<Story> stories)
3532
{
3633
var viewModel = new HtmlReportViewModel(_configuration, stories);
37-
ShouldTheReportUseCustomization(viewModel);
34+
LoadCustomScripts(viewModel);
3835
string report;
3936

4037
try
@@ -49,20 +46,15 @@ void WriteOutHtmlReport(IEnumerable<Story> stories)
4946
_writer.OutputReport(report, _configuration.OutputFileName, _configuration.OutputPath);
5047
}
5148

52-
private void ShouldTheReportUseCustomization(HtmlReportViewModel viewModel)
49+
private void LoadCustomScripts(HtmlReportViewModel viewModel)
5350
{
5451
var customStylesheet = Path.Combine(_configuration.OutputPath, "BDDfyCustom.css");
55-
viewModel.UseCustomStylesheet = File.Exists(customStylesheet);
52+
if (File.Exists(customStylesheet))
53+
viewModel.CustomStylesheet = File.ReadAllText(customStylesheet);
5654

5755
var customJavascript = Path.Combine(_configuration.OutputPath, "BDDfyCustom.js");
58-
viewModel.UseCustomJavascript = File.Exists(customJavascript);
59-
}
60-
61-
void WriteOutScriptFiles()
62-
{
63-
_writer.OutputReport(HtmlReportResources.BDDfy_css, "BDDfy.css", _configuration.OutputPath);
64-
_writer.OutputReport(HtmlReportResources.jquery_1_7_1_min, "jquery-1.7.1.min.js", _configuration.OutputPath);
65-
_writer.OutputReport(HtmlReportResources.BDDfy_js, "BDDfy.js", _configuration.OutputPath);
56+
if(File.Exists(customJavascript))
57+
viewModel.CustomJavascript = File.ReadAllText(customJavascript);
6658
}
6759
}
6860
}

TestStack.BDDfy/Processors/Reporters/Html/HtmlTag.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public enum HtmlTag
1111
p,
1212
body,
1313
header,
14-
section
14+
section,
15+
style,
16+
script
1517
}
1618
// ReSharper restore InconsistentNaming
1719
}

TestStack.BDDfy/Processors/Reporters/Html/jquery-1.7.1.min.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@
159159
<ItemGroup>
160160
<Content Include="Processors\Reporters\Html\BDDfy.css" />
161161
<Content Include="Processors\Reporters\Html\BDDfy.js" />
162-
<Content Include="Processors\Reporters\Html\jquery-1.7.1.min.js" />
163162
</ItemGroup>
164163
<ItemGroup>
165164
<EmbeddedResource Include="Processors\Reporters\Html\HtmlReportResources.resx">

0 commit comments

Comments
 (0)