Best Serenity Cucumber code snippet using net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario
Source:WeightedCucumberScenarios.java
...17import static org.apache.commons.lang3.ObjectUtils.compare;18import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;19/**20 * Represents a collection of cucumber scenarios.21 * Can split itself up into a number of smaller WeightedCucumberScenarios.22 * Can return a new WeightedCucumberScenarios that has some scenarios filtered out.23 */24public class WeightedCucumberScenarios {25 private static final Logger LOGGER = LoggerFactory.getLogger(WeightedCucumberScenarios.class);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:CucumberScenarioLoader.java
...20import static java.util.Arrays.asList;21import static java.util.stream.Collectors.toList;22import static java.util.stream.Collectors.toSet;23/**24 * Reads cucumber feature files and breaks them down into a collection of scenarios (WeightedCucumberScenarios).25 */26public class CucumberScenarioLoader {27 private static final Logger LOGGER = LoggerFactory.getLogger(CucumberScenarioLoader.class);28 private final List<URI> featurePaths;29 private final TestStatistics statistics;30 public CucumberScenarioLoader(List<URI> featurePaths, TestStatistics statistics) {31 this.featurePaths = featurePaths;32 this.statistics = statistics;33 }34 public WeightedCucumberScenarios load() {35 LOGGER.debug("Feature paths are {}", featurePaths);36 ResourceLoader resourceLoader = new MultiLoader(CucumberSuiteSlicer.class.getClassLoader());37 List<WeightedCucumberScenario> weightedCucumberScenarios = new FeatureLoader(resourceLoader).load(featurePaths).stream()38 .map(getScenarios())39 .flatMap(List::stream)40 .collect(toList());41 return new WeightedCucumberScenarios(weightedCucumberScenarios);42 }43 private Function<CucumberFeature, List<WeightedCucumberScenario>> getScenarios() {44 return cucumberFeature -> {45 try {46 return (cucumberFeature.getGherkinFeature().getFeature() == null) ? Collections.emptyList() : cucumberFeature.getGherkinFeature().getFeature().getChildren()47 .stream()48 .filter(child -> asList(ScenarioOutline.class, Scenario.class).contains(child.getClass()))49 .map(scenarioDefinition -> new WeightedCucumberScenario(50 PathUtils.getAsFile(cucumberFeature.getUri()).getName(),51 cucumberFeature.getGherkinFeature().getFeature().getName(),52 scenarioDefinition.getName(),53 scenarioWeightFor(cucumberFeature, scenarioDefinition),54 tagsFor(cucumberFeature, scenarioDefinition),55 scenarioCountFor(scenarioDefinition)))56 .collect(toList());57 } catch (Exception e) {58 throw new IllegalStateException(String.format("Could not extract scenarios from %s", cucumberFeature.getUri()), e);59 }60 };61 }62 private int scenarioCountFor(ScenarioDefinition scenarioDefinition) {63 if (scenarioDefinition instanceof ScenarioOutline) {...
Source:WeightedCucumberScenariosTest.java
...7import static org.hamcrest.collection.IsCollectionWithSize.hasSize;8import static org.hamcrest.core.Is.is;9import static org.junit.Assert.assertThat;10import static org.junit.Assert.fail;11public class WeightedCucumberScenariosTest {12 @Test13 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:MatchingCucumberScenario.java
...3import org.hamcrest.TypeSafeMatcher;4import java.util.HashSet;5import java.util.Set;6import static java.util.Arrays.asList;7public class MatchingCucumberScenario extends TypeSafeMatcher<WeightedCucumberScenario> {8 private String featurePath;9 private String feature;10 private String scenario;11 private Set<String> tags;12 private MatchingCucumberScenario() {13 }14 public static MatchingCucumberScenario with() {15 return new MatchingCucumberScenario();16 }17 public MatchingCucumberScenario featurePath(String featurePath) {18 this.featurePath = featurePath;19 return this;20 }21 public MatchingCucumberScenario feature(String feature) {22 this.feature = feature;23 return this;24 }25 public MatchingCucumberScenario scenario(String scenario) {26 this.scenario = scenario;27 return this;28 }29 @Override30 protected boolean matchesSafely(WeightedCucumberScenario weightedCucumberScenario) {31 return (feature == null || feature.equals(weightedCucumberScenario.feature)) &&32 (featurePath == null || featurePath.equals(weightedCucumberScenario.featurePath)) &&33 (scenario == null || scenario.equals(weightedCucumberScenario.scenario)) &&34 (tags == null || tags.equals(weightedCucumberScenario.tags));35 }36 @Override37 public void describeTo(Description description) {38 description.appendText("A WeightedCucumberScenario matching: featurePath: " + featurePath +39 ", feature: " + feature);40 }41 public MatchingCucumberScenario tags(String... tags) {42 this.tags = new HashSet<>(asList(tags));43 return this;44 }45}...
Source:WeightedCucumberScenario.java
...3import java.math.BigDecimal;4import java.util.Set;5import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;6import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;7public class WeightedCucumberScenario {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 }...
Source:CucumberSuiteSlicer.java
...10 public CucumberSuiteSlicer(List<URI> featurePaths, TestStatistics statistics) {11 this.featurePaths = featurePaths;12 this.statistics = statistics;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}...
WeightedCucumberScenario
Using AI Code Generation
1import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario2import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario3import java.util.function.Supplier4public class WeightedScenarioSupplier implements Supplier<WeightedCucumberScenario> {5 public WeightedCucumberScenario get() {6 return new WeightedCucumberScenario("some.feature", 1);7 }8}9import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario10import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario11import java.util.function.Supplier12public class WeightedScenarioSupplier implements Supplier<WeightedCucumberScenario> {13 public WeightedCucumberScenario get() {14 return new WeightedCucumberScenario("some.feature", 1);15 }16}17import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario18import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario19import java.util.function.Supplier20public class WeightedScenarioSupplier implements Supplier<WeightedCucumberScenario> {21 public WeightedCucumberScenario get() {22 return new WeightedCucumberScenario("some.feature", 1);23 }24}25import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario26import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario27import java.util.function.Supplier28public class WeightedScenarioSupplier implements Supplier<WeightedCucumberScenario> {29 public WeightedCucumberScenario get() {30 return new WeightedCucumberScenario("some.feature", 1);31 }32}33import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario34import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario35import java.util.function.Supplier36public class WeightedScenarioSupplier implements Supplier<WeightedCucumberScenario> {37 public WeightedCucumberScenario get() {38 return new WeightedCucumberScenario("some.feature", 1);39 }40}41import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario42import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario43import java.util.function.Supplier
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!!