Skip to content

Commit

Permalink
Fix attaching files to the corresponding test steps (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan-Sap authored Dec 12, 2023
1 parent 62b5db1 commit d137149
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package net.serenitybdd.core.reports;

import net.thucydides.core.steps.events.StepEventBusEventBase;
import net.thucydides.model.domain.ReportData;

public class AddReportContentEvent extends StepEventBusEventBase {


private ReportDataSaver reportDataSaver;
private String contents;
private ReportData reportData;

public AddReportContentEvent(final ReportDataSaver reportDataSaver,String contents) {
public AddReportContentEvent(final ReportDataSaver reportDataSaver,ReportData reportData) {
this.reportDataSaver = reportDataSaver;
this.contents = contents;
this.reportData = reportData;
}


@Override
public void play() {
reportDataSaver.doAddContents(contents);
reportDataSaver.doAddContents(reportData);
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ public AndContent withTitle(String title) {

@Override
public void andContents(String contents) {
ReportData reportData = ReportData.withTitle(title).andContents(contents).asEvidence(isEvidence);
addReportContent(reportData);
}

private void addReportContent(ReportData reportData) {
if(!TestSession.isSessionStarted()) {
doAddContents(contents);
doAddContents(reportData);
} else {
TestSession.addEvent(new AddReportContentEvent(this,contents));
TestSession.addEvent(new AddReportContentEvent(this,reportData));
}
}

public void doAddContents(String contents) {
public void doAddContents(ReportData reportData) {
eventBus.getBaseStepListener().latestTestOutcome().ifPresent(
outcome -> currentStepOrBackgroundIn(outcome)
.withReportData(ReportData.withTitle(title).andContents(contents).asEvidence(isEvidence))
outcome -> currentStepOrBackgroundIn(outcome)
.withReportData(reportData)
);
}

Expand All @@ -64,16 +69,10 @@ public void fromFile(Path source) throws IOException {

@Override
public void fromFile(Path source, Charset encoding) throws IOException {

Optional<TestOutcome> outcome = eventBus.getBaseStepListener().latestTestOutcome();

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

outcome.get().currentStep().get().withReportData(reportData);
}
ReportData reportData = (fileIsDownloadable) ?
ReportData.withTitle(title).fromPath(source).asEvidence(isEvidence) :
ReportData.withTitle(title).fromFile(source, encoding).asEvidence(isEvidence);
addReportContent(reportData);
}

@Override
Expand Down

0 comments on commit d137149

Please sign in to comment.