Skip to content

Commit

Permalink
You can now include downloadable files in the report data
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Oct 13, 2018
1 parent 06c622a commit 3876558
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package net.serenitybdd.core.reports;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;

public interface AndContent {
public interface AndContent extends FromFile {
void andContents(String contents);
void fromFile(Path source) throws IOException;
void fromFile(Path source, Charset encoding) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.serenitybdd.core.reports;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;

public interface FromFile {
void fromFile(Path source) throws IOException;
void fromFile(Path source, Charset encoding) throws IOException;
FromFile downloadable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

import static net.thucydides.core.ThucydidesSystemProperty.SERENITY_REPORT_ENCODING;

public class ReportDataSaver implements WithTitle, AndContent {
public class ReportDataSaver implements WithTitle, AndContent, FromFile {


private final StepEventBus eventBus;
private String title;
private boolean fileIsDownloadable = false;

public ReportDataSaver(StepEventBus eventBus) {
this.eventBus = eventBus;
Expand Down Expand Up @@ -46,7 +47,17 @@ public void fromFile(Path source, Charset encoding) throws IOException {
Optional<TestOutcome> outcome = eventBus.getBaseStepListener().latestTestOutcome();

if (outcome.isPresent()) {
outcome.get().currentStep().withReportData(ReportData.withTitle(title).fromFile(source, encoding));
ReportData reportData = (fileIsDownloadable) ?
ReportData.withTitle(title).fromPath(source) :
ReportData.withTitle(title).fromFile(source, encoding);

outcome.get().currentStep().withReportData(reportData);
}
}

@Override
public FromFile downloadable() {
this.fileIsDownloadable = true;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class WhenIncludingReportDataWithAStep extends Specification {
testOutcome.lastStep().reportData == ReportData.withTitle("Some data").andContents("<some><data/></some>")
}

def "Arbitrary report data can be added to a step from a file using the Serenity class"() {


def "Arbitrary report data can be added to a step from a downloadable file using the Serenity class"() {
given:
File outputDir = Files.createTempDirectory("out").toFile()

Expand All @@ -72,14 +74,16 @@ class WhenIncludingReportDataWithAStep extends Specification {

when:
def testDataSource = Paths.get(this.class.getResource("/testdata/report-data.xml").toURI())
Serenity.recordReportData().withTitle("Some data").fromFile(testDataSource)
Serenity.recordReportData().withTitle("Some data").downloadable().fromFile(testDataSource)

then:
TestOutcome testOutcome = StepEventBus.getEventBus().baseStepListener.testOutcomes.get(0)
testOutcome.lastStep().hasData() &&
testOutcome.lastStep().reportData == ReportData.withTitle("Some data").andContents("<some><more><data/></more></some>")
testOutcome.lastStep().hasData()
testOutcome.lastStep().reportData.path.startsWith("downloadable")
testOutcome.lastStep().reportData.path.endsWith("report-data.xml")
}


def "Arbitrary report data can be added to a step from a file using the Serenity class using the encoding"() {
given:
File outputDir = Files.createTempDirectory("out").toFile()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.thucydides.core.model;

import net.serenitybdd.core.environment.ConfiguredEnvironment;
import net.thucydides.core.webdriver.Configuration;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.UUID;

public class Downloadables {
public static String copyDownloadableFileFrom(Path source) throws IOException {
Configuration configuration = ConfiguredEnvironment.getConfiguration();
File outputDirectory = configuration.getOutputDirectory();

Path downloadDirectory= outputDirectory.toPath().resolve("downloadable");
Files.createDirectories(downloadDirectory);

Path relativePath = downloadableCopyOf(source);
Path downloadablePath = outputDirectory.toPath().resolve(relativePath);

Files.copy(source, downloadablePath, StandardCopyOption.REPLACE_EXISTING);

return relativePath.toString();
}

private static Path downloadableCopyOf(Path path) {
return Paths.get("downloadable", "downloadable-" + UUID.randomUUID() + "-" + path.getFileName().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
public class ReportData {
private final String title;
private final String contents;
private final String path;

public ReportData(String title, String contents) {
public ReportData(String title, String contents, String path) {

this.title = title;
this.contents = contents;
this.path = path;
}

public static ReportDataBuilder withTitle(String title) {
Expand All @@ -28,6 +30,8 @@ public String getContents() {
return contents;
}

public String getPath() { return path; }

public static class ReportDataBuilder {
private final String title;

Expand All @@ -36,13 +40,20 @@ public ReportDataBuilder(String title) {
}

public ReportData andContents(String contents) {
return new ReportData(title, contents);
return new ReportData(title, contents, null);
}

public ReportData fromFile(Path source, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(source);
return new ReportData(title, new String(encoded, encoding));
return new ReportData(title, new String(encoded, encoding), null);
}

public ReportData fromPath(Path path) throws IOException {
String storedRelativePath = Downloadables.copyDownloadableFileFrom(path);
return new ReportData(title, null, storedRelativePath);
}


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
</#macro>
<#macro reportData(reportData, number) >
<#if reportData.contents?has_content>
<span>
<button type="button" class="btn btn-success btn-sm" data-toggle="collapse"
data-target="#reportData-${number}">
Expand All @@ -366,6 +367,13 @@
<div class="card-body"><pre>${(formatter.renderText(reportData.contents))!}</pre></div>
</div>
</div>
<#else>
<span>
<a role="button" class="btn btn-success btn-sm" href="${reportData.path}">
<i class="fas fa-download"></i>&nbsp;${reportData.title}
</a>
</span>
</#if>
</#macro>
Expand Down

0 comments on commit 3876558

Please sign in to comment.