Best Serenity Cucumber code snippet using io.cucumber.core.plugin.FeatureTracker.isNewFeature
Source:SerenityReporter.java
...194 URI featurePath = event.getTestCase().getUri();195 getContext().currentFeaturePathIs(featurePath);196 contextURISet.add(featurePath);197 setStepEventBus(featurePath);198 if (FeatureTracker.isNewFeature(event)) {199 // Shut down any drivers remaining open from a previous feature, if @singlebrowser is used.200 // Cucumber has no event to mark the start and end of a feature, so we need to do this here.201 if (RestartBrowserForEach.configuredIn(systemConfiguration.getEnvironmentVariables()).restartBrowserForANew(FEATURE)) {202 ThucydidesWebDriverSupport.closeCurrentDrivers();203 }204 FeatureTracker.startNewFeature(event);205 }206 String scenarioName = event.getTestCase().getName();207 TestSourcesModel.AstNode astNode = featureLoader.getAstNode(getContext().currentFeaturePath(), event.getTestCase().getLocation().getLine());208 Optional<Feature> currentFeature = featureFrom(featurePath);209 if ((astNode != null) && currentFeature.isPresent()) {210 getContext().setCurrentScenarioDefinitionFrom(astNode);211 //the sources are read in parallel, global current feature cannot be used212 String scenarioId = scenarioIdFrom(currentFeature.get().getName(), TestSourcesModel.convertToId(getContext().currentScenarioDefinition.getName()));...
Source:FeatureTracker.java
...6 private static ThreadLocal<Set<URI>> STARTED_FEATURES = ThreadLocal.withInitial(HashSet::new);7 public static void startNewFeature(TestCaseStarted event) {8 STARTED_FEATURES.get().add(event.getTestCase().getUri());9 }10 public static boolean isNewFeature(TestCaseStarted event) {11 return !STARTED_FEATURES.get().contains(event.getTestCase().getUri());12 }13 public static boolean isASingleBrowserScenario(TestCaseStarted event) {14 return event.getTestCase().getTags().contains("@singlebrowser");15 }16}...
isNewFeature
Using AI Code Generation
1package io.cucumber.core.plugin;2import io.cucumber.core.eventbus.EventBus;3import io.cucumber.core.eventbus.Subscribe;4import io.cucumber.core.feature.CucumberFeature;5import io.cucumber.core.feature.FeatureParser;6import io.cucumber.core.gherkin.Feature;7import io.cucumber.core.gherkin.Scenario;8import io.cucumber.core.gherkin.Step;9import io.cucumber.core.gherkin.StepType;10import io.cucumber.core.gherkin.events.PickleEvent;11import io.cucumber.core.gherkin.events.PickleStepTestStep;12import io.cucumber.core.runtime.CucumberException;13import io.cucumber.core.runtime.TimeServiceEventBus;14import io.cucumber.core.runtime.TimeServiceEventBus.TimeService;15import io.cucumber.plugin.event.EventPublisher;16import io.cucumber.plugin.event.TestCase;17import io.cucumber.plugin.event.TestCaseFinished;18import io.cucumber.plugin.event.TestCaseStarted;19import io.cucumber.plugin.event.TestRunFinished;20import io.cucumber.plugin.event.TestRunStarted;21import io.cucumber.plugin.event.TestStep;22import io.cucumber.plugin.event.TestStepFinished;23import io.cucumber.plugin.event.TestStepStarted;24import io.cucumber.plugin.event.WriteEvent;25import org.apiguardian.api.API;26import org.apiguardian.api.API.Status;27import java.io.ByteArrayOutputStream;28import java.io.IOException;29import java.io.OutputStream;30import java.io.OutputStreamWriter;31import java.io.PrintWriter;32import java.io.Writer;33import java.nio.charset.StandardCharsets;34import java.time.Duration;35import java.time.Instant;36import java.util.ArrayList;37import java.util.Collections;38import java.util.HashMap;39import java.util.List;40import java.util.Map;41import java.util.Stack;42import java.util.concurrent.ConcurrentHashMap;43import java.util.concurrent.ConcurrentMap;44import java.util.concurrent.atomic.AtomicInteger;45import java.util.concurrent.atomic.AtomicLong;46import java.util.stream.Collectors;47import static java.util.Collections.emptyList;48import static java.util.Collections.unmodifiableMap;49import static java.util.stream.Collectors.joining;50import static org.apiguardian.api.API.Status.STABLE;51@API(status = STABLE)52public final class PrettyFormatter implements EventListener, EventPublisher {
isNewFeature
Using AI Code Generation
1import java.util.List;2import java.util.stream.Collectors;3import io.cucumber.core.plugin.FeatureTracker;4import io.cucumber.core.plugin.FeatureTracker.Status;5import io.cucumber.java.en.Given;6import io.cucumber.java.en.Then;7import io.cucumber.java.en.When;8public class StepDefinitions {9 private static final FeatureTracker featureTracker = new FeatureTracker();10 private int num;11 @Given("a number {int}")12 public void a_number(Integer int1) {13 num = int1;14 }15 @When("I add {int}")16 public void i_add(Integer int1) {17 num += int1;18 }19 @Then("the result should be {int}")20 public void the_result_should_be(Integer int1) {21 assert num == int1;22 }23 @Given("a scenario")24 public void a_scenario() {25 }26 @When("I run scenario")27 public void i_run_scenario() {28 }29 @Then("the scenario should run")30 public void the_scenario_should_run() {31 }32 @Given("a scenario which is not new")33 public void a_scenario_which_is_not_new() {34 featureTracker.updateStatus(Status.NOT_NEW);35 }36 @When("I run scenario which is not new")37 public void i_run_scenario_which_is_not_new() {38 }39 @Then("the scenario should not run")40 public void the_scenario_should_not_run() {41 }42 @Given("a scenario which is new")43 public void a_scenario_which_is_new() {44 featureTracker.updateStatus(Status.NEW);45 }46 @When("I run scenario which is new")47 public void i_run_scenario_which_is_new() {48 }49 @Then("the scenario should run")50 public void the_scenario_should_run() {51 }52 @Given("a scenario which is undefined")53 public void a_scenario_which_is_undefined() {
isNewFeature
Using AI Code Generation
1package stepdefinition;2import io.cucumber.java.en.Given;3import io.cucumber.java.en.Then;4import io.cucumber.java.en.When;5import io.cucumber.java.Before;6import io.cucumber.java.After;7import io.cucumber.java.BeforeStep;8import io.cucumber.java.AfterStep;9import io.cucumber.core.plugin.FeatureTracker;10import io.cucumber.core.plugin.FeatureTrackerSet;11public class StepDefinition {12 @Given("I have a calculator")13 public void i_have_a_calculator() {14 System.out.println("I have a calculator");15 }16 @When("I add {int} and {int}")17 public void i_add_and(Integer int1, Integer int2) {18 System.out.println("I add "+int1+" and "+int2);19 }20 @Then("the result should be {int}")21 public void the_result_should_be(Integer int1) {22 System.out.println("the result should be "+int1);23 }24}
isNewFeature
Using AI Code Generation
1import io.cucumber.core.plugin.FeatureTracker;2import io.cucumber.java.en.Given;3import io.cucumber.java.en.Then;4import io.cucumber.java.en.When;5public class StepDefinitions {6 private static FeatureTracker featureTracker = new FeatureTracker();7 private int sum;8 @Given("I have a calculator")9 public void i_have_a_calculator() {10 System.out.println("Given");11 }12 @When("I add {int} and {int}")13 public void i_add_and(int firstNumber, int secondNumber) {14 if (featureTracker.isNewFeature()) {15 sum = firstNumber + secondNumber;16 System.out.println("When");17 }18 }19 @Then("the result should be {int}")20 public void the_result_should_be(int result) {21 if (featureTracker.isNewFeature()) {22 System.out.println("Then");23 assert sum == result;24 }25 }26}27public FeatureTracker() {28 this.featureName = Thread.currentThread().getStackTrace()[3].getFileName();29}
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!!