Skip to content

Commit 85eb2cf

Browse files
committed
fixes #3
1 parent 251e0cc commit 85eb2cf

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/main/java/com/aventstack/extentreports/cucumber/adapter/ExtentCucumberAdapter.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import com.aventstack.extentreports.GherkinKeyword;
1717
import com.aventstack.extentreports.MediaEntityBuilder;
1818
import com.aventstack.extentreports.Status;
19+
import com.aventstack.extentreports.gherkin.model.Given;
1920
import com.aventstack.extentreports.markuputils.MarkupHelper;
2021
import com.aventstack.extentreports.service.ExtentService;
2122

2223
import cucumber.api.PickleStepTestStep;
2324
import cucumber.api.Result;
2425
import cucumber.api.TestCase;
26+
import cucumber.api.TestStep;
2527
import cucumber.api.event.EmbedEvent;
2628
import cucumber.api.event.EventHandler;
2729
import cucumber.api.event.EventPublisher;
@@ -155,15 +157,48 @@ private synchronized void handleTestCaseStarted(TestCaseStarted event) {
155157
}
156158
}
157159

160+
@SuppressWarnings("deprecation")
158161
private synchronized void handleTestStepStarted(TestStepStarted event) {
162+
if (event.testStep.isHook()) {
163+
ExtentTest t = scenarioThreadLocal.get()
164+
.createNode(Given.class, event.testStep.getCodeLocation());
165+
stepTestThreadLocal.set(t);
166+
}
159167
if (event.testStep instanceof PickleStepTestStep) {
160168
PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
161169
createTestStep(testStep);
162170
}
163171
}
164172

165173
private synchronized void handleTestStepFinished(TestStepFinished event) {
166-
updateResult(event.result);
174+
updateResult(event.testStep, event.result);
175+
}
176+
177+
private synchronized void updateResult(TestStep step, Result result) {
178+
switch (result.getStatus().lowerCaseName()) {
179+
case "failed":
180+
stepTestThreadLocal.get().fail(result.getError());
181+
break;
182+
case "skipped":
183+
case "pending":
184+
Boolean currentEndingEventSkipped = stepTestThreadLocal.get().getModel().getLogContext() != null
185+
&& !stepTestThreadLocal.get().getModel().getLogContext().isEmpty()
186+
? stepTestThreadLocal.get().getModel().getLogContext().getLast().getStatus() == Status.SKIP
187+
: false;
188+
if (result.getError() != null) {
189+
stepTestThreadLocal.get().skip(result.getError());
190+
} else if (!currentEndingEventSkipped) {
191+
String details = result.getErrorMessage() == null ? "Step skipped" : result.getErrorMessage();
192+
stepTestThreadLocal.get().skip(details);
193+
}
194+
break;
195+
case "passed":
196+
if (stepTestThreadLocal.get()!= null && stepTestThreadLocal.get().getModel().getLogContext().isEmpty())
197+
stepTestThreadLocal.get().pass("");
198+
break;
199+
default:
200+
break;
201+
}
167202
}
168203

169204
private synchronized void handleEmbed(EmbedEvent event) {
@@ -214,7 +249,12 @@ private URL toUrl(String fileName) {
214249
}
215250
}
216251

217-
private void handleWrite(WriteEvent event) { }
252+
private void handleWrite(WriteEvent event) {
253+
String text = event.text;
254+
if (text != null && !text.isEmpty()) {
255+
stepTestThreadLocal.get().info(text);
256+
}
257+
}
218258

219259
private void finishReport() {
220260
ExtentService.getInstance().flush();
@@ -393,31 +433,4 @@ private Map<String, Object> createDocStringMap(PickleString docString) {
393433
return docStringMap;
394434
}
395435

396-
private synchronized void updateResult(Result result) {
397-
switch (result.getStatus().lowerCaseName()) {
398-
case "failed":
399-
stepTestThreadLocal.get().fail(result.getError());
400-
break;
401-
case "skipped":
402-
case "pending":
403-
Boolean currentEndingEventSkipped = stepTestThreadLocal.get().getModel().getLogContext() != null
404-
&& !stepTestThreadLocal.get().getModel().getLogContext().isEmpty()
405-
? stepTestThreadLocal.get().getModel().getLogContext().getLast().getStatus() == Status.SKIP
406-
: false;
407-
if (result.getError() != null) {
408-
stepTestThreadLocal.get().skip(result.getError());
409-
} else if (!currentEndingEventSkipped) {
410-
String details = result.getErrorMessage() == null ? "Step skipped" : result.getErrorMessage();
411-
stepTestThreadLocal.get().skip(details);
412-
}
413-
break;
414-
case "passed":
415-
if (stepTestThreadLocal.get()!= null && stepTestThreadLocal.get().getModel().getLogContext().isEmpty())
416-
stepTestThreadLocal.get().pass("");
417-
break;
418-
default:
419-
break;
420-
}
421-
}
422-
423436
}

0 commit comments

Comments
 (0)