diff --git a/serenity-core/src/main/java/net/serenitybdd/core/reports/ReportDataSaver.java b/serenity-core/src/main/java/net/serenitybdd/core/reports/ReportDataSaver.java index a89687ee7..1a38357f0 100644 --- a/serenity-core/src/main/java/net/serenitybdd/core/reports/ReportDataSaver.java +++ b/serenity-core/src/main/java/net/serenitybdd/core/reports/ReportDataSaver.java @@ -1,23 +1,26 @@ package net.serenitybdd.core.reports; +import net.thucydides.core.steps.session.PlaybackSession; import net.thucydides.model.domain.ReportData; import net.thucydides.model.domain.TestOutcome; import net.thucydides.model.domain.TestResult; import net.thucydides.model.domain.TestStep; import net.thucydides.core.steps.StepEventBus; import net.thucydides.core.steps.session.TestSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.util.Optional; import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_REPORT_ENCODING; public class ReportDataSaver implements WithTitle, AndContent, FromFile { - private final StepEventBus eventBus; + private static final Logger LOGGER = LoggerFactory.getLogger(ReportDataSaver.class); + private StepEventBus eventBus; private String title; private boolean fileIsDownloadable = false; private boolean isEvidence = false; @@ -46,6 +49,11 @@ private void addReportContent(ReportData reportData) { } public void doAddContents(ReportData reportData) { + if (PlaybackSession.isSessionStarted()) { + StepEventBus playBackStepEventBus = PlaybackSession.getTestSessionContext().getStepEventBus(); + LOGGER.debug("SRP:replace event bus {} with playback event bus {}", eventBus , playBackStepEventBus ); + eventBus = playBackStepEventBus; + } eventBus.getBaseStepListener().latestTestOutcome().ifPresent( outcome -> currentStepOrBackgroundIn(outcome) .withReportData(reportData) diff --git a/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSession.java b/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSession.java new file mode 100644 index 000000000..e80e8d203 --- /dev/null +++ b/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSession.java @@ -0,0 +1,35 @@ +package net.thucydides.core.steps.session; + +import net.thucydides.core.steps.StepEventBus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + + +public class PlaybackSession { + + private static final Logger LOGGER = LoggerFactory.getLogger(PlaybackSession.class); + + private static final ThreadLocal sessionContext = ThreadLocal.withInitial(PlaybackSessionContext::new); + + public static void startSession(String sessionId, StepEventBus stepEventBus) { + sessionContext.get().getSessionStarted().set(true); + sessionContext.get().setSessionId(sessionId); + sessionContext.get().setStepEventBus(stepEventBus); + LOGGER.debug("SRP:PlaybackSessionStart: id: {} " , sessionId); + } + + public static void closeSession() { + sessionContext.get().getSessionStarted().set(false); + LOGGER.debug("SRP:PlaybackSessionEnd: id: {} ", sessionContext.get().getSessionId()); + } + + public static PlaybackSessionContext getTestSessionContext() { + return sessionContext.get(); + } + + + public static boolean isSessionStarted() { + return sessionContext.get().getSessionStarted().get(); + } +} diff --git a/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSessionContext.java b/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSessionContext.java new file mode 100644 index 000000000..8a67210c9 --- /dev/null +++ b/serenity-core/src/main/java/net/thucydides/core/steps/session/PlaybackSessionContext.java @@ -0,0 +1,33 @@ +package net.thucydides.core.steps.session; + +import net.thucydides.core.steps.StepEventBus; +import java.util.concurrent.atomic.AtomicBoolean; + +public class PlaybackSessionContext { + + private String sessionId; + + private StepEventBus stepEventBus; + + private final AtomicBoolean sessionStarted = new AtomicBoolean(false); + + public AtomicBoolean getSessionStarted() { + return sessionStarted; + } + + public void setSessionId(String sessionId) { + this.sessionId = sessionId; + } + + public String getSessionId() { + return sessionId; + } + + public StepEventBus getStepEventBus() { + return stepEventBus; + } + + public void setStepEventBus(StepEventBus stepEventBus) { + this.stepEventBus = stepEventBus; + } +} diff --git a/serenity-cucumber/src/main/java/io/cucumber/core/plugin/ScenarioContextParallel.java b/serenity-cucumber/src/main/java/io/cucumber/core/plugin/ScenarioContextParallel.java index 13cefff6e..ac227f5a3 100644 --- a/serenity-cucumber/src/main/java/io/cucumber/core/plugin/ScenarioContextParallel.java +++ b/serenity-cucumber/src/main/java/io/cucumber/core/plugin/ScenarioContextParallel.java @@ -10,6 +10,7 @@ import io.cucumber.messages.types.Tag; import io.cucumber.plugin.event.TestCase; import io.cucumber.plugin.event.TestStep; +import net.thucydides.core.steps.session.PlaybackSession; import net.thucydides.model.domain.DataTable; import net.thucydides.model.domain.DataTableRow; import net.thucydides.model.domain.TestTag; @@ -373,15 +374,20 @@ public void storeAllStepEventBusEventsForLine(int line, TestCase testCase) { */ public synchronized void playAllTestEvents() { LOGGER.debug("SRP:playAllTestEvents for URI " + scenarioContextURI + "--" + allTestEventsByLine); - for (var entry : allTestEventsByLine.entrySet()) { - try { - replayAllTestCaseEventsForLine(entry.getKey(), entry.getValue()); - } catch (Throwable exception) { - LOGGER.error("An unrecoverable error occurred during test execution: " + exception.getMessage(), exception); - exception.printStackTrace(); - recordUnexpectedFailure(new SerenityManagedException("An unrecoverable error occurred during test execution: " + exception.getMessage(), - exception)); + PlaybackSession.startSession("ScenarioContextParallelPlayback_" + scenarioContextURI ,stepEventBus()); + try { + for (var entry : allTestEventsByLine.entrySet()) { + try { + replayAllTestCaseEventsForLine(entry.getKey(), entry.getValue()); + } catch (Throwable exception) { + LOGGER.error("An unrecoverable error occurred during test execution: " + exception.getMessage(), exception); + exception.printStackTrace(); + recordUnexpectedFailure(new SerenityManagedException("An unrecoverable error occurred during test execution: " + exception.getMessage(), + exception)); + } } + } finally { + PlaybackSession.closeSession(); } clearEventBus(); } diff --git a/serenity-cucumber/src/main/java/io/cucumber/core/plugin/SerenityReporterParallel.java b/serenity-cucumber/src/main/java/io/cucumber/core/plugin/SerenityReporterParallel.java index 0b4f6b7c8..617c163ee 100644 --- a/serenity-cucumber/src/main/java/io/cucumber/core/plugin/SerenityReporterParallel.java +++ b/serenity-cucumber/src/main/java/io/cucumber/core/plugin/SerenityReporterParallel.java @@ -84,7 +84,7 @@ public class SerenityReporterParallel implements Plugin, ConcurrentEventListener private LineFilters lineFilters; - private static final Logger LOGGER = LoggerFactory.getLogger(SerenityReporter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SerenityReporterParallel.class); private final Set contextURISet = new CopyOnWriteArraySet<>();