All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qa.justtestlah.log.CucumberLoggingPlugin Maven / Gradle / Ivy

Go to download

JustTestLah! is a JAVA test framework targeting projects that support multiple platforms, in particular Web, Android and iOS. It follows a BDD approach and allows testing against all platforms using the same feature files. JustTestLah's main aim is to make the configuration and the actual test code as easy as possible.

There is a newer version: 1.9-RC4
Show newest version
package qa.justtestlah.log;

import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.Step;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseFinished;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestStep;
import io.cucumber.plugin.event.TestStepStarted;
import qa.justtestlah.utils.SpringContext;

/** Cucumber plugin to write scenario- and step-based log messages. */
public class CucumberLoggingPlugin implements ConcurrentEventListener {

  private EventHandler caseStartedHandler =
      new EventHandler() {
        @Override
        public void receive(TestCaseStarted event) {
          TestCase testCase = event.getTestCase();
          SpringContext.getBean(TestLogWriter.class)
              .log(
                  LogLevel.INFO,
                  TestLogWriter.CUCUMBER_SCENARIO_INDENTATION,
                  "Scenario: {} ({}:{})",
                  testCase.getName(),
                  testCase.getUri(),
                  testCase.getLine());
        }
      };

  private EventHandler stepStartedHandler =
      new EventHandler() {
        @Override
        public void receive(TestStepStarted event) {
          TestStep step = event.getTestStep();
          if (step instanceof PickleStepTestStep) {
            Step pickle = ((PickleStepTestStep) step).getStep();
            SpringContext.getBean(TestLogWriter.class)
                .log(
                    LogLevel.INFO,
                    TestLogWriter.CUCUMBER_STEP_INDENTATION,
                    "Step: {}",
                    pickle.getText(),
                    pickle.getLine());
          }
        }
      };

  private EventHandler caseFinishedHandler =
      new EventHandler() {
        @Override
        public void receive(TestCaseFinished event) {
          Result result = event.getResult();
          Throwable error = result.getError();
          if (error == null) {
            SpringContext.getBean(TestLogWriter.class)
                .log(
                    LogLevel.INFO,
                    TestLogWriter.CUCUMBER_SCENARIO_INDENTATION,
                    "[{}] Scenario \"{}\" finished after {} seconds\n",
                    result.getStatus(),
                    event.getTestCase().getName(),
                    result.getDuration().toSeconds());
          } else {
            SpringContext.getBean(TestLogWriter.class)
                .log(
                    LogLevel.INFO,
                    TestLogWriter.CUCUMBER_SCENARIO_INDENTATION,
                    "[{}] Scenario \"{}\" finished after {} seconds with error \"{}\"\n",
                    result.getStatus(),
                    event.getTestCase().getName(),
                    result.getDuration().toSeconds(),
                    error.getMessage().replaceAll("[\\t\\n\\r]+", " "));
          }
        }
      };

  @Override
  public void setEventPublisher(EventPublisher publisher) {
    publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
    publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
    publisher.registerHandlerFor(TestCaseFinished.class, caseFinishedHandler);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy