Skip to content

Instantly share code, notes, and snippets.

@billwang1990
Created October 28, 2016 10:56
Show Gist options
  • Save billwang1990/15ee93dd64bf07027fff57d378259b3e to your computer and use it in GitHub Desktop.
Save billwang1990/15ee93dd64bf07027fff57d378259b3e to your computer and use it in GitHub Desktop.
# encoding: UTF-8
require 'cucumber/formatter/html'
class NewHtml < Cucumber::Formatter::Html
def initialize(step_mother, path_or_io, options)
super(step_mother, path_or_io, options)
@summary={}
@currentFeature = ""
end
def before_features(features)
super(features)
@builder.div(:id => 'summary_table') do
end
end
def feature_name(keyword, name)
lines = name.split(/\r?\n/)
return if lines.empty?
@builder.h2 do |h2|
@builder.span(keyword + ': ' + lines[0], :class => 'val')
@builder.span("Top", :id => 'back_to_top', :style => "float: right; cursor: pointer;")
end
@builder.p(:class => 'narrative') do
lines[1..-1].each do |line|
@builder.text!(line.strip)
@builder.br
end
end
end
def inline_js_content
puts "1212"
<<-EOF
SCENARIOS = "h3[id^='scenario_'],h3[id^=background_]";
BACK_TOPS = "span[id^='back_to_top']";
$(document).ready(function() {
$(SCENARIOS).css('cursor', 'pointer');
$(SCENARIOS).click(function() {
$(this).siblings().toggle(250);
});
$("#collapser").css('cursor', 'pointer');
$("#collapser").click(function() {
$(SCENARIOS).siblings().hide();
});
$("#expander").css('cursor', 'pointer');
$("#expander").click(function() {
$(SCENARIOS).siblings().show();
});
$(BACK_TOPS).click(function() {
alert("hahah");
$('html, body').animate({scrollTop:0},500);
});
})
function moveProgressBar(percentDone) {
$("cucumber-header").css('width', percentDone +"%");
}
function makeRed(element_id) {
$('#'+element_id).css('background', '#C40D0D');
$('#'+element_id).css('color', '#FFFFFF');
}
function makeYellow(element_id) {
$('#'+element_id).css('background', '#FAF834');
$('#'+element_id).css('color', '#000000');
}
EOF
end
def before_feature(feature)
@currentFeature=feature.name
ft = {
:title => @currentFeature,
:desc => feature.description,
:start_at => Time.now,
:total => 0, :pass => 0, :fail => 0, :skip => 0}
@summary[@currentFeature] = ft
@exceptions = []
@builder << "<div id=\"feature_#{@summary.size}\" class=\"feature\">"
end
def after_feature(feature)
@summary[@currentFeature][:end_at] = Time.now
@summary[@currentFeature][:run_time] = @summary[@currentFeature][:end_at] - @summary[@currentFeature][:start_at]
@summary[@currentFeature][:pass_rate] = (@summary[@currentFeature][:pass].to_f / @summary[@currentFeature][:total].to_f * 1000).to_i / 10.0
super feature
end
def after_feature_element(feature_element)
if feature_element.is_a?(Cucumber::Formatter::LegacyApi::Ast::Scenario)
if feature_element.status == :passed then
@summary[@currentFeature][:pass] += 1
elsif feature_element.status == :failed then
@summary[@currentFeature][:fail] += 1
else
@summary[@currentFeature][:skip] += 1
end
@summary[@currentFeature][:total] += 1
end
super feature_element
end
def print_stats(features)
@builder << <<-JS
<script type="text/javascript">
$('#summary_table').html("<br><table border=1 width=98% cellpadding=0 cellspacing=0 align=center bgcolor=blue><tr><th align=left><b>功能</b></th><th align=left><b>描述</b></th><th align=left><b>成功率</b></th><th align=left><b>执行时间</b></th></tr></table>");
$('#summary_table').ready(function(){
$("th").css("background-color","#CCFFFF");
$("tr").each(function(){
var trObj = parseFloat($(this).attr("value"));
if(!isNaN(trObj) && trObj == 100.0){
$(this).find("td").each(function(){
$(this).css("background-color","#CCFF66");
})
} else if(!isNaN(trObj) && trObj >= 80.0 && trObj < 100.0 ){
$(this).find("td").each(function(){
$(this).css("background-color","#FFFF99");
})
} else if(!isNaN(trObj)) {
$(this).find("td").each(function(){
$(this).css("background-color","#FFCCFF");
})
}
})
});
JS
for i in @summary.to_a do
feature = i[1]
index = @summary.to_a.index(i)+1
@builder << <<-JS
tr = document.createElement('tr');
tr.setAttribute('value',"#{feature[:pass_rate]}");
title = document.createElement('td');
title_a = document.createElement('a');
title_a.appendChild(document.createTextNode("#{feature[:title]}"));
title_a.setAttribute('href',"#feature_#{index}");
title.appendChild(title_a);
desc = document.createElement('td');
desc.appendChild(document.createTextNode("#{feature[:desc].gsub(/\n/,'\\n')}"));
pass_rate = document.createElement('td');
pass_rate.appendChild(document.createTextNode("#{feature[:pass_rate]}%"));
pass_rate.setAttribute('value',"#{feature[:pass_rate]}");
run_time = document.createElement('td');
run_time.appendChild(document.createTextNode("#{format_duration(feature[:run_time])}"));
tr.appendChild(title);
tr.appendChild(desc);
tr.appendChild(pass_rate);
tr.appendChild(run_time);
$('#summary_table table').append(tr);
JS
end
@builder << "</script>"
super features
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment