|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 Qameta Software Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.qameta.allure.core; |
| 17 | + |
| 18 | +import io.qameta.allure.Aggregator2; |
| 19 | +import io.qameta.allure.ReportGenerationException; |
| 20 | +import io.qameta.allure.ReportStorage; |
| 21 | +import io.qameta.allure.entity.Attachment; |
| 22 | +import io.qameta.allure.entity.StageResult; |
| 23 | +import io.qameta.allure.entity.Step; |
| 24 | +import io.qameta.allure.entity.TestResult; |
| 25 | +import org.apache.commons.io.IOUtils; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * Plugin that stores Playwright Trace Viewer assets when the report contains |
| 35 | + * Playwright trace attachments. |
| 36 | + */ |
| 37 | +public class PlaywrightTraceViewerPlugin implements Aggregator2 { |
| 38 | + |
| 39 | + static final String PLAYWRIGHT_TRACE_MIME_TYPE = "application/vnd.allure.playwright-trace"; |
| 40 | + static final String PLAYWRIGHT_TRACE_VIEWER_URL = "playwright-trace-viewer/index.html"; |
| 41 | + static final String PLAYWRIGHT_TRACE_VIEWER_INFO_JSON = "data/playwright-trace-viewer.json"; |
| 42 | + |
| 43 | + private static final String PLAYWRIGHT_TRACE_VIEWER_ASSETS_JSON = "playwright-trace-viewer-assets.json"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void aggregate(final Configuration configuration, |
| 47 | + final List<LaunchResults> launchesResults, |
| 48 | + final ReportStorage storage) { |
| 49 | + if (storage instanceof InMemoryReportStorage || !hasAnyPlaywrightTraces(launchesResults)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + StaticAssetManifest.load(PLAYWRIGHT_TRACE_VIEWER_ASSETS_JSON) |
| 54 | + .forEach(resourceName -> storage.addDataBinary(resourceName, readResource(resourceName))); |
| 55 | + storage.addDataJson( |
| 56 | + PLAYWRIGHT_TRACE_VIEWER_INFO_JSON, |
| 57 | + Collections.singletonMap("url", PLAYWRIGHT_TRACE_VIEWER_URL) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + static boolean hasAnyPlaywrightTraces(final List<LaunchResults> launchesResults) { |
| 62 | + return launchesResults.stream() |
| 63 | + .flatMap(launch -> launch.getAllResults().stream()) |
| 64 | + .anyMatch(PlaywrightTraceViewerPlugin::hasTraceInResult); |
| 65 | + } |
| 66 | + |
| 67 | + private static boolean hasTraceInResult(final TestResult result) { |
| 68 | + return hasTraceInStages(result.getBeforeStages()) |
| 69 | + || hasTraceInStage(result.getTestStage()) |
| 70 | + || hasTraceInStages(result.getAfterStages()); |
| 71 | + } |
| 72 | + |
| 73 | + private static boolean hasTraceInStages(final List<StageResult> stages) { |
| 74 | + return stages != null && stages.stream().anyMatch(PlaywrightTraceViewerPlugin::hasTraceInStage); |
| 75 | + } |
| 76 | + |
| 77 | + private static boolean hasTraceInStage(final StageResult stage) { |
| 78 | + return stage != null |
| 79 | + && (hasTraceInAttachments(stage.getAttachments()) |
| 80 | + || hasTraceInSteps(stage.getSteps())); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean hasTraceInSteps(final List<Step> steps) { |
| 84 | + return steps != null && steps.stream().anyMatch(PlaywrightTraceViewerPlugin::hasTraceInStep); |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean hasTraceInStep(final Step step) { |
| 88 | + return step != null |
| 89 | + && (hasTraceInAttachments(step.getAttachments()) |
| 90 | + || hasTraceInSteps(step.getSteps())); |
| 91 | + } |
| 92 | + |
| 93 | + private static boolean hasTraceInAttachments(final List<Attachment> attachments) { |
| 94 | + return attachments != null && attachments.stream().anyMatch(PlaywrightTraceViewerPlugin::isTraceAttachment); |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean isTraceAttachment(final Attachment attachment) { |
| 98 | + return attachment != null && PLAYWRIGHT_TRACE_MIME_TYPE.equals(attachment.getType()); |
| 99 | + } |
| 100 | + |
| 101 | + @SuppressWarnings("PMD.ExceptionAsFlowControl") |
| 102 | + private static byte[] readResource(final String resourceName) { |
| 103 | + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName)) { |
| 104 | + if (Objects.isNull(is)) { |
| 105 | + throw new ReportGenerationException(String.format("Resource %s not found", resourceName)); |
| 106 | + } |
| 107 | + return IOUtils.toByteArray(is); |
| 108 | + } catch (IOException e) { |
| 109 | + throw new ReportGenerationException("Can't read resource " + resourceName, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments