Search code examples
seleniumcucumbercucumber-jvm

How to skip execution of test case from the Hooks in cucumber


I have to skip execution of the test case @bannerVerificationSMMDView only when the viewPort is Large

@Before
    public void beforestartUp(Scenario scenario) throws IOException {    
     boolean runTest = true;
        if (viewPort.contains("LARGE")) {
            System.out.println("for Scenario " + scenario.getName() + " tagname are");
            List<String> tags = (List<String>) scenario.getSourceTagNames();
            for (String tagName : tags) {
                if (tagName.contains("bannerVerificationLView"))
                    runTest = false;
            }
            try {
                Assume.assumeTrue(runTest);
            } catch (AssumptionViolatedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
   }

Not sure why, but exception is not getting caught


Solution

  • Throw a AssumptionViolatedException to skip the execution of scenario.

    @Before(value="@bannerVerificationSMMDView")
    public void before(Scenario scenario) {
    
        if(viewPort.contains("LARGE"))
            throw new AssumptionViolatedException("Skipping as view is LARGE");
    
    }
    

    If you are on cucumber version 3 plus, you can use a @BeforeStep annotation instead, keep everything else same. This will allow you to run any previous steps in the scenario and if condition is not met then skip the rest of the steps in the scenario