5

I have 8 cucumber-jvm scenarios and the very first scenario measures the page load time and environment availability. In order to avoid unnecessary runs, if the first scenario is failed - for instance, the environment is not available, or loading too slowly - all other scenarios should be skipped.

How can I do that?

My CucumberOptions:

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        features = {"src/test/resources/features"},
        glue = {"stepDefinitions"},
        format = {  "progress", "html:target/Results",
                "json:target/Results/cucumber.json"},
        tags = {"@test"})
        public class TestRunner {
}

Thanks!

Gus
  • 3,534
  • 1
  • 30
  • 39
arena
  • 233
  • 3
  • 15

2 Answers2

5

You can make use of Assume.assumeTrue(false) to skip tests. But this will require some changes in test runner and code changes.

  1. Create a separate runner for the scenario which checks the environment details are all working. Let's call it RunFirstTest.java and give tags a value of @Smoke. Other option values default to what you have.
@RunWith(Cucumber.class)
@CucumberOptions(plugin={ }, tags={"@Smoke"}, glue=" ", features=" ")
public class RunFirstTest {
  1. Add the @Smoke tag to the scenario in the feature file that checks the environment etc. Optionally you could look at splitting the feature files.

  2. Create a new class to hold a static flag. It is a simple implementation you might look at making this more robust.

public class SkipFlag {   
     public static boolean skipFlag = false; }
  1. Create an After hook with the value option set to @Smoke. Thus it will run only for the smoke scenario.
@After(value={"@Smoke"})  
public void afterSkip(Scenario scen) {        
   if(scen.isFailed())            
      SkipFlag.skipFlag = true;   
}
  1. Create a second runner for the main tests. Let's call it RunMainTest.java and give its tags value of @MainTests. Other option values default to what you have.

@RunWith(Cucumber.class) @CucumberOptions(plugin={" "}, tags={"@MainTests"}, glue=" ", features=" ") public class RunMainTest {
@BeforeClass public static void before() { if(SkipFlag.skipFlag) Assume.assumeTrue(false); } }

  1. Add the @MainTests tag to the other scenarios in the feature file. Optionally you could look at splitting the feature files and give the name of the feature file in the features option value.

  2. Run this by using maven failsafe plugin. In the configuration of this plugin add the inclusion of these 2 runners in the pom.xml.

 <configuration>
      <includes>
          <include>RunFirstTest</include>
          <include>RunMainTest</include>
      </includes>
        <runOrder>alphabetical</runOrder>
 </configuration>

The includes part might be optional if you only have 2 runners. The most important point is that the RunFirstTest should be the first to run, so alphabetically should be first.

  1. Run it with maven.

Hope it works.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • wow! Thank you for this comprehensive answer! It seems, this is the most robust way to handle such a thing, especially the @smoke tag, dividing the scenarios. This is the answer what I'm looking for! Thank you! It should work with cucumber picocontainer (shared driver) without any issue right? – arena Mar 13 '18 at 21:19
  • Implemented this method and works like a charm! Very useful! Not complicated at all, I learnt a very good approach! I owe you a beer! You should write a blog or something like that, It would be very helpful to step forward in our automation path. – arena Mar 14 '18 at 21:19
  • I think, asking another question from a more experienced person is not prohibited. @Grasshopper, could you please take a look on my other question about verifying error msg? Thanks in advance. – arena Mar 14 '18 at 22:14
0

Instead of trying to do this in Cucumber consider just writing a simple bash script that

  • runs the first feature file e.g. cucumber features/first_scenarios.feature
  • uses the exit code of the first run to decide whether or not to run the rest of the features

You can probably do this in about 3 lines of shell script.

One of the things about features is that different people will want to run them in different ways depending on their own particular circumstances. Putting this stuff inside your set of features can make things more difficult for other users.

diabolist
  • 3,990
  • 1
  • 11
  • 15
  • Thanks for your reply. Grasshopper idea has been implemented and it is not complicated at all. Makes our life easier, I definitely recommend it to anybody. Anyway, Thanks! – arena Mar 14 '18 at 21:19