Skip to content

Instantly share code, notes, and snippets.

@dipakkumar1225
Created October 28, 2023 15:50
Show Gist options
  • Save dipakkumar1225/44a398c9d239eaa691b30baac61eb742 to your computer and use it in GitHub Desktop.
Save dipakkumar1225/44a398c9d239eaa691b30baac61eb742 to your computer and use it in GitHub Desktop.
CucumberListener
public class CucumberListener implements ConcurrentEventListener {
public static String stepName;
AtomicInteger atomicInteger = new AtomicInteger(1);
@Override
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(TestRunStarted.class, this::beforeTestRun);
eventPublisher.registerHandlerFor(TestSourceRead.class,
this::readFeatureFile);
eventPublisher.registerHandlerFor(TestRunFinished.class, this::afterTestRun);
eventPublisher.registerHandlerFor(TestCaseStarted.class, this::testCaseStarted);
eventPublisher.registerHandlerFor(TestCaseFinished.class, this::testCaseFinished);
eventPublisher.registerHandlerFor(TestStepStarted.class, this::stepStarted);
eventPublisher.registerHandlerFor(TestStepFinished.class, this::stepFinished);
}
private void beforeTestRun(TestRunStarted testRunStarted) {
System.out.println("TEST RUN STARTED : ");
}
private final HashMap<String, Integer> totalScenariosForFeature = new HashMap<>();
List<String> tempFeatureFileList = new ArrayList<>();
private void readFeatureFile(TestSourceRead testSourceRead) {
String featureLocation = testSourceRead.getUri().toString();
System.out.println("FEATURE FILE PATH : " + featureLocation);
int scenarioCount = countScenariosInFeature(testSourceRead.getSource());
totalScenariosForFeature.put(featureLocation, scenarioCount);
tempFeatureFileList.add(featureLocation);
}
List<String> tempTestCaseList = new ArrayList<>();
String previousFeatureFile = null;
private String currentFeatureFile = null;
private final HashMap<String, Integer> featureScenarioCount = new HashMap<>();
private void testCaseStarted(TestCaseStarted testCaseStarted) {
System.out.println("Feature Files " + tempFeatureFileList);
currentFeatureFile = testCaseStarted.getTestCase().getUri().toString();
if (!currentFeatureFile.equals(previousFeatureFile)) {
System.out.println("NEW FEATURE FILE STARTED: " + currentFeatureFile);
previousFeatureFile = currentFeatureFile;
atomicInteger.set(1);
}
featureScenarioCount.put(currentFeatureFile, featureScenarioCount.getOrDefault(currentFeatureFile, 0));
String featureFileName = currentFeatureFile.substring(currentFeatureFile.indexOf(':') + 1);
tempTestCaseList.add(featureFileName);
System.out.println(System.lineSeparator() + "[" + atomicInteger.getAndIncrement() + "] STARTING SCENARIO : " + testCaseStarted.getTestCase().getName() + " #" + featureFileName + ":" + testCaseStarted.getTestCase().getLocation().getLine());
System.out.println("TAGS PRESENT : " + testCaseStarted.getTestCase().getTags().stream().parallel().collect(Collectors.toList()));
}
private void stepStarted(TestStepStarted testStepStarted) {
if (testStepStarted.getTestStep() instanceof PickleStepTestStep) {
final PickleStepTestStep ev = (PickleStepTestStep) testStepStarted.getTestStep();
final String args = StringUtils.join(ev.getDefinitionArgument().stream().map(Argument::getValue).toArray(), ",");
stepName = ev.getStep().getText();
if (StringUtils.isNotBlank(args)) {
stepName += (" : arguments in this steps = (" + args + ")");
}
System.out.println("STARTING STEP : " + stepName);
}
}
private void stepFinished(TestStepFinished testStepFinished) {
if (testStepFinished.getTestStep() instanceof PickleStepTestStep) {
System.out.println("STEP FINISHED : " + ((PickleStepTestStep) testStepFinished.getTestStep()).getStep().getText());
switch (testStepFinished.getResult().getStatus()) {
case PASSED -> onPassedStep(testStepFinished);
case SKIPPED -> onSkippedStep(testStepFinished);
case PENDING -> System.out.println("PENDING");
case UNDEFINED -> System.out.println("UNDEFINED");
case AMBIGUOUS -> System.out.println("AMBIGUOUS");
case FAILED -> onFailedStep(testStepFinished);
case UNUSED -> System.out.println("UNUSED");
}
}
}
public void onPassedStep(TestStepFinished testStepPassed) {
Status strStatus = testStepPassed.getResult().getStatus();
System.out.println("STEP STATUS : " + strStatus);
}
public void onSkippedStep(TestStepFinished testStepSkipped) {
Status strStatus = testStepSkipped.getResult().getStatus();
System.out.println("STEP STATUS : " + strStatus);
}
public void onFailedStep(TestStepFinished testStepFailed) {
Status strStatus = testStepFailed.getResult().getStatus();
String strReason = testStepFailed.getResult().getError().getMessage();
System.out.println("STEP STATUS : " + strStatus + " " + strReason);
}
private int countScenariosInFeature(String featureContent) {
int count = 0;
String[] lines = featureContent.split(System.lineSeparator());
for (String line : lines) {
line = line.trim();
if (line.startsWith("Scenario:") || line.startsWith("Scenario Outline:")) {
count++;
}
}
return count;
}
private void testCaseFinished(TestCaseFinished testCaseFinished) {
String strTestName = testCaseFinished.getTestCase().getName();
System.out.println("FINISHED SCENARIO : " + strTestName);
int executedScenarios = (atomicInteger.get() - 1);
System.out.println("executedScenarios " + executedScenarios);
int totalScenarios = totalScenariosForFeature.get(currentFeatureFile);
System.out.println("totalScenarios " + totalScenarios);
if (executedScenarios == totalScenarios) {
System.out.println("FEATURE FILE COMPLETED: " + currentFeatureFile);
}
switch (testCaseFinished.getResult().getStatus()) {
case PASSED -> onPassedTest(testCaseFinished);
case SKIPPED -> onSkippedTest(testCaseFinished);
case PENDING -> System.out.println("PENDING");
case UNDEFINED -> System.out.println("UNDEFINED");
case AMBIGUOUS -> System.out.println("AMBIGUOUS");
case FAILED -> onFailedTest(testCaseFinished);
case UNUSED -> System.out.println("UNUSED");
}
}
public void onPassedTest(TestCaseFinished testCasePassed) {
Status strStatus = testCasePassed.getResult().getStatus();
System.out.println("SCENARIO STATUS : " + strStatus);
}
public void onSkippedTest(TestCaseFinished testCaseSkipped) {
Status strStatus = testCaseSkipped.getResult().getStatus();
String strReason = testCaseSkipped.getResult().getError().getMessage();
System.out.println("SCENARIO STATUS : " + strStatus + " " + strReason);
}
public void onFailedTest(TestCaseFinished testCaseFailed) {
Status strStatus = testCaseFailed.getResult().getStatus();
String strReason = testCaseFailed.getResult().getError().getMessage();
System.out.println("SCENARIO STATUS : " + strStatus + " " + strReason);
}
private void afterTestRun(TestRunFinished testRunFinished) {
System.out.println("END Of TEST CASE : " + testRunFinished.getResult());
System.out.println("END Of TEST CASE ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment