Best Serenity Cucumber code snippet using net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios.sliceInto
Source:CucumberScenarioLoaderTest.java
...57 new URI("classpath:samples/scenario_with_table_in_background_steps.feature"),58 new URI("classpath:samples/tagged_example_tables.feature"));59 int sliceCount = 5;60 List<WeightedCucumberScenarios> jvm1Slices = new CucumberScenarioLoader(featurePaths, new DummyStatsOfWeightingOne()).load()61 .sliceInto(sliceCount);62 List<WeightedCucumberScenarios> jvm2Slices = new CucumberScenarioLoader(featurePaths, new DummyStatsOfWeightingOne()).load()63 .sliceInto(sliceCount);64 for (int i = 0; i < jvm1Slices.size(); i++) {65 assertThat(jvm1Slices.get(i).scenarios, contains(buildMatchingCucumberScenario(jvm2Slices.get(i))));66 }67 }68 private MatchingCucumberScenario[] buildMatchingCucumberScenario(WeightedCucumberScenarios weightedCucumberScenarios) {69 List<MatchingCucumberScenario> list = new ArrayList<>();70 weightedCucumberScenarios.scenarios.forEach(s -> {71 MatchingCucumberScenario match = MatchingCucumberScenario.with()72 .feature(s.feature)73 .featurePath(s.featurePath)74 .scenario(s.scenario);75 list.add(match);76 });77 return list.toArray(new MatchingCucumberScenario[list.size()]);...
Source:WeightedCucumberScenarios.java
...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);...
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:SliceBuilder.java
...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}...
sliceInto
Using AI Code Generation
1import cucumber.api.junit.Cucumber;2import net.serenitybdd.cucumber.CucumberWithSerenity;3import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios;4import org.junit.runner.RunWith;5import java.util.List;6@RunWith(Cucumber.class)7public class RunCucumberTest {8 public static void main(String[] args) {9 List<String> scenarios = WeightedCucumberScenarios.sliceInto(5, "src/test/resources/features", "classpath:features");10 for (String scenario : scenarios) {11 System.out.println(scenario);12 }13 }14}
sliceInto
Using AI Code Generation
1import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios;2import net.serenitybdd.cucumber.suiteslicing.TestScenario;3import net.serenitybdd.cucumber.suiteslicing.ScenarioWeightCalculator;4import net.serenitybdd.cucumber.suiteslicing.ScenarioWeight;5import net.serenitybdd.cucumber.suiteslicing.CucumberScenario;6import java.util.List;7import java.util.ArrayList;8import java.util.Arrays;9import java.util.stream.Collectors;10import static net.serenitybdd.cucumber.suiteslicing.ScenarioWeight.*;11public class ScenarioWeightCalculatorImpl implements ScenarioWeightCalculator {12 private final List<String> tagsToInclude = Arrays.asList("@smoke", "@regression");13 private final List<String> tagsToExclude = Arrays.asList("@wip", "@manual", "@manual-qa", "@manual-automation");14 public ScenarioWeight calculateWeightFor(CucumberScenario scenario) {15 if (scenario.hasTags(tagsToInclude) && !scenario.hasTags(tagsToExclude)) {16 return Important;17 }18 if (scenario.hasTags(tagsToExclude)) {19 return Excluded;20 }21 return Unimportant;22 }23 public static void main(String[] args) {24 String[] scenarios = new String[]{"@smoke", "@regression", "@wip", "@manual", "@manual-qa", "@manual-automation"};25 List<TestScenario> testScenarios = new ArrayList<>();26 for (String scenario : scenarios) {27 testScenarios.add(new TestScenario(scenario));28 }29 WeightedCucumberScenarios weightedCucumberScenarios = new WeightedCucumberScenarios();30 List<TestScenario> slicedScenarios = weightedCucumberScenarios.sliceInto(testScenarios, 3, new ScenarioWeightCalculatorImpl());31 System.out.println(slicedScenarios.stream().map(TestScenario::getTags).collect(Collectors.toList()));32 }33}
sliceInto
Using AI Code Generation
1 private List<CucumberScenario> sliceInto(int numberOfSlices, int sliceNumber) {2 List<CucumberScenario> scenarios = new ArrayList<>();3 for (CucumberScenario scenario : allScenarios) {4 if (scenario.isIncludedInSlice(numberOfSlices, sliceNumber)) {5 scenarios.add(scenario);6 }7 }8 return scenarios;9 }10}11 private List<CucumberScenario> sliceInto(int numberOfSlices, int sliceNumber) {12 List<CucumberScenario> scenarios = new ArrayList<>();13 for (CucumberScenario scenario : allScenarios) {14 if (scenario.isIncludedInSlice(numberOfSlices, sliceNumber)) {15 scenarios.add(scenario);16 }17 }18 return scenarios;19 }20package com.testrunner;21import cucumber.api.CucumberOptions;22import cucumber.api.junit.Cucumber;23import net.serenitybdd.cucumber.CucumberWithSerenity;24import org.junit.runner.RunWith;25@RunWith(CucumberWithSerenity.class)26@CucumberOptions(27 features = {"classpath:features/"},28 glue = {"com.stepdefinitions"},29 plugin = {"pretty", "json:target/cucumber.json"})30public class TestRunner {31}32package com.testrunner;33import cucumber.api.CucumberOptions;34import cucumber.api.junit.Cucumber;35import net.serenitybdd.cucumber.CucumberWithSerenity;36import org.junit.runner.RunWith;37@RunWith(CucumberWithSerenity.class)38@CucumberOptions(39 features = {"classpath:features/Feature1.feature:1"},40 glue = {"com.stepdefinitions"},41 plugin = {"pretty", "json:target/cucumber.json"})42public class TestRunner1 {43}44package com.testrunner;45import cucumber.api.CucumberOptions;46import cucumber.api.junit.Cucumber;47import net.serenitybdd.cucumber.CucumberWithSerenity;48import org.junit.runner.RunWith;49@RunWith(CucumberWithS
sliceInto
Using AI Code Generation
1import cucumber.api.CucumberOptions2import cucumber.api.junit.Cucumber3import org.junit.runner.RunWith4import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenarios5import net.serenitybdd.cucumber.suiteslicing.WeightedCucumberScenario6import java.io.File7import java.nio.file.Paths8@RunWith(Cucumber::class)9@CucumberOptions(10class SliceIntoTest {11 def "test slicing into multiple feature files"() {12 def featureFile = new File("src/test/resources/features/sliceInto.feature")13 def scenarios = WeightedCucumberScenarios.fromFeatureFile(featureFile)14 def targetDirectory = Paths.get("target", "cucumber").toFile()15 WeightedCucumberScenarios.sliceInto(scenarios, numberOfScenariosPerFile, targetDirectory)16 def slicedFiles = targetDirectory.listFiles()17 slicedFiles.size() == 218 slicedFiles[0].text.contains("Scenario: scenario 1")19 slicedFiles[0].text.contains("Scenario: scenario 2")20 slicedFiles[1].text.contains("Scenario: scenario 3")21 slicedFiles[1].text.contains("Scenario: scenario 4")22 }23}
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!!