Search code examples
javacucumbertestngqaf

Stopping the execution if first scenario fails in QAF - Cucumber


Currently we are triggering our smoke test from testng.xml where we have two different sceanrios to be validated.

Our requirement is that if one scenario fails(@Test1), other should not execute(@Test2). How can I achieve this in QAF, Testng - Cucumber set up ?

    <groups>
        <run>
            <include name="@Test1" />
            <include name="@Test2" />
        </run>
    </groups>
    <classes>
        <class
            name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
    </classes>
</test>

Solution

  • One of the way is by implementing method invocation listener. In after method you can set a flag and in before method you can check flag and skip test depending on value of flag. For example:

    package com.qmetry.qaf.example.test;
    ...
    public class StopRunListener implements IInvokedMethodListener {
       private static boolean hasFailure=false;
    
        @Override
        public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
            String[] groups = testResult.getMethod().getGroups();
    
            if(hasFailure && Arrays.asList(groups).contains("Test2")) {
                throw new SkipException("Stop execution due to failure");
            }
        }
    
        @Override
        public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
            String[] groups = testResult.getMethod().getGroups();
    
            if(!testResult.isSuccess() && Arrays.asList(groups).contains("Test1")) {
                hasFailure=true;
            }
    
        }
    
    }
    

    Add listener in your XML configuration file

        <listeners>
           <listener class-name="com.qmetry.qaf.example.test.StopRunListener" />
        </listeners>
        <groups>
            <run>
                <include name="@Test1" />
                <include name="@Test2" />
            </run>
        </groups>
        <classes>
            <class
                name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
        </classes>
    

    With Gherkin syntax you can't specify dependency or other meta-data. You can use BDD2 syntax available with qaf-2.1.14 and set dependency of group test1 on test2. It will make sure that test from group test2 get executed after group test1. But it will not skip the test if one of the test in depending group is fail. That you can achieve using listener like provided in above example.

    For example:

    #meta-data on feature will be assigned to all scenario in feature file
    @Test1
    Feature: A feature is a collection of scenarios
    
    
    @Test2
    @dependsOnGroups:Test1
    Feature: A feature is a collection of scenarios
    
    

    XML config will be:

        <listeners>
           <listener class-name="com.qmetry.qaf.example.test.StopRunListener" />
        </listeners>
        <groups>
            <run>
                <include name="Test1" /> <!-- don't add @ in group for BDD or BDD2 -->
                <include name="Test2" />
            </run>
        </groups>
        <classes>
            <class
                name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2" />
        </classes>