How to use scenarioWeightFor method of net.serenitybdd.cucumber.suiteslicing.SingleRunTestStatistics class

Best Serenity Cucumber code snippet using net.serenitybdd.cucumber.suiteslicing.SingleRunTestStatistics.scenarioWeightFor

Run Serenity Cucumber automation tests on LambdaTest cloud grid

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

copy
1package net.serenitybdd.cucumber.suiteslicing;
2
3
4import net.serenitybdd.core.time.Stopwatch;
5import net.thucydides.core.util.Inflector;
6
7import org.apache.commons.csv.CSVFormat;
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11import java.io.BufferedReader;
12import java.io.InputStreamReader;
13import java.io.Reader;
14import java.math.BigDecimal;
15import java.util.List;
16import java.util.stream.Collectors;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.DATE;
20import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.DURATION;
21import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.RESULT;
22import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.STABILITY;
23import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.STORY;
24import static net.serenitybdd.cucumber.suiteslicing.SerenityCSVHeader.TITLE;
25
26public class SingleRunTestStatistics implements TestStatistics {
27
28    private static final Logger LOGGER = LoggerFactory.getLogger(SingleRunTestStatistics.class);
29
30    private final List<TestScenarioResult> records;
31    private String fileName;
32    private String[] headers;
33
34
35    private SingleRunTestStatistics(String fileName) {
36        Stopwatch timer = Stopwatch.started();
37        this.fileName = fileName;
38        this.headers = new String[]{STORY, TITLE, RESULT, DATE, STABILITY, DURATION};
39        this.records = records();
40        LOGGER.info("Loaded {} records from {} in {}", records.size(), fileName, timer.executionTimeFormatted());
41    }
42
43    public static TestStatistics fromFileName(String fileName) {
44        return new SingleRunTestStatistics(fileName);
45    }
46
47    @Override
48    public BigDecimal scenarioWeightFor(String feature, String scenario) {
49        return records.stream()
50            .filter(record -> record.feature.equals(feature) && record.scenario.equals(scenario))
51            .map(TestScenarioResult::duration)
52            .findFirst()
53            .orElseThrow(() -> new IllegalArgumentException(String.format("no result found for feature %s and scenario %s", feature, scenario)));
54    }
55
56    @Override
57    public List<TestScenarioResult> records() {
58        try (Reader bufferedReader = new BufferedReader(new InputStreamReader(checkNotNull(getClass().getResourceAsStream(fileName), fileName + " could not be found")))) {
59            return CSVFormat.DEFAULT
60                .withHeader(headers)
61                .withSkipHeaderRecord(true)
62                .parse(bufferedReader)
63                .getRecords().stream()
64                .map(TestScenarioResult::createFromCSV).collect(Collectors.toList());
65        } catch (Exception e) {
66            throw new RuntimeException(fileName + " could not be opened", e);
67        }
68    }
69
70    public String toString() {
71        return Inflector.getInstance().kebabCase(this.getClass().getSimpleName());
72    }
73
74}
75
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

Most used method in SingleRunTestStatistics

Trigger scenarioWeightFor code on LambdaTest Cloud Grid

Execute automation tests with scenarioWeightFor on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)