How to use generateReports method of net.serenitybdd.junit.runners.SerenityRunner class

Best Serenity JUnit code snippet using net.serenitybdd.junit.runners.SerenityRunner.generateReports

Run Serenity JUnit automation tests on LambdaTest cloud grid

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

copy
1package net.serenitybdd.junit5.extension;
2
3import net.serenitybdd.core.environment.ConfiguredEnvironment;
4import net.thucydides.core.model.TestOutcome;
5import net.thucydides.core.reports.AcceptanceTestReporter;
6import net.thucydides.core.reports.ReportService;
7import net.thucydides.core.steps.BaseStepListener;
8import net.thucydides.core.steps.StepEventBus;
9import org.jetbrains.annotations.NotNull;
10import org.junit.jupiter.api.extension.AfterAllCallback;
11import org.junit.jupiter.api.extension.ExtensionContext;
12
13import java.io.File;
14import java.util.Collection;
15import java.util.List;
16
17public class SerenityReportExtension implements AfterAllCallback {
18
19    @Override
20    public void afterAll(@NotNull ExtensionContext context) throws Exception {
21        final BaseStepListener baseStepListener = StepEventBus.getEventBus().getBaseStepListener();
22        generateReports(baseStepListener);
23    }
24
25    // net.serenitybdd.junit.runners.SerenityRunner.generateReports
26    protected void generateReports(BaseStepListener baseStepListener) {
27        generateReportsFor(baseStepListener.getTestOutcomes());
28    }
29
30    // net.serenitybdd.junit.runners.SerenityRunner.generateReportsFor
31    /**
32     * @param testOutcomeResults the test results from the previous test run.
33     */
34    private void generateReportsFor(final List<TestOutcome> testOutcomeResults) {
35        final ReportService reportService = new ReportService(getOutputDirectory(), getDefaultReporters());
36        reportService.generateReportsFor(testOutcomeResults);
37        reportService.generateConfigurationsReport();
38    }
39
40    /**
41     *  @return The default reporters applicable for standard test runs.
42     */
43    protected Collection<AcceptanceTestReporter> getDefaultReporters() {
44        return ReportService.getDefaultReporters();
45    }
46
47    public File getOutputDirectory() {
48        return ConfiguredEnvironment.getConfiguration().getOutputDirectory();
49    }
50
51}
52
Full Screen
copy
1package net.serenitybdd.junit.runners;
2
3import net.serenitybdd.core.environment.*;
4import net.thucydides.core.batches.*;
5import net.thucydides.core.guice.*;
6import net.thucydides.core.model.*;
7import net.thucydides.core.reports.*;
8import net.thucydides.core.steps.*;
9import net.thucydides.core.tags.*;
10import net.thucydides.core.webdriver.*;
11import net.thucydides.junit.*;
12import net.thucydides.junit.annotations.*;
13import org.apache.commons.lang3.*;
14import org.junit.runner.*;
15import org.junit.runner.notification.*;
16import org.junit.runners.*;
17import org.junit.runners.model.*;
18
19import java.io.*;
20import java.util.*;
21
22/**
23 * Run a Serenity test suite using a set of data.
24 * Similar to the JUnit parameterized tests, but better ;-).
25 */
26public class SerenityParameterizedRunner extends Suite implements Taggable {
27
28    private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
29
30    private List<Runner> runners = new ArrayList<>();
31
32    private final DriverConfiguration configuration;
33    private ReportService reportService;
34    private final ParameterizedTestsOutcomeAggregator parameterizedTestsOutcomeAggregator = ParameterizedTestsOutcomeAggregator.from(this);
35    private TagScanner tagScanner;
36
37    /**
38     * Test runner used for testing purposes.
39     *
40     * @param klass            The test class to run
41     * @param configuration    current system configuration (usually mocked)
42     * @param webDriverFactory a webdriver factory (can be mocked)
43     * @param batchManager     a batch manager to process batched testing
44     * @throws Throwable - cause anything can happen!
45     */
46    public SerenityParameterizedRunner(final Class<?> klass,
47                                       DriverConfiguration configuration,
48                                       final WebDriverFactory webDriverFactory,
49                                       final BatchManager batchManager
50    ) throws Throwable {
51        super(klass, Collections.<Runner>emptyList());
52        this.configuration = configuration;
53        this.tagScanner = new TagScanner(configuration.getEnvironmentVariables());
54
55        if (runTestsInParallelFor(klass)) {
56            scheduleParallelTestRunsFor(klass);
57        }
58
59        DataDrivenAnnotations testClassAnnotations = getTestAnnotations();
60        if (testClassAnnotations.hasTestDataDefined()) {
61            runners = buildTestRunnersForEachDataSetUsing(webDriverFactory, batchManager);
62        } else if (testClassAnnotations.hasTestDataSourceDefined()) {
63            runners = buildTestRunnersFromADataSourceUsing(webDriverFactory, batchManager);
64        }
65    }
66
67    private void scheduleParallelTestRunsFor(final Class<?> klass) {
68        setScheduler(new ParameterizedRunnerScheduler(klass, getThreadCountFor(klass)));
69    }
70
71    public boolean runTestsInParallelFor(final Class<?> klass) {
72        return (klass.getAnnotation(Concurrent.class) != null);
73    }
74
75    public int getThreadCountFor(final Class<?> klass) {
76        Concurrent concurrent = klass.getAnnotation(Concurrent.class);
77        String threadValue = getThreadParameter(concurrent);
78        int threads = (AVAILABLE_PROCESSORS * 2);
79        if (StringUtils.isNotEmpty(threadValue)) {
80            if (StringUtils.isNumeric(threadValue)) {
81                threads = Integer.parseInt(threadValue);
82            } else if (threadValue.endsWith("x")) {
83                threads = getRelativeThreadCount(threadValue);
84            }
85
86        }
87        return threads;
88    }
89
90    private String getThreadParameter(Concurrent concurrent) {
91//        String systemPropertyThreadValue =
92//                configuration.getEnvironmentVariables().getProperty(ThucydidesJUnitSystemProperties.CONCURRENT_THREADS.getName());
93//
94        String systemPropertyThreadValue = EnvironmentSpecificConfiguration.from(configuration.getEnvironmentVariables())
95                .getOptionalProperty(ThucydidesJUnitSystemProperties.CONCURRENT_THREADS.getName())
96                .orElse(null);
97
98        String annotatedThreadValue = concurrent.threads();
99        return (StringUtils.isNotEmpty(systemPropertyThreadValue) ? systemPropertyThreadValue : annotatedThreadValue);
100
101    }
102
103    private int getRelativeThreadCount(final String threadValue) {
104        try {
105            String threadCount = threadValue.substring(0, threadValue.length() - 1);
106            return Integer.parseInt(threadCount) * AVAILABLE_PROCESSORS;
107        } catch (NumberFormatException cause) {
108            throw new IllegalArgumentException("Illegal thread value: " + threadValue, cause);
109        }
110    }
111
112    private List<Runner> buildTestRunnersForEachDataSetUsing(final WebDriverFactory webDriverFactory,
113                                                     final BatchManager batchManager) throws Throwable {
114        if (shouldSkipAllTests()) {
115            return new ArrayList<>();
116        }
117
118        List<Runner> runners = new ArrayList<>();
119        DataTable parametersTable = getTestAnnotations().getParametersTableFromTestDataAnnotation();
120        for (int i = 0; i < parametersTable.getRows().size(); i++) {
121            Class<?> testClass = getTestClass().getJavaClass();
122            SerenityRunner runner = new TestClassRunnerForParameters(testClass,
123                    configuration,
124                    webDriverFactory,
125                    batchManager,
126                    parametersTable,
127                    i);
128            runner.useQualifier(from(parametersTable.getRows().get(i).getValues()));
129            runners.add(runner);
130        }
131        return runners;
132    }
133
134    private List<Runner> buildTestRunnersFromADataSourceUsing(final WebDriverFactory webDriverFactory,
135                                                      final BatchManager batchManager) throws Throwable {
136        if (shouldSkipAllTests()) {
137            return new ArrayList<>();
138        }
139
140        List<Runner> runners = new ArrayList<>();
141        List<?> testCases = getTestAnnotations().getDataAsInstancesOf(getTestClass().getJavaClass());
142        DataTable parametersTable = getTestAnnotations().getParametersTableFromTestDataSource();
143        for (int i = 0; i < testCases.size(); i++) {
144            Object testCase = testCases.get(i);
145            SerenityRunner runner = new TestClassRunnerForInstanciatedTestCase(testCase,
146                    configuration,
147                    webDriverFactory,
148                    batchManager,
149                    parametersTable,
150                    i);
151            runner.useQualifier(getQualifierFor(testCase));
152            runners.add(runner);
153        }
154        return runners;
155    }
156
157    private boolean shouldSkipTest(FrameworkMethod method) {
158        return !tagScanner.shouldRunMethod(getTestClass().getJavaClass(), method.getName());
159    }
160
161    private boolean shouldSkipAllTests() {
162        return getTestAnnotations()
163                .getTestMethods()
164                .stream()
165                .allMatch(this::shouldSkipTest);
166    }
167
168    private String getQualifierFor(final Object testCase) {
169        return QualifierFinder.forTestCase(testCase).getQualifier();
170    }
171
172    private DataDrivenAnnotations getTestAnnotations() {
173        return DataDrivenAnnotations.forClass(getTestClass());
174    }
175
176    private String from(final Collection testData) {
177        StringBuffer testDataQualifier = new StringBuffer();
178        boolean firstEntry = true;
179        for (Object testDataValue : testData) {
180            if (!firstEntry) {
181                testDataQualifier.append("/");
182            }
183            testDataQualifier.append(testDataValue);
184            firstEntry = false;
185        }
186        return testDataQualifier.toString();
187    }
188
189
190    /**
191     * Only called reflectively. Do not use programmatically.
192     *
193     * @param klass The test class to run
194     * @throws Throwable Cause shit happens
195     */
196    public SerenityParameterizedRunner(final Class<?> klass) throws Throwable {
197        this(klass, WebDriverConfiguredEnvironment.getDriverConfiguration(), new WebDriverFactory(),
198                Injectors.getInjector().getInstance(BatchManager.class));
199    }
200
201    @Override
202    protected List<Runner> getChildren() {
203        return runners;
204    }
205
206    @Override
207    public void run(final RunNotifier notifier) {
208        try {
209            super.run(notifier);
210        } finally {
211            StepEventBus.getEventBus().testSuiteFinished();
212            generateReports();
213        }
214    }
215
216    public void generateReports() {
217        generateReportsFor(parameterizedTestsOutcomeAggregator.aggregateTestOutcomesByTestMethods());
218    }
219
220    private void generateReportsFor(List<TestOutcome> testOutcomes) {
221        getReportService().generateReportsFor(testOutcomes);
222        getReportService().generateConfigurationsReport();
223    }
224
225    private ReportService getReportService() {
226        if (reportService == null) {
227            reportService = new ReportService(getOutputDirectory(), getDefaultReporters());
228        }
229        return reportService;
230
231    }
232
233    private Collection<AcceptanceTestReporter> getDefaultReporters() {
234        return ReportService.getDefaultReporters();
235    }
236
237    private File getOutputDirectory() {
238        return this.configuration.getOutputDirectory();
239    }
240
241    public void subscribeReporter(final AcceptanceTestReporter reporter) {
242        getReportService().subscribe(reporter);
243    }
244
245    public List<Runner> getRunners() {
246        return runners;
247    }
248
249
250}
251
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)