Skip to content

Commit

Permalink
Fix for screenshots FOR_FAILURES (#3361)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Sap <[email protected]>
  • Loading branch information
Johan-Sap and Johan Sap authored Dec 23, 2023
1 parent a98add4 commit f4fd076
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public class BaseStepListener implements StepListener, StepPublisher {
private Darkroom darkroom;
private Photographer photographer;
private SoundEngineer soundEngineer;
private List<ScreenshotAndHtmlSource> usedScreenshots = new ArrayList<>();
private List<ScreenshotAndHtmlSource> allScreenshots = new ArrayList<>();

private final CloseBrowser closeBrowsers;

Expand Down Expand Up @@ -644,6 +646,7 @@ public void testFinished(final TestOutcome outcome, boolean isInDataDrivenTest,
while (!currentGroupStack.isEmpty()) {
finishGroup();
}
removeUnnecesaryScreenshotsFromOutputDir();
LifecycleRegister.clear();
}

Expand Down Expand Up @@ -910,6 +913,7 @@ public void stepFailed(StepFailure failure) {

recordFailureDetails(failure);
}
removeUnnecesaryScreenshotsFromOutputDir();
// In all cases, mark the step as done with the appropriate result
currentStepDone(failureAnalysis.resultFor(failure));
}
Expand All @@ -925,6 +929,7 @@ public void stepFailed(StepFailure failure, List<ScreenshotAndHtmlSource> screen

recordFailureDetails(failure);
}
removeUnnecesaryScreenshotsFromOutputDir();
// Step marked as done with the appropriate result before
currentStepDone(failureAnalysis.resultFor(failure));
}
Expand Down Expand Up @@ -1054,16 +1059,17 @@ private void takeEndOfStepScreenshotFor(final TestResult result) {
}
}


private void takeEndOfStepScreenshotForRecording(final TestResult result, List<ScreenshotAndHtmlSource> screenshots) {
if ((currentTestIsABrowserTest() && shouldTakeEndOfStepScreenshotFor(result))) {
if (currentTestIsABrowserTest()) {
takeRecord(MANDATORY_SCREENSHOT, result, screenshots);
}
}

private void takeEndOfStepScreenshotForPlayback(final TestResult result, List<ScreenshotAndHtmlSource> screenshots) {
if ((screenshots != null && screenshots.size() > 0)) {
allScreenshots.addAll(screenshots);
if ((screenshots != null && screenshots.size() > 0) && shouldTakeEndOfStepScreenshotFor(result)) {
takePlayback(MANDATORY_SCREENSHOT, result, screenshots);
usedScreenshots.addAll(screenshots);
}
}

Expand Down Expand Up @@ -1167,6 +1173,20 @@ private void removeFirstScreenshotOfCurrentStep() {
getCurrentStep().removeScreenshot(0);
}

private void removeUnnecesaryScreenshotsFromOutputDir() {
usedScreenshots.forEach(usedScreenshot -> allScreenshots =
allScreenshots.stream().filter(scr -> !scr.getScreenshotName().equals(usedScreenshot.getScreenshotName()))
.collect(Collectors.toList()));
allScreenshots.forEach(this::removeScreenshot);
}


private void removeScreenshot(ScreenshotAndHtmlSource screenshot) {
if(screenshot.getScreenshot().delete() && currentStep().isPresent()) {
getCurrentStep().getScreenshots().remove(screenshot);
}
}

private boolean currentStepHasMoreThanOneScreenshot() {
return currentStepExists() && getCurrentStep().getScreenshotCount() > 1;
}
Expand Down Expand Up @@ -1322,14 +1342,15 @@ public void testIgnored() {
if (!testOutcomeRecorded()) {
return;
}
removeUnnecesaryScreenshotsFromOutputDir();
getCurrentTestOutcome().setAnnotatedResult(IGNORED);
}

public void testSkipped() {
if (!testOutcomeRecorded()) {
return;
}

removeUnnecesaryScreenshotsFromOutputDir();
getCurrentTestOutcome().setAnnotatedResult(SKIPPED);
}

Expand All @@ -1338,15 +1359,15 @@ public void testAborted() {
if (!testOutcomeRecorded()) {
return;
}

removeUnnecesaryScreenshotsFromOutputDir();
getCurrentTestOutcome().setAnnotatedResult(ABORTED);
}

public void testPending() {
if (!testOutcomeRecorded()) {
return;
}

removeUnnecesaryScreenshotsFromOutputDir();
getCurrentTestOutcome().setAnnotatedResult(PENDING);
updateExampleTableIfNecessary(PENDING);
}
Expand Down

0 comments on commit f4fd076

Please sign in to comment.