How to use SerenityReporter class of io.cucumber.core.plugin package

Best Serenity Cucumber code snippet using io.cucumber.core.plugin.SerenityReporter

Source:SerenityReporter.java Github

copy

Full Screen

...43 * Cucumber Formatter for Serenity.44 *45 * @author L.Carausu (liviu.carausu@gmail.com)46 */47public class SerenityReporter implements Plugin, ConcurrentEventListener {48    private static final String OPEN_PARAM_CHAR = "\uff5f";49    private static final String CLOSE_PARAM_CHAR = "\uff60";50    private static final String SCENARIO_OUTLINE_NOT_KNOWN_YET = "";51    private Configuration systemConfiguration;52    private final List<BaseStepListener> baseStepListeners;53    private final static String FEATURES_ROOT_PATH = "features";54    private FeatureFileLoader featureLoader = new FeatureFileLoader();55    private LineFilters lineFilters;56    private List<Tag> scenarioTags;57    private static final Logger LOGGER = LoggerFactory.getLogger(SerenityReporter.class);58    private ManualScenarioChecker manualScenarioDateChecker;59    private ThreadLocal<ScenarioContext> localContext = ThreadLocal.withInitial(ScenarioContext::new);60    private ScenarioContext getContext() {61        return localContext.get();62    }63    /**64     * Constructor automatically called by cucumber when class is specified as plugin65     * in @CucumberOptions.66     */67    public SerenityReporter() {68        this.systemConfiguration = Injectors.getInjector().getInstance(Configuration.class);69        this.manualScenarioDateChecker = new ManualScenarioChecker(systemConfiguration.getEnvironmentVariables());70        baseStepListeners = Collections.synchronizedList(new ArrayList<>());71        lineFilters = LineFilters.forCurrentContext();72    }73    public SerenityReporter(Configuration systemConfiguration) {74        this.systemConfiguration = systemConfiguration;75        this.manualScenarioDateChecker = new ManualScenarioChecker(systemConfiguration.getEnvironmentVariables());76        baseStepListeners = Collections.synchronizedList(new ArrayList<>());77        lineFilters = LineFilters.forCurrentContext();78    }79    private FeaturePathFormatter featurePathFormatter = new FeaturePathFormatter();80    private StepEventBus getStepEventBus(URI featurePath) {81        URI prefixedPath = featurePathFormatter.featurePathWithPrefixIfNecessary(featurePath);82        return StepEventBus.eventBusFor(prefixedPath);83    }84    private void setStepEventBus(URI featurePath) {85        URI prefixedPath = featurePathFormatter.featurePathWithPrefixIfNecessary(featurePath);86        StepEventBus.setCurrentBusToEventBusFor(prefixedPath);87    }...

Full Screen

Full Screen

Source:CucumberSerenityRunner.java Github

copy

Full Screen

...7import io.cucumber.core.gherkin.Pickle;8import io.cucumber.core.options.*;9import io.cucumber.core.plugin.PluginFactory;10import io.cucumber.core.plugin.Plugins;11import io.cucumber.core.plugin.SerenityReporter;12import io.cucumber.core.resource.ClassLoaders;13import io.cucumber.core.runtime.Runtime;14import io.cucumber.core.runtime.*;15import io.cucumber.plugin.Plugin;16import io.cucumber.tagexpressions.Expression;17import net.serenitybdd.cucumber.suiteslicing.CucumberSuiteSlicer;18import net.serenitybdd.cucumber.suiteslicing.ScenarioFilter;19import net.serenitybdd.cucumber.suiteslicing.TestStatistics;20import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios;21import net.serenitybdd.cucumber.util.PathUtils;22import net.serenitybdd.cucumber.util.Splitter;23import net.thucydides.core.ThucydidesSystemProperty;24import net.thucydides.core.guice.Injectors;25import net.thucydides.core.util.EnvironmentVariables;26import net.thucydides.core.webdriver.Configuration;27import org.junit.runner.Description;28import org.junit.runner.manipulation.NoTestsRemainException;29import org.junit.runner.notification.RunNotifier;30import org.junit.runners.ParentRunner;31import org.junit.runners.model.InitializationError;32import org.junit.runners.model.RunnerScheduler;33import org.junit.runners.model.Statement;34import org.slf4j.Logger;35import org.slf4j.LoggerFactory;36import java.io.File;37import java.net.URI;38import java.time.Clock;39import java.util.*;40import java.util.concurrent.atomic.AtomicInteger;41import java.util.function.Function;42import java.util.function.Predicate;43import java.util.function.Supplier;44import static io.cucumber.junit.FileNameCompatibleNames.uniqueSuffix;45import static java.util.stream.Collectors.groupingBy;46import static java.util.stream.Collectors.toList;47import static net.thucydides.core.ThucydidesSystemProperty.*;48/**49 * Glue code for running Cucumber via Serenity.50 * Sets up Serenity reporting and instrumentation.51 */52public class CucumberSerenityRunner extends ParentRunner<ParentRunner<?>> {53    private static final Logger LOGGER = LoggerFactory.getLogger(CucumberSerenityRunner.class);54    private List<ParentRunner<?>> children = new ArrayList<ParentRunner<?>>();55    private final EventBus bus;56    private static ThreadLocal<RuntimeOptions> RUNTIME_OPTIONS = new ThreadLocal<>();57    private final List<Feature> features;58    private final Plugins plugins;59    private final CucumberExecutionContext context;60    private boolean multiThreadingAssumed = false;61    /**62     * Constructor called by JUnit.63     *64     * @param clazz the class with the @RunWith annotation.65     * @throws InitializationError if there is another problem66     */67    public CucumberSerenityRunner(Class clazz) throws InitializationError {68        super(clazz);69        Assertions.assertNoCucumberAnnotatedMethods(clazz);70        // Parse the options early to provide fast feedback about invalid options71        RuntimeOptions propertiesFileOptions = new CucumberPropertiesParser()72                .parse(CucumberProperties.fromPropertiesFile())73                .build();74        RuntimeOptions annotationOptions = new CucumberOptionsAnnotationParser()75                .withOptionsProvider(new JUnitCucumberOptionsProvider())76                .parse(clazz)77                .build(propertiesFileOptions);78        RuntimeOptions environmentOptions = new CucumberPropertiesParser()79                .parse(CucumberProperties.fromEnvironment())80                .build(annotationOptions);81        RuntimeOptions runtimeOptions = new CucumberPropertiesParser()82                .parse(CucumberProperties.fromSystemProperties())83                .addDefaultSummaryPrinterIfAbsent()84                .build(environmentOptions);85        RuntimeOptionsBuilder runtimeOptionsBuilder =  new RuntimeOptionsBuilder();86        Collection<String> tagFilters = environmentSpecifiedTags(runtimeOptions.getTagExpressions());87        for(String tagFilter : tagFilters ) {88            runtimeOptionsBuilder.addTagFilter(new LiteralExpression(tagFilter));89        }90        runtimeOptionsBuilder.build(runtimeOptions);91        // Next parse the junit options92        JUnitOptions junitPropertiesFileOptions = new JUnitOptionsParser()93                .parse(CucumberProperties.fromPropertiesFile())94                .build();95        JUnitOptions junitAnnotationOptions = new JUnitOptionsParser()96                .parse(clazz)97                .build(junitPropertiesFileOptions);98        JUnitOptions junitEnvironmentOptions = new JUnitOptionsParser()99                .parse(CucumberProperties.fromEnvironment())100                .build(junitAnnotationOptions);101        JUnitOptions junitOptions = new JUnitOptionsParser()102                .parse(CucumberProperties.fromSystemProperties())103                //.setStrict(runtimeOptions.isStrict())104                .build(junitEnvironmentOptions);105        this.bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);106        setRuntimeOptions(runtimeOptions);107        // Parse the features early. Don't proceed when there are lexer errors108        FeatureParser parser = new FeatureParser(bus::generateId);109        Supplier<ClassLoader> classLoader = ClassLoaders::getDefaultClassLoader;110        FeaturePathFeatureSupplier featureSupplier = new FeaturePathFeatureSupplier(classLoader, runtimeOptions, parser);111        this.features = featureSupplier.get();112        // Create plugins after feature parsing to avoid the creation of empty files on lexer errors.113        this.plugins = new Plugins(new PluginFactory(), runtimeOptions);114        ExitStatus exitStatus = new ExitStatus(runtimeOptions);115        this.plugins.addPlugin(exitStatus);116        Configuration systemConfiguration = Injectors.getInjector().getInstance(Configuration.class);117        // customer change on 20201023118        systemConfiguration.setOutputDirectory(new File(System.getProperty(SerenityCucumberMain.REPORT_PROPERTY_NAME)));119        SerenityReporter reporter = new SerenityReporter(systemConfiguration);120        addSerenityReporterPlugin(plugins,reporter);121        ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(runtimeOptions);122        ObjectFactorySupplier objectFactorySupplier = new ThreadLocalObjectFactorySupplier(objectFactoryServiceLoader);123        BackendSupplier backendSupplier = new BackendServiceLoader(clazz::getClassLoader, objectFactorySupplier);124        TypeRegistryConfigurerSupplier typeRegistryConfigurerSupplier = new ScanningTypeRegistryConfigurerSupplier(classLoader, runtimeOptions);125        ThreadLocalRunnerSupplier runnerSupplier = new ThreadLocalRunnerSupplier(runtimeOptions, bus, backendSupplier, objectFactorySupplier, typeRegistryConfigurerSupplier);126        this.context = new CucumberExecutionContext(bus, exitStatus, runnerSupplier);127        Predicate<Pickle> filters = new Filters(runtimeOptions);128        Map<Optional<String>, List<Feature>> groupedByName = features.stream()129                .collect(groupingBy(Feature::getName));130        children = features.stream()131                .map(feature -> {132                    Integer uniqueSuffix = uniqueSuffix(groupedByName, feature, Feature::getName);133                    return FeatureRunner.create(feature, uniqueSuffix,filters, runnerSupplier, junitOptions);134                })135                .filter(runner -> !runner.isEmpty())136                .collect(toList());137    }138    private static RuntimeOptions DEFAULT_RUNTIME_OPTIONS;139    public static void setRuntimeOptions(RuntimeOptions runtimeOptions) {140        RUNTIME_OPTIONS.set(runtimeOptions);141        DEFAULT_RUNTIME_OPTIONS = runtimeOptions;142    }143    public static RuntimeOptions currentRuntimeOptions() {144        return (RUNTIME_OPTIONS.get() != null) ? RUNTIME_OPTIONS.get() : DEFAULT_RUNTIME_OPTIONS;145    }146    private static Collection<String> environmentSpecifiedTags(List<?> existingTags) {147        EnvironmentVariables environmentVariables = Injectors.getInjector().getInstance(EnvironmentVariables.class);148        String tagsExpression = ThucydidesSystemProperty.TAGS.from(environmentVariables,"");149        List<String> existingTagsValues = existingTags.stream().map(Object::toString).collect(toList());150        return Splitter.on(",").trimResults().omitEmptyStrings().splitToList(tagsExpression).stream()151                .map(CucumberSerenityRunner::toCucumberTag).filter(t -> !existingTagsValues.contains(t)).collect(toList());152    }153    private static String toCucumberTag(String from) {154        String tag = from.replaceAll(":","=");155        if (tag.startsWith("~@") || tag.startsWith("@")) { return tag; }156        if (tag.startsWith("~")) { return "~@" + tag.substring(1); }157        return "@" + tag;158    }159    public static Runtime createSerenityEnabledRuntime(/*ResourceLoader resourceLoader,*/160            Supplier<ClassLoader> classLoaderSupplier,161            RuntimeOptions runtimeOptions,162            Configuration systemConfiguration) {163        RuntimeOptionsBuilder runtimeOptionsBuilder = new RuntimeOptionsBuilder();164        Collection<String> allTagFilters = environmentSpecifiedTags(runtimeOptions.getTagExpressions());165        for(String tagFilter :  allTagFilters) {166            runtimeOptionsBuilder.addTagFilter(new LiteralExpression(tagFilter));167        }168        runtimeOptionsBuilder.build(runtimeOptions);169        setRuntimeOptions(runtimeOptions);170        EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);171        FeatureParser parser = new FeatureParser(bus::generateId);172        FeaturePathFeatureSupplier featureSupplier = new FeaturePathFeatureSupplier(classLoaderSupplier, runtimeOptions, parser);173        SerenityReporter serenityReporter = new SerenityReporter(systemConfiguration);174        Runtime runtime = Runtime.builder().withClassLoader(classLoaderSupplier).withRuntimeOptions(runtimeOptions).175                withAdditionalPlugins(serenityReporter).176                withEventBus(bus).withFeatureSupplier(featureSupplier).177                build();178        return runtime;179    }180    private static void addSerenityReporterPlugin(Plugins plugins, SerenityReporter plugin)181    {182        for(Plugin currentPlugin : plugins.getPlugins()){183            if (currentPlugin instanceof SerenityReporter) {184                return;185            }186        }187        plugins.addPlugin(plugin);188    }189    @Override190    protected Description describeChild(ParentRunner<?> child) {191        return child.getDescription();192    }193    @Override194    protected void runChild(ParentRunner<?> child, RunNotifier notifier) {195        child.run(notifier);196    }197    @Override...

Full Screen

Full Screen

Source:CucumberWithSerenityRuntime.java Github

copy

Full Screen

1package net.serenitybdd.cucumber;2import io.cucumber.core.options.RuntimeOptions;3import io.cucumber.core.plugin.SerenityReporter;4import io.cucumber.core.runtime.Runtime;5import net.thucydides.core.guice.Injectors;6import net.thucydides.core.webdriver.Configuration;7import java.util.function.Supplier;8public class CucumberWithSerenityRuntime {9    public static Runtime using(Supplier<ClassLoader> classLoaderSupplier,10                                RuntimeOptions runtimeOptions) {11        Configuration systemConfiguration = Injectors.getInjector().getInstance(Configuration.class);12        return createSerenityEnabledRuntime(classLoaderSupplier, runtimeOptions, systemConfiguration);13    }14    private static Runtime createSerenityEnabledRuntime(Supplier<ClassLoader> classLoaderSupplier,15                                                        RuntimeOptions runtimeOptions,16                                                        Configuration systemConfiguration) {17        //ClassFinder resolvedClassFinder = Optional.ofNullable(classFinder).orElse(new ResourceLoaderClassFinder(resourceLoader, classLoader));18        SerenityReporter reporter = new SerenityReporter(systemConfiguration);19        //Runtime runtime = Runtime.builder().withResourceLoader(resourceLoader).withClassFinder(resolvedClassFinder).20        //        withClassLoader(classLoader).withRuntimeOptions(runtimeOptions).withAdditionalPlugins(reporter).build();21        Runtime runtime = Runtime.builder().22                withClassLoader(classLoaderSupplier).23                withRuntimeOptions(runtimeOptions).24                withAdditionalPlugins(reporter).build();25        return runtime;26    }27}...

Full Screen

Full Screen

SerenityReporter

Using AI Code Generation

copy

Full Screen

1package com.serenitybdd.cucumber.integration;2import io.cucumber.core.plugin.SerenityReporter;3import net.serenitybdd.cucumber.CucumberWithSerenity;4import org.junit.runner.RunWith;5@RunWith(CucumberWithSerenity.class)6public class RunSerenityTest {7}

Full Screen

Full Screen

SerenityReporter

Using AI Code Generation

copy

Full Screen

1import io.cucumber.core.plugin.SerenityReporter;2import io.cucumber.core.plugin.SerenityReporterOptions;3import io.cucumber.serenity.SerenityCucumberWithSerenity;4import io.cucumber.junit.CucumberOptions;5import io.cucumber.junit.Cucumber;6import org.junit.runner.RunWith;7@RunWith(SerenityCucumberWithSerenity.class)8@CucumberOptions(plugin = {"pretty", "html:target/cucumber-html-report",9        "io.cucumber.core.plugin.SerenityReporter"})10public class RunCucumberTest {11}

Full Screen

Full Screen
copy
1System.setProperty("java.rmi.server.hostname","Ip or DNS of the server");2
Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Serenity Cucumber automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SerenityReporter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful