Skip to content

Commit

Permalink
HTML report: generate a page with all failed scenarios #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 25, 2014
1 parent 88f1375 commit 5bd810f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,29 @@ public void writeEnd() {
PackageToc packageToc = new PackageTocBuilder( models ).getRootPackageToc();
HtmlTocWriter tocWriter = new HtmlTocWriter( tagMap, packageToc );
ReportStatistics totalStatistics = new ReportStatistics();
List<ScenarioModel> failedScenarios = Lists.newArrayList();

for( ModelFile modelFile : models ) {
ReportModelHtmlWriter modelWriter = ReportModelHtmlWriter.writeModelToFile( modelFile.model, tocWriter, modelFile.file );
totalStatistics = totalStatistics.add( modelWriter.getStatistics() );
failedScenarios.addAll( modelFile.model.getFailedScenarios() );
}

writeTagFiles( tocWriter );
writeFailedFile( tocWriter, failedScenarios );

StatisticsPageHtmlWriter statisticsPageHtmlWriter = new StatisticsPageHtmlWriter( tocWriter, totalStatistics );
statisticsPageHtmlWriter.write( toDir );

}

private void writeFailedFile( HtmlTocWriter tocWriter, List<ScenarioModel> failedScenarios ) {
ReportModel reportModel = new ReportModel();
reportModel.setScenarios( failedScenarios );
reportModel.setClassName( ".Failed Scenarios" );
ReportModelHtmlWriter.writeModelToFile( reportModel, tocWriter, new File( toDir, "failed.html" ) );
}

private void writeTagFiles( HtmlTocWriter tocWriter ) {
if( tagMap.isEmpty() ) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ private void writeStatistics() {
writeStatisticNumber( statistics.numScenarios, "scenarios" );
writeStatisticNumber( statistics.numCases, "cases" );
writeStatisticNumber( statistics.numSteps, "steps" );
writeStatisticNumber( "" + statistics.numFailedCases, "failed cases", statistics.numFailedCases > 0 ? "failed" : "" );

String failedClass = statistics.numFailedCases > 0 ? "failed" : "";
printWriter.write( "<div class='statistics-number " + failedClass + "'><a href='failed.html'><i>"
+ statistics.numFailedCases + "</i><br/><b>failed cases</b></a></div>" );

writeStatisticNumber( DurationFormatter.format( statistics.durationInNanos ), "total time" );
long averageNanos = statistics.numCases != 0 ? statistics.durationInNanos / statistics.numCases : 0;
writeStatisticNumber( DurationFormatter.format( averageNanos ), "time / case" );
printWriter.println( "</div>" );
}

private void writeStatisticNumber( int number, String name ) {
writeStatisticNumber( number + "", name, "" );
writeStatisticNumber( number + "", name );
}

private void writeStatisticNumber( String number, String name ) {
writeStatisticNumber( number, name, "" );
}

private void writeStatisticNumber( String number, String name, String extraClass ) {
printWriter.write( "<div class='statistics-number " + extraClass + "'><i>" + number + "</i><br/><b>" + name + "</b></div>" );
printWriter.write( "<div class='statistics-number'><i>" + number + "</i><br/><b>" + name + "</b></div>" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ public String getPackageName() {
return this.className.substring( 0, index );
}

public List<ScenarioModel> getFailedScenarios() {
List<ScenarioModel> result = Lists.newArrayList();
for( ScenarioModel m : scenarios ) {
if( m.getExecutionStatus() == ExecutionStatus.FAILED ) {
result.add( m );
}
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -655,33 +655,39 @@ ul.steps li {
border-radius: 20px;
display: inline-block;
margin-right: 20px;
margin-bottom: 20px;
text-align: center;
background-color: #f0f0f0;
}

.statistics-number a {
display: inline-block;
color: red;
}

.statistics-number.failed {
background-color: #f0f0f0;
}

.statistics-number > i {
.statistics-number i {
font-size: 20px;
color: #222;
font-weight: 200;
font-style: normal;
}

.statistics-number.failed > i {
.statistics-number.failed i {
color: red;
font-weight: bold;
}

.statistics-number > b {
.statistics-number b {
text-transform: uppercase;
font-size: 14px;
color: gray;
}

.statistics-number.failed > b {
.statistics-number.failed b {
color: red;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.tngtech.jgiven.report.json.GivenJsonReports;
import com.tngtech.jgiven.tags.FeatureHtmlReport;
import com.tngtech.jgiven.tags.FeatureTags;
import com.tngtech.jgiven.tags.Issue;

@RunWith( DataProviderRunner.class )
@FeatureHtmlReport
Expand Down Expand Up @@ -57,4 +58,14 @@ public void the_static_HTML_reporter_generates_one_file_for_each_tag_value() thr
.and().a_file_with_name_$_exists( "TestTag-456.html" );
}

@Test
@Issue( "#29" )
public void the_static_HTML_report_generates_one_file_for_all_failed_scenarios() throws IOException {
given().a_report_model()
.and().the_report_has_$_scenarios( 1 )
.and().case_$_of_scenario_$_has_failed( 1, 1 )
.and().the_report_exist_as_JSON_file();
when().the_static_HTML_reporter_is_executed();
then().a_file_with_name_$_exists( "failed.html" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public SELF the_scenario_has_parameters( String... params ) {
return self();
}

public SELF case_$_of_scenario_$_has_failed( int caseNr, int scenarioNr ) {
getCase( scenarioNr, caseNr ).success = false;
return self();
}

public SELF case_$_fails_with_error_message( int ncase, String errorMessage ) {
getCase( ncase ).errorMessage = errorMessage;
getCase( ncase ).success = false;
Expand Down Expand Up @@ -138,6 +143,10 @@ public SELF the_scenario_has_parameters( String... params ) {
return case_$_has_a_when_step_$_with_argument( i, name, arg );
}

private ScenarioCaseModel getCase( int scenarioNr, int caseNr ) {
return reportModel.getScenarios().get( scenarioNr - 1 ).getCase( caseNr - 1 );
}

private ScenarioCaseModel getCase( int ncase ) {
return reportModel.getLastScenarioModel().getScenarioCases().get( ncase - 1 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public SELF the_first_scenario_has_tag( String name ) {
givenReportModel.scenario_$_has_tag_$_with_value_$( i, name, value );
return self();
}

public SELF case_$_of_scenario_$_has_failed( int caseNr, int scenarioNr ) {
givenReportModel.case_$_of_scenario_$_has_failed( caseNr, scenarioNr );
return self();
}
}

0 comments on commit 5bd810f

Please sign in to comment.