Search code examples
javaseleniumselenium-webdrivertestngqaf

How to exclude groups based on condition in TestNG?


I have around 10 testcase in Login module. I have to execute the test on Staging and Product environment but on Product environment, need to exclude some specific testcase which require some dummy data to be insert in application. For that i have added one group name PRO_EXCLUDE in my scenarios.

Refer the below example with combined group names which i need to exclude while executing.

SCENARIO: verify login landing page
META-DATA: {"TestCase_ID":"BP_L&R_001","description":"verify login landing page ","groups":["REGRESSION","PRO_EXCLUDE"]}    
    Given user is on homepage
    When clicks on login link
    Then verify page title text with title '${loginpage.title}'
END

And rest of method have only one group i.e. REGRESSION

I've configured the test in below manner

<test name="Login" enabled="true">
    <method-selectors>
        <method-selector>
            <script language="beanshell"><![CDATA[ return groups.containsKey("REGRESSION") && groups.containsKey("PRO_EXCLUDE");]]></script>
        </method-selector>
    </method-selectors>
    <parameter name="scenario.file.loc" value="scenarios/login.bdd" />

    <classes>
        <class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory"></class>
    </classes>
</test>

This executes the scenario which have REGRESSION and PRO_EXCLUDE group both. I don't want to execute this one but rest of Scenarios with only REGRESSION group.


Solution

  • Better approach is to utilize meta-data feature of qaf. According to that rather than adding multiple groups, categorize them depending on nature. For example:

    • Scope - Smoke, Regression
    • Module - FunctionlModule1, FM2
    • Channel - Web, API, Mobile

    etc...

    You need to define for your AUT and set in scenario as meta-data.

    SCENARIO: verify login landing page
    META-DATA: {"TestCase_ID":"BP_L&R_001","description":"verify login landing page ","scope":"REGRESSION","feature":"PRO_EXCLUDE"]}    
        Given user is on homepage
        When clicks on login link
        Then verify page title text with title '${loginpage.title}'
    END
    

    If you are authoring test case in java you can use @MetaData on test method to set test case meta-data. You can use meta-data filter by setting appropriate include and exclude property value as below:

    include= {'scope': ['REGRESSION'], 'feature': ['PRO_EXCLUDE']}
    

    It will include test cases/scenario which has meta-data scope whose value is REGRESSION AND feature whose value is PRO_EXCLUDE. Please refer documentation for more usage examples.

    NOTE: To work this feature properly, you have to add method selector from qaf com.qmetry.qaf.automation.testng.pro.QAFMethodSelector in xml configuration file or in ant testng target or in maven pom. Groups is also considered as one of the meta-data by qaf.