Best Serenity Cucumber code snippet using net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios.slice
Source:CucumberSerenityRunner.java
...238 int forkCount = environmentVariables.getPropertyAsInteger(SERENITY_FORK_COUNT, 1);239 if ((batchCount == 1) && (forkCount == 1)) {240 return children;241 } else {242 LOGGER.info("Running slice {} of {} using fork {} of {} from feature paths {}", batchNumber, batchCount, forkNumber, forkCount, featurePaths);243 List<String> tagFiltersAsString = tagFilters.stream().map(Expression::toString).collect(toList());244 WeightedCucumberScenarios weightedCucumberScenarios = new CucumberSuiteSlicer(featurePaths, TestStatistics.from(environmentVariables, featurePaths))245 .scenarios(batchNumber, batchCount, forkNumber, forkCount, tagFiltersAsString);246 List<ParentRunner<?>> unfilteredChildren = children;247 AtomicInteger filteredInScenarioCount = new AtomicInteger();248 List<ParentRunner<?>> filteredChildren = unfilteredChildren.stream()249 .filter(forIncludedFeatures(weightedCucumberScenarios))250 .map(toPossibleFeatureRunner(weightedCucumberScenarios, filteredInScenarioCount))251 .filter(Optional::isPresent)252 .map(Optional::get)253 .collect(toList());254 if (filteredInScenarioCount.get() != weightedCucumberScenarios.totalScenarioCount()) {255 LOGGER.warn(256 "There is a mismatch between the number of scenarios included in this test run ({}) and the expected number of scenarios loaded ({}). This suggests that the scenario filtering is not working correctly or feature file(s) of an unexpected structure are being run",...
Source:CucumberWithSerenity.java
...85 int forkCount = environmentVariables.getPropertyAsInteger(SERENITY_FORK_COUNT, 1);86 if ((batchCount == 1) && (forkCount == 1)) {87 return super.getChildren();88 } else {89 LOGGER.info("Running slice {} of {} using fork {} of {} from feature paths {}", batchNumber, batchCount, forkNumber, forkCount, featurePaths);90 WeightedCucumberScenarios weightedCucumberScenarios = new CucumberSuiteSlicer(featurePaths, TestStatistics.from(environmentVariables, featurePaths))91 .scenarios(batchNumber, batchCount, forkNumber, forkCount, tagFilters);92 List<FeatureRunner> unfilteredChildren = super.getChildren();93 AtomicInteger filteredInScenarioCount = new AtomicInteger();94 List<FeatureRunner> filteredChildren = unfilteredChildren.stream()95 .filter(forIncludedFeatures(weightedCucumberScenarios))96 .map(toPossibleFeatureRunner(weightedCucumberScenarios, filteredInScenarioCount))97 .filter(Optional::isPresent)98 .map(Optional::get)99 .collect(toList());100 if (filteredInScenarioCount.get() != weightedCucumberScenarios.totalScenarioCount()) {101 LOGGER.warn(102 "There is a mismatch between the number of scenarios included in this test run ({}) and the expected number of scenarios loaded ({}). This suggests that the scenario filtering is not working correctly or feature file(s) of an unexpected structure are being run",103 filteredInScenarioCount.get(),...
Source:WeightedCucumberScenarios.java
...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 }...
Source:CucumberScenarioVisualiser.java
...20 }21 private String outputDirectory() {22 return environmentVariables.getProperty(SERENITY_OUTPUT_DIRECTORY, "target/site/serenity");23 }24 public static List<VisualisableCucumberScenarios> sliceIntoForks(int forkCount, List<WeightedCucumberScenarios> slices) {25 return slices.stream()26 .map(slice -> IntStream.rangeClosed(1, forkCount).mapToObj(forkNumber -> VisualisableCucumberScenarios.create(slices.indexOf(slice) + 1, forkNumber, slice.slice(forkNumber).of(forkCount)))27 .collect(toList())).flatMap(List::stream).collect(toList());28 }29 public void visualise(URI rootFolderURI, int sliceCount, int forkCount, TestStatistics testStatistics) {30 try {31 Files.createDirectories(Paths.get(outputDirectory()));32 List<WeightedCucumberScenarios> slices = new CucumberScenarioLoader(newArrayList(rootFolderURI), testStatistics).load().sliceInto(sliceCount);33 List<VisualisableCucumberScenarios> visualisedSlices = CucumberScenarioVisualiser.sliceIntoForks(forkCount, slices);34 String jsonFile = String.format("%s/%s-slice-config-%s-forks-in-each-of-%s-slices-using-%s.json", outputDirectory(), PathUtils35 .getAsFile(rootFolderURI).getPath().replaceAll("[:/]", "-"), forkCount, sliceCount, testStatistics);36 Files.write(Paths.get(jsonFile), new GsonBuilder().setPrettyPrinting().create().toJson(visualisedSlices).getBytes());37 LOGGER.info("Wrote visualisation as JSON for {} slices -> {}", visualisedSlices.size(), jsonFile);38 } catch (Exception e) {39 throw new RuntimeException("failed to visualise scenarios", e);40 }41 }42}...
Source:WeightedCucumberScenariosTest.java
...13 public void slicingMoreThinlyThanTheNumberOfScenariosShouldResultInSomeEmptySlices() {14 WeightedCucumberScenario weightedCucumberScenario = new WeightedCucumberScenario("test.feature", "featurename", "scenarioname", BigDecimal.ONE, emptySet(), 0);15 List<WeightedCucumberScenario> scenarios = Collections.singletonList(weightedCucumberScenario);16 WeightedCucumberScenarios oneScenario = new WeightedCucumberScenarios(scenarios);17 List<WeightedCucumberScenarios> weightedCucumberScenarios = oneScenario.sliceInto(100);18 assertThat(weightedCucumberScenarios, hasSize(100));19 assertThat(weightedCucumberScenarios.get(0).scenarios, hasSize(1));20 assertThat(weightedCucumberScenarios.get(0).scenarios.get(0), is(weightedCucumberScenario));21 assertThat(weightedCucumberScenarios.get(1).scenarios, hasSize(0));22 assertThat(weightedCucumberScenarios.get(99).scenarios, hasSize(0));23 }24 @Test25 public void slicingASliceIntoOneSliceOfOneShouldBeTheSameAsAllScenarios() {26 List<WeightedCucumberScenario> scenarios = Collections.singletonList(new WeightedCucumberScenario("test.feature", "featurename", "scenarioname", BigDecimal.ONE, emptySet(), 0));27 WeightedCucumberScenarios oneScenario = new WeightedCucumberScenarios(scenarios);28 WeightedCucumberScenarios fork1 = oneScenario.slice(1).of(1);29 assertThat(oneScenario, is(fork1));30 }31}...
Source:VisualisableCucumberScenarios.java
2import org.apache.commons.lang3.builder.HashCodeBuilder;3import org.apache.commons.lang3.builder.ToStringBuilder;4import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;5public class VisualisableCucumberScenarios extends WeightedCucumberScenarios {6 public final Integer slice;7 public final Integer forkNumber;8 private VisualisableCucumberScenarios(Integer slice, Integer forkNumber, WeightedCucumberScenarios WeightedCucumberScenarios) {9 super(WeightedCucumberScenarios.scenarios);10 this.forkNumber = forkNumber;11 this.slice = slice;12 }13 public static VisualisableCucumberScenarios create(Integer slice, Integer forkNumber, WeightedCucumberScenarios WeightedCucumberScenarios) {14 return new VisualisableCucumberScenarios(slice, forkNumber, WeightedCucumberScenarios);15 }16 @Override17 public int hashCode() {18 return HashCodeBuilder.reflectionHashCode(this);19 }20 @Override21 public boolean equals(Object obj) {22 return reflectionEquals(this, obj);23 }24 @Override25 public String toString() {26 return ToStringBuilder.reflectionToString(this);27 }28}...
Source:CucumberSuiteSlicer.java
...13 }14 public WeightedCucumberScenarios scenarios(int batchNumber, int batchCount, int forkNumber, int forkCount, List<String> tagFilters) {15 return new CucumberScenarioLoader(featurePaths, statistics).load()16 .filter(forSuppliedTags(tagFilters))17 .slice(batchNumber).of(batchCount).slice(forkNumber).of(forkCount);18 }19 private Predicate<WeightedCucumberScenario> forSuppliedTags(List<String> tagFilters) {20 return cucumberScenario -> TagParser.parseFromTagFilters(tagFilters).evaluate(newArrayList(cucumberScenario.tags));21 }22}
Source:SliceBuilder.java
1package net.serenitybdd.cucumber.suiteslicing;2public class SliceBuilder {3 private int sliceNumber;4 private WeightedCucumberScenarios weightedCucumberScenarios;5 public SliceBuilder(int sliceNumber, WeightedCucumberScenarios weightedCucumberScenarios) {6 this.sliceNumber = sliceNumber;7 this.weightedCucumberScenarios = weightedCucumberScenarios;8 }9 public WeightedCucumberScenarios of(int sliceCount) {10 return weightedCucumberScenarios.sliceInto(sliceCount).get(sliceNumber - 1);11 }12}...
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!!