|
| 1 | +/* |
| 2 | + * Copyright 2021 Qameta Software OÜ |
| 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.karate; |
| 17 | + |
| 18 | +import com.intuit.karate.RuntimeHook; |
| 19 | +import com.intuit.karate.core.Feature; |
| 20 | +import com.intuit.karate.core.Result; |
| 21 | +import com.intuit.karate.core.Scenario; |
| 22 | +import com.intuit.karate.core.ScenarioResult; |
| 23 | +import com.intuit.karate.core.ScenarioRuntime; |
| 24 | +import com.intuit.karate.core.Step; |
| 25 | +import com.intuit.karate.core.StepResult; |
| 26 | +import io.qameta.allure.Allure; |
| 27 | +import io.qameta.allure.AllureLifecycle; |
| 28 | +import io.qameta.allure.model.Stage; |
| 29 | +import io.qameta.allure.model.Status; |
| 30 | +import io.qameta.allure.model.StatusDetails; |
| 31 | +import io.qameta.allure.model.TestResult; |
| 32 | +import io.qameta.allure.util.ResultsUtils; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.io.BufferedInputStream; |
| 37 | +import java.io.FileInputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Objects; |
| 42 | +import java.util.Optional; |
| 43 | +import java.util.UUID; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author charlie (Dmitry Baev). |
| 47 | + */ |
| 48 | +public class AllureKarate implements RuntimeHook { |
| 49 | + |
| 50 | + private static final Logger LOGGER = LoggerFactory.getLogger(AllureKarate.class); |
| 51 | + |
| 52 | + private static final String ALLURE_UUID = "ALLURE_UUID"; |
| 53 | + |
| 54 | + private AllureLifecycle lifecycle; |
| 55 | + |
| 56 | + public AllureKarate() { |
| 57 | + this(Allure.getLifecycle()); |
| 58 | + } |
| 59 | + |
| 60 | + public AllureKarate(final AllureLifecycle lifecycle) { |
| 61 | + this.lifecycle = lifecycle; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean beforeScenario(final ScenarioRuntime sr) { |
| 66 | + final Feature feature = sr.featureRuntime.feature; |
| 67 | + final String featureName = feature.getName(); |
| 68 | + final String featureNameQualified = feature.getPackageQualifiedName(); |
| 69 | + final Scenario scenario = sr.scenario; |
| 70 | + final String scenarioName = scenario.getName(); |
| 71 | + LOGGER.info("tags: {}", sr.tags.getTagValues()); |
| 72 | + |
| 73 | + final String uuid = UUID.randomUUID().toString(); |
| 74 | + sr.magicVariables.put(ALLURE_UUID, uuid); |
| 75 | + |
| 76 | + final TestResult result = new TestResult() |
| 77 | + .setUuid(uuid) |
| 78 | + .setFullName(String.format("%s | %s", featureNameQualified, scenarioName)) |
| 79 | + .setName(scenarioName) |
| 80 | + .setDescription(scenario.getDescription()) |
| 81 | + .setTestCaseId(scenario.getUniqueId()) |
| 82 | + .setStage(Stage.RUNNING) |
| 83 | + .setLabels(List.of( |
| 84 | + ResultsUtils.createFeatureLabel(featureName) |
| 85 | + )); |
| 86 | + |
| 87 | + lifecycle.scheduleTestCase(result); |
| 88 | + lifecycle.startTestCase(uuid); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void afterScenario(final ScenarioRuntime sr) { |
| 94 | + final String uuid = (String) sr.magicVariables.get(ALLURE_UUID); |
| 95 | + if (Objects.isNull(uuid)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + final Optional<ScenarioResult> maybeResult = Optional.of(sr) |
| 99 | + .map(s -> s.result); |
| 100 | + |
| 101 | + final Status status = !sr.isFailed() |
| 102 | + ? Status.PASSED |
| 103 | + : maybeResult |
| 104 | + .map(ScenarioResult::getError) |
| 105 | + .flatMap(ResultsUtils::getStatus) |
| 106 | + .orElse(null); |
| 107 | + |
| 108 | + final StatusDetails statusDetails = maybeResult |
| 109 | + .map(ScenarioResult::getError) |
| 110 | + .flatMap(ResultsUtils::getStatusDetails) |
| 111 | + .orElse(null); |
| 112 | + |
| 113 | + lifecycle.updateTestCase(uuid, tr -> { |
| 114 | + tr.setStage(Stage.FINISHED); |
| 115 | + tr.setStatus(status); |
| 116 | + tr.setStatusDetails(statusDetails); |
| 117 | + }); |
| 118 | + |
| 119 | + lifecycle.stopTestCase(uuid); |
| 120 | + lifecycle.writeTestCase(uuid); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean beforeStep(final Step step, |
| 125 | + final ScenarioRuntime sr) { |
| 126 | + final String parentUuid = (String) sr.magicVariables.get(ALLURE_UUID); |
| 127 | + if (Objects.isNull(parentUuid)) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + final String uuid = parentUuid + "-" + step.getIndex(); |
| 132 | + final io.qameta.allure.model.StepResult stepResult = new io.qameta.allure.model.StepResult() |
| 133 | + .setName(step.getText()); |
| 134 | + |
| 135 | + lifecycle.startStep(parentUuid, uuid, stepResult); |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void afterStep(final StepResult result, |
| 142 | + final ScenarioRuntime sr) { |
| 143 | + final String parentUuid = (String) sr.magicVariables.get(ALLURE_UUID); |
| 144 | + if (Objects.isNull(parentUuid)) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + final Step step = result.getStep(); |
| 149 | + final String uuid = parentUuid + "-" + step.getIndex(); |
| 150 | + |
| 151 | + |
| 152 | + final Result stepResult = result.getResult(); |
| 153 | + |
| 154 | + final Status status = !stepResult.isFailed() |
| 155 | + ? Status.PASSED |
| 156 | + : Optional.of(stepResult) |
| 157 | + .map(Result::getError) |
| 158 | + .flatMap(ResultsUtils::getStatus) |
| 159 | + .orElse(null); |
| 160 | + |
| 161 | + final StatusDetails statusDetails = Optional.of(stepResult) |
| 162 | + .map(Result::getError) |
| 163 | + .flatMap(ResultsUtils::getStatusDetails) |
| 164 | + .orElse(null); |
| 165 | + |
| 166 | + result.getEmbeds().forEach(embed -> { |
| 167 | + try (InputStream is = new BufferedInputStream(new FileInputStream(embed.getFile()))) { |
| 168 | + lifecycle.addAttachment( |
| 169 | + embed.getFile().getName(), |
| 170 | + embed.getResourceType().contentType, |
| 171 | + embed.getResourceType().getExtension(), |
| 172 | + is |
| 173 | + ); |
| 174 | + } catch (IOException e) { |
| 175 | + LOGGER.warn("could not save embedding", e); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + lifecycle.updateStep(uuid, s -> { |
| 180 | + s.setStatus(status); |
| 181 | + s.setStatusDetails(statusDetails); |
| 182 | + }); |
| 183 | + lifecycle.stopStep(uuid); |
| 184 | + |
| 185 | + } |
| 186 | +} |
0 commit comments