
com.automationrockstars.bmo.AllureStoryReporter Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015, 2016 Automation RockStars Ltd.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License v2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* Automation RockStars - initial API and implementation
*******************************************************************************/
package com.automationrockstars.bmo;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.GivenStories;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Meta;
import org.jbehave.core.model.Narrative;
import org.jbehave.core.model.OutcomesTable;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
import org.jbehave.core.model.StoryDuration;
import org.jbehave.core.reporters.StoryReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.automationrockstars.asserts.Asserts;
import com.automationrockstars.base.ConfigLoader;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ru.yandex.qatools.allure.Allure;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.events.MakeAttachmentEvent;
import ru.yandex.qatools.allure.events.StepCanceledEvent;
import ru.yandex.qatools.allure.events.StepFailureEvent;
import ru.yandex.qatools.allure.events.StepFinishedEvent;
import ru.yandex.qatools.allure.events.StepStartedEvent;
import ru.yandex.qatools.allure.events.TestCaseFailureEvent;
import ru.yandex.qatools.allure.events.TestCaseFinishedEvent;
import ru.yandex.qatools.allure.events.TestCaseStartedEvent;
import ru.yandex.qatools.allure.events.TestSuiteFinishedEvent;
import ru.yandex.qatools.allure.events.TestSuiteStartedEvent;
import ru.yandex.qatools.allure.model.Description;
import ru.yandex.qatools.allure.report.AllureReportBuilder;
import ru.yandex.qatools.allure.report.AllureReportBuilderException;
public class AllureStoryReporter implements StoryReporter {
private static Logger LOG = LoggerFactory.getLogger(AllureStoryReporter.class);
private void setLogger(){
if ( ! LoggerFactory.getILoggerFactory().getClass().getName().contains("logback")){
return;
}
final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final PatternLayoutEncoder ple = new PatternLayoutEncoder();
String pattern = ConfigLoader.config().getString("allure.log.pattern","%date|%logger{8}|%level| %msg%n");
if (ConfigLoader.config().containsKey("allure.log.detailed")){
pattern = "%date %level [%thread] %logger{10} [%file:%line] %msg%n";
}
ple.setPattern(pattern);
ple.setContext(lc);
ple.start();
final Logger rootLogger = LoggerFactory.getLogger("ROOT");
if (rootLogger instanceof ch.qos.logback.classic.Logger){
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)rootLogger;
Appender> lame =null;
Iterator> appenders = logger.iteratorForAppenders();
while (appenders.hasNext()){
Appender> a = appenders.next();
if (a instanceof AllureLogbackAppender) {
lame = a;
}
}
if (lame == null){
AllureLogbackAppender appender = new AllureLogbackAppender<>();
appender.setEncoder(ple);
appender.setContext(lc);
appender.start();
logger.addAppender(appender);
}
}
}
public AllureStoryReporter() {
LOG.info("instantiating");
GenericAllureStoryReporter.cleanPreviousReport();
setLogger();
ConfigLoader.config().setProperty("assert.screenshot", false);
}
@Override
public void storyNotAllowed(Story story, String filter) {
LOG.info("Story not allowed {} {} ",story, filter);
}
@Override
public void storyCancelled(Story story, StoryDuration storyDuration) {
LOG.info("Story cancelled {} {} ",story, storyDuration);
}
private static final ThreadLocal currentSuite = new ThreadLocal<>();
private static final ThreadLocal currentSuiteName = new ThreadLocal<>();
public static String storyName(){
return currentSuiteName.get();
}
public static String scenarioName(){
return currentScenario.get();
}
@Override
public void beforeStory(Story story, boolean givenStory) {
String uuid = UUID.randomUUID().toString();
currentSuite.set(uuid);
TestSuiteStartedEvent storyEvent = new TestSuiteStartedEvent(uuid, story.getPath())
.withDescription(new Description()
.withValue(story.getDescription().asString()))
.withTitle(story.getName());
currentSuiteName.set(story.getName());
Allure.LIFECYCLE.fire(storyEvent);
}
@Override
public void afterStory(boolean givenOrRestartingStory) {
Allure.LIFECYCLE.fire(new TestSuiteFinishedEvent(currentSuite.get()));
}
@Override
public void narrative(Narrative narrative) {
}
@Override
public void lifecyle(Lifecycle lifecycle) {
}
@Override
public void scenarioNotAllowed(Scenario scenario, String filter) {
LOG.info("Scenario not allowed {} {}",scenario,filter);
}
public static final ThreadLocal currentScenario = new ThreadLocal<>();
private static final ThreadLocal originalThreadName = new ThreadLocal();
@Override
public void beforeScenario(String scenarioTitle) {
LOG.info("Starting scenario {}",scenarioTitle);
originalThreadName.set(Thread.currentThread().getName());
currentScenario.set(scenarioTitle);
Thread.currentThread().setName(scenarioTitle);
stepFailed = null;
TestCaseStartedEvent testCase = new TestCaseStartedEvent(currentSuite.get(), scenarioTitle);
if (! Strings.isNullOrEmpty(currentMeta.get().getProperty("feature"))){
testCase.withLabels(AllureModelUtils.createFeatureLabel(currentMeta.get().getProperty("feature")));
}
if (! Strings.isNullOrEmpty(currentMeta.get().getProperty("story"))){
testCase.withLabels(AllureModelUtils.createFeatureLabel(currentMeta.get().getProperty("story")));
}
Allure.LIFECYCLE.fire(testCase);
}
private static final ThreadLocal currentMeta = new ThreadLocal(){
protected Meta initialValue(){
return new Meta();
}
};
@Override
public void scenarioMeta(Meta meta) {
currentMeta.set(meta);
String feature = meta.getProperty("feature");
String story = meta.getProperty("story");
LOG.info("Scenario in feature {} and story {}",feature,story);
}
@Override
public void afterScenario() {
Thread.currentThread().setName(originalThreadName.get());
if (stepFailed != null){
Allure.LIFECYCLE.fire(new TestCaseFailureEvent().withThrowable(stepFailed));
}
Allure.LIFECYCLE.fire(new TestCaseFinishedEvent());
}
@Override
public void givenStories(GivenStories givenStories) {
LOG.info("Given stories {}",givenStories);
}
@Override
public void givenStories(List storyPaths) {
LOG.info("Given stories {}",storyPaths);
}
@Override
public void beforeExamples(List steps, ExamplesTable table) {
LOG.info("before examples {} {}",steps,table);
}
static ThreadLocal
© 2015 - 2025 Weber Informatics LLC | Privacy Policy