Best Serenity Cucumber code snippet using net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario.weighting
Source:WeightedCucumberScenarios.java
...26 public final BigDecimal totalWeighting;27 public final List<WeightedCucumberScenario> scenarios;28 public WeightedCucumberScenarios(List<WeightedCucumberScenario> scenarios) {29 this.scenarios = scenarios;30 this.totalWeighting = scenarios.stream().map(WeightedCucumberScenario::weighting).reduce(ZERO, BigDecimal::add);31 }32 public SliceBuilder slice(int sliceNumber) {33 return new SliceBuilder(sliceNumber, this);34 }35 public List<WeightedCucumberScenarios> sliceInto(int sliceCount) {36 BigDecimal totalWeight = scenarios.stream().map(WeightedCucumberScenario::weighting).reduce(ZERO, BigDecimal::add);37 BigDecimal averageWeightPerSlice = totalWeight.divide(new BigDecimal(sliceCount), 2, RoundingMode.HALF_UP);38 LOGGER.debug("Total weighting for {} scenarios is {}, split across {} slices provides average weighting per slice of {}", scenarios.size(), totalWeight, sliceCount, averageWeightPerSlice);39 List<List<WeightedCucumberScenario>> allScenarios = IntStream.rangeClosed(1, sliceCount).mapToObj(initialiseAs -> new ArrayList<WeightedCucumberScenario>()).collect(toList());40 scenarios.stream()41 .sorted(bySlowestFirst())42 .forEach(scenario -> allScenarios.stream().min(byLowestSumOfDurationFirst()).get().add(scenario));43 return allScenarios.stream().map(WeightedCucumberScenarios::new).collect(toList());44 }45 public ScenarioFilter createFilterContainingScenariosIn(String featureName) {46 LOGGER.debug("Filtering for scenarios in feature {}", featureName);47 List<String> scenarios = this.scenarios.stream()48 .filter(scenario -> {49 boolean matches = scenario.feature.equals(featureName);50 LOGGER.debug("Scenario {} matches {} -> {}", scenario.feature, featureName, matches);51 return matches;52 })53 .map(scenario -> scenario.scenario)54 .collect(toList());55 if (scenarios.isEmpty()) {56 throw new IllegalArgumentException("Can't find feature '" + featureName + "' in this slice");57 }58 return ScenarioFilter.onScenarios(scenarios);59 }60 public WeightedCucumberScenarios filter(Predicate<WeightedCucumberScenario> predicate) {61 return new WeightedCucumberScenarios(scenarios.stream().filter(predicate).collect(toList()));62 }63 @Override64 public int hashCode() {65 return HashCodeBuilder.reflectionHashCode(this);66 }67 @Override68 public boolean equals(Object obj) {69 return reflectionEquals(this, obj);70 }71 @Override72 public String toString() {73 return ToStringBuilder.reflectionToString(this);74 }75 private static Comparator<WeightedCucumberScenario> bySlowestFirst() {76 return (item1, item2) -> compare(item2.weighting(), item1.weighting());77 }78 private static Comparator<List<WeightedCucumberScenario>> byLowestSumOfDurationFirst() {79 return (item1, item2) -> compare(item1.stream().map(WeightedCucumberScenario::weighting).reduce(ZERO, BigDecimal::add),80 item2.stream().map(WeightedCucumberScenario::weighting).reduce(ZERO, BigDecimal::add));81 }82 public int totalScenarioCount() {83 return scenarios.stream().map(scenario -> scenario.scenarioCount).reduce(0, Integer::sum);84 }85}...
Source:WeightedCucumberScenario.java
...8 public final String featurePath;9 public final String feature;10 public final String scenario;11 public final int scenarioCount;12 public final BigDecimal weighting;13 public final Set<String> tags;14 public WeightedCucumberScenario(String featurePath, String feature, String scenario, BigDecimal weighting, Set<String> tags, int scenarioCount) {15 this.featurePath = featurePath;16 this.feature = feature;17 this.scenario = scenario;18 this.weighting = weighting;19 this.tags = tags;20 this.scenarioCount = scenarioCount;21 }22 public BigDecimal weighting() {23 return weighting;24 }25 @Override26 public int hashCode() {27 return HashCodeBuilder.reflectionHashCode(this);28 }29 @Override30 public boolean equals(Object obj) {31 return reflectionEquals(this, obj);32 }33 @Override34 public String toString() {35 return reflectionToString(this);36 }37}...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!