Is it possible to skip a script using Cucumber-JVM at run time

I want to add the @skiponchrome tag to the script, this should skip the script when running the Selenium test with the Chrome browser. The reason for this is that some scripts work in some environments, and not in others, it may not even be browser specific, and may be applied in a different situation, for example, on OS platforms.

Example:

@Before("@skiponchrome") // this works public void beforeScenario() { if(currentBrowser == 'chrome') { // this works // Skip scenario code here } } 

I know that you can define ~ @skiponchrome in cucumber tags to skip a tag, but I would like to skip a tag at runtime. This way, I don’t need to think about what steps to skip beforehand when I run a test run in a specific environment.

I would like to create a hook that catches the tag and skips the script without error / error message. Is it possible?

+4
source share
5 answers

I also had the same problem when I need to skip a script from running based on a flag that I get from the application dynamically at runtime, which indicates whether the tested function is enabled in the application or not ..

so this is how I wrote my logic in a script file, where we have glue code for each step.

I used the unique tag '@ Feature-01AXX' to mark my scripts that need to be run only when this function (code) is available in the application.

therefore, for each script, the '@ Feature-01XX' tag is checked first, if it is present, then the availability of this function is checked, only then the script will be selected to run. Otherwise, it will simply be skipped and Dunnit will not mark it as a failure, instead it will be marked as Pass. Thus, the end result, if these tests were not performed due to the unavailability of the function, will pass, this is cool ...

 @Before public void before(final Scenario scenario) throws Exception { /* my other pre-setup tasks for each scenario. */ // get all the scenario tags from the scenario head. final ArrayList<String> scenarioTags = new ArrayList<>(); scenarioTags.addAll(scenario.getSourceTagNames()); // check if the feature is enabled on the appliance, so that the tests can be run. if (checkForSkipScenario(scenarioTags)) { throw new AssumptionViolatedException("The feature 'Feature-01AXX' is not enabled on this appliance, so skipping"); } } private boolean checkForSkipScenario(final ArrayList<String> scenarioTags) { // I use a tag "@Feature-01AXX" on the scenarios which needs to be run when the feature is enabled on the appliance/application if (scenarioTags.contains("@Feature-01AXX") && !isTheFeatureEnabled()) { // if feature is not enabled, then we need to skip the scenario. return true; } return false; } private boolean isTheFeatureEnabled(){ /* my logic to check if the feature is available/enabled on the application. in my case its an REST api call, I parse the JSON and check if the feature is enabled. if it is enabled return 'true', else return 'false' */ } 
+3
source

I really prefer to be explicit about which tests are run, having separate run configurations defined for each environment. I would also like to keep the number of tags that I use in order to minimize the number of configurations.

I don’t think you can achieve what you want with tags. You will need to write a custom jUnit test runner to use instead of @RunWith (Cucumber.class). Take a look at the Cucumber implementation to see how everything works. You will need to modify the RuntimeOptions created by the RuntimeOptionsFactory to include / exclude tags depending on the browser or other execution condition.

Alternatively, you might consider writing a small script that calls your test suite, creating a list of tags to include / exclude dynamically, depending on the environment in which you work. I would think this is a more supported, cleaner solution.

+5
source

I realized that this is the last update for the already answered question, but I want to add another option supported with the help of the cucumber-JVM:

 @Before //(cucumber one) public void setup(){ Assume.assumeTrue(weAreInPreProductionEnvironment); } 

"and the script will be marked as ignored (but the test will pass) if weAreInPreProductionEnvironment is false."

You will need to add

 import org.junit.Assume; 

The main difference from the accepted answer is that JUnit assumes that the errors behave as expected

Important Due to bug fixes, you will need release 1.2.5 cucumber-jvm, which is the last letter about this. For example, the above will cause a crash, not a wait in cucumber-java8-1.2.3.jar

+4
source

It is really very simple. If you dig out the source code for Cucumber-JVM and JUnit 4, you will find that JUnit makes runtime skipping very easy (just undocumented).

Take a look at the following source code for the JUnit 4 ParentRunner , which is the Cucumber-JVM FeatureRunner (which is used in Cucumber , the default is Cucumber runner):

 @Override public void run(final RunNotifier notifier) { EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription()); try { Statement statement = classBlock(notifier); statement.evaluate(); } catch (AssumptionViolatedException e) { testNotifier.fireTestIgnored(); } catch (StoppedByUserException e) { throw e; } catch (Throwable e) { testNotifier.addFailure(e); } } 

This is how JUnit decides which result to show. If it @Ignore , it will show a skip, but in JUnit maybe @Ignore , so what happens in this case? Well, an AssumptionViolatedException RunNotifier (or Cucumber FeatureRunner in this case).

So your example:

 @Before("@skiponchrome") // this works public void beforeScenario() { if(currentBrowser == 'chrome') { // this works throw new AssumptionViolatedException("Not supported on Chrome") } } 

If you've already used vanilla JUnit 4, remember that @Ignore accepts an optional message that displays when the test is ignored by the runner. AssumptionViolatedException carries the message, so you should see it in your test output after the test is skipped this way, without having to write your own custom runner.

+3
source

If you are using Maven, you can read using your browser profile and then set the appropriate ~ exclude tags?

If you do not ask how to run this from the command line, in this case you mark the script with @skipchrome, and then when you start the cucumber, set the cucumber parameters to tags = {"~ @skipchrome"}

0
source

All Articles