Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

[TechCorner Challenge #1] How to add a new tag and assign it to all TestCases in a TestSuite?

Hi Community!


As you know, we have a TechCorner label, a place where tons of useful information about ReadyAPI is stored. We would like to make this section even bigger.

 

I would like to encourage you to share more great content with all ReadyAPI community members under this label: use cases, tips, scripts, script templates, all the helpful things you are using in your daily testing with ReadyAPI.

 

Hereโ€™s one request that we get a lot from users,that you can help with:

 

Create a script, that can add a new tag and then assign it to every TestCases in a TestSuite

Difficulty


A link that will help you choose the right methods of working with tags at the TestCase level.

 

Tags are a neat feature of ReadyAPI, they help group TestCases and give more control over the testing process. This script is going to be very useful for ReadyAPI users. Who will contribute?๐Ÿ™‚

 

  • Task: Create a script, that can add a new tag and then assign it to every TestCases in a TestSuite.

     

    This is a solution created for [TechCorner Challenge #1]

     

    Here's a groovy script that will take a Test Suite name, and then iterate through each test case to add that name. Can be adapted to go through each test suite too, easily enough.

     

     

    // set up project level variable
    def project = context.testCase.testSuite.project;
    String tagName = "DesiredTagName";
    
    // Add desired tag name to project
    project.addTag(tagName);
    
    // get tagId of added tag
    String tagID = project.getTagId(tagName);
    
    // set up desired test suite, by name.
    def testSuite = project.getTestSuiteByName("test");
    
    for (int i = 0; i < testSuite.getTestCaseCount(); i++)
    {
    	// get test Case object by index
    	def tc = testSuite.getTestCaseAt(i);
    
    	// assign the tag
    	tc.assignTag(tagID);
    
    }
    

     

10 Replies

  • sonya_m's avatar
    sonya_m
    SmartBear Alumni (Retired)

    Hint: This can be done in the following manner: add a new tag to a project using the addTag(String tagName) function and assign it to TestCases with the assignTag(String tagId) function.

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        Task: Create a script, that can add a new tag and then assign it to every TestCases in a TestSuite.

         

        This is a solution created for [TechCorner Challenge #1]

         

        Here's a groovy script that will take a Test Suite name, and then iterate through each test case to add that name. Can be adapted to go through each test suite too, easily enough.

         

         

        // set up project level variable
        def project = context.testCase.testSuite.project;
        String tagName = "DesiredTagName";
        
        // Add desired tag name to project
        project.addTag(tagName);
        
        // get tagId of added tag
        String tagID = project.getTagId(tagName);
        
        // set up desired test suite, by name.
        def testSuite = project.getTestSuiteByName("test");
        
        for (int i = 0; i < testSuite.getTestCaseCount(); i++)
        {
        	// get test Case object by index
        	def tc = testSuite.getTestCaseAt(i);
        
        	// assign the tag
        	tc.assignTag(tagID);
        
        }