Skip to content

Instantly share code, notes, and snippets.

@Hrissimir
Created February 12, 2020 07:04
Show Gist options
  • Save Hrissimir/f3026cd0b5bf165ecbb5438646ac5263 to your computer and use it in GitHub Desktop.
Save Hrissimir/f3026cd0b5bf165ecbb5438646ac5263 to your computer and use it in GitHub Desktop.
package cucumber_selenium.plugins;
import io.cucumber.plugin.event.HookTestStep;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.TestCaseFinished;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestStepStarted;
import io.cucumber.plugin.event.TestStepFinished;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
interface Output {
void write(String msg);
default void write(String msg, Object... formatArgs) {
write(String.format(msg, formatArgs));
}
}
class ConsoleOutput implements Output {
@Override
public void write(String msg) {
System.out.println(msg);
}
}
class FileOutput implements Output {
private final PrintWriter writer;
public FileOutput(String filename) throws FileNotFoundException {
this.writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(filename, true)), true);
}
@Override
public void write(String msg) {
writer.println(msg);
writer.flush();
}
}
public class EventLoggingPlugin extends EventListenerPlugin {
private static final String OUTPUT_FILE = "cucumber_events.log";
private final Output out;
public EventLoggingPlugin() {
Output output = null;
try {
output = new FileOutput(OUTPUT_FILE);
} catch (FileNotFoundException e) {
output = new ConsoleOutput();
}
out = output;
}
@Override
public void onTestRunStarted(TestRunStarted event) {
out.write("Execution started: %s", event.getInstant());
}
@Override
public void onTestCaseStarted(TestCaseStarted event) {
out.write("Test started : '%s'", event.getTestCase().getName());
}
@Override
public void onHookStepStarted(TestStepStarted event) {
HookTestStep step = (HookTestStep) event.getTestStep();
out.write("Hook started : type=%s code=%s", step.getHookType(), step.getCodeLocation());
}
@Override
public void onHookStepFinished(TestStepFinished event) {
HookTestStep step = (HookTestStep) event.getTestStep();
out.write("Hook finished: type=%s status=%s code=%s ", step.getHookType(), event.getResult().getStatus(),
step.getCodeLocation());
}
@Override
public void onTestStepStarted(TestStepStarted event) {
PickleStepTestStep step = (PickleStepTestStep) event.getTestStep();
out.write("Step started : text='%s' code=%s", step.getStep().getText(), step.getCodeLocation());
}
@Override
public void onTestStepFinished(TestStepFinished event) {
PickleStepTestStep step = (PickleStepTestStep) event.getTestStep();
out.write("Step finished: status=%s text='%s'", event.getResult().getStatus(), step.getStep().getText());
}
@Override
public void onTestCaseFinished(TestCaseFinished event) {
out.write("Test finished: status=%s name='%s'", event.getResult().getStatus(), event.getTestCase().getName());
}
@Override
public void onTestRunFinished(TestRunFinished event) {
out.write("Execution finished: %s", event.getInstant());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment