Forum Discussion

krogold's avatar
krogold
Regular Contributor
4 years ago

Setup script execution from another groovy step

Hello,

I have several testCases in my project for which the setup script cleans the properties (generated dynamically during testCase execution). In order to clean my project before GIT commit, I wish to clear those dynamic properties and I created a testCase with a groovy script that parses all my project's testCases and, if the setup script cleans the properties, execute it. However, though my detection is correct, the script execution does not occur.

Am I missing something ? here is my script :

 

/* check setup scripts
 if those contain 
testRunner.testCase.getPropertyList().each{
	testRunner.testCase.removeProperty(it.name)
}
it means that, prior to execution the setup script will clear data that will be (re)created during test execution
it means that this data is likely to be detected as a relevant modification in git though it is not
to avoid this this script will detect which test has such a setup script and will execute the setup script
in order to delete these dynamic properties
*/

testRunner.testCase.testSuite.project.testSuiteList.each
{
	suite ->
	name = suite.getName()

//	if (name == "USE CASES - Configuration")
	if ((name.contains("USE CASES -"))||(name.contains("TOOLS - Configuration")))
	{
		suite.testCaseList.each{
			TC -> 
			if ((TC.setupScript != null)&&(TC.setupScript.contains("removeProperty")))
			{
				log.info "concerned test " + suite.getName() + " ; " + TC.name
				TC.runSetupScript(context, testRunner)
				
			}
		}
	}
}
  • Finally I found out how to do it : it requires to modify context.ExecutionID

     

    /* check setup scripts
     if those contain 
    testRunner.testCase.getPropertyList().each{
    	testRunner.testCase.removeProperty(it.name)
    }
    it means that, prior to execution the setup script will clear data that will be (re)created during test execution
    it means that this data is likely to be detected as a relevant modification in git though it is not
    to avoid this this script will detect which test has such a setup script and will execute the setup script
    in order to delete these dynamic properties
    */
    
    testRunner.testCase.testSuite.project.testSuiteList.each
    {
    	suite ->
    	name = suite.getName()
    
    	if ((name.contains("USE CASES -"))||(name.contains("TOOLS - Configuration")))
    	{
    		suite.testCaseList.each{
    			TC -> 
    			if ((TC.setupScript != null)&&(TC.setupScript.contains("removeProperty")))
    			{
    				s_script = TC.setupScript 	// store the script
    				TC.setSetupScript(null) 		// clear setup script
    
    				// neutralize init_test_case
    				sscript = s_script.replace("setup.init_test_case","//setup.init_test_case")
    				sleep(1000)	
    				TC.setSetupScript(sscript) 	// replace initial setup script
    
    				// execute the modified setup script to clean properties without launching init
    				context.ExecutionID = TC.config.id
    				TC.runSetupScript(context, testRunner)
    
    				// restore initial setupScript
    				// re-enable setup init
    				TC.setSetupScript(s_script)
    			}
    		}
    	}
    }

     

  • krogold's avatar
    krogold
    Regular Contributor

    actually, it looks like it takes, as a context, the script that executes the research. Even if this one does not have a setup script, it executes the setup script of a target testCase but with its own characteristics (I've put a log that shows the name of the testCase that is run and it gives the one of my cleaning script !)

    • krogold's avatar
      krogold
      Regular Contributor

      Finally I found out how to do it : it requires to modify context.ExecutionID

       

      /* check setup scripts
       if those contain 
      testRunner.testCase.getPropertyList().each{
      	testRunner.testCase.removeProperty(it.name)
      }
      it means that, prior to execution the setup script will clear data that will be (re)created during test execution
      it means that this data is likely to be detected as a relevant modification in git though it is not
      to avoid this this script will detect which test has such a setup script and will execute the setup script
      in order to delete these dynamic properties
      */
      
      testRunner.testCase.testSuite.project.testSuiteList.each
      {
      	suite ->
      	name = suite.getName()
      
      	if ((name.contains("USE CASES -"))||(name.contains("TOOLS - Configuration")))
      	{
      		suite.testCaseList.each{
      			TC -> 
      			if ((TC.setupScript != null)&&(TC.setupScript.contains("removeProperty")))
      			{
      				s_script = TC.setupScript 	// store the script
      				TC.setSetupScript(null) 		// clear setup script
      
      				// neutralize init_test_case
      				sscript = s_script.replace("setup.init_test_case","//setup.init_test_case")
      				sleep(1000)	
      				TC.setSetupScript(sscript) 	// replace initial setup script
      
      				// execute the modified setup script to clean properties without launching init
      				context.ExecutionID = TC.config.id
      				TC.runSetupScript(context, testRunner)
      
      				// restore initial setupScript
      				// re-enable setup init
      				TC.setSetupScript(s_script)
      			}
      		}
      	}
      }

       

      • krogold's avatar
        krogold
        Regular Contributor

        Hello,

        unfortunately it seems that this solution does not work any longer on latest ReadyAPI versions (problems in 3.8.0 and 3.9.0) so I had to find another way to perform what I wanted, ie. to play a part of each testCase's setup script.

        The workaround I found is to modify temporarily the concerned tests :

        - I get the list of their testSteps id (I use config.id as a unique identifier) and status (disabled or not)

        - I disable all the testSteps

        - I store the setup script and modify it accordingly to my needs (in my case I neutralize an initialization call)

        - I store the teardown script and erase it

        - I run the test so it can clean all the unwanted properties that were created during the test runs (and that were needed for further testing)

        then I reset my tests back to their original configuration

        - I enable the testSteps according to their original status

        - I replace the setup script with its original value

        - I replace the teardown script with its original value

         

        here is the code associated:

        testRunner.testCase.testSuite.project.testSuiteList.each
        {
            suite ->
            name = suite.getName()
        
            suite.testCaseList.each{
                TC -> 
                if ((TC.setupScript != null)&&(TC.setupScript.contains("removeProperty")))
                {
                    log.info "clear lists from test '${TC.name}'"
                    // store testSteps state
                    ts_state_map = [:]
                    //testRunner.testCase.gettes
                    TC.getTestStepList().each{
                        //log.info "${it.config.id} / " + it.isDisabled()
                        ts_state_map[it.config.id] = it.isDisabled()
                        // neutralize each testSTep
                        if (it.isDisabled() != true)
                            it.setDisabled(true)
                    }
                    log.info ts_state_map
        
                    // neutralize Setup script init phase
                    s_script = TC.setupScript     // store the script
                    TC.setSetupScript(null)         // clear setup script
        
                    // neutralize init_test_case part
                    sscript = s_script.replace("setup.init_test_case","//setup.init_test_case")
                    sleep(1000)    
                    TC.setSetupScript(sscript)     // replace initial setup script
                    
                    // neutralize TD script as well
                    td_script = TC.tearDownScript
                    TC.setTearDownScript(null)
        
                    // execute testCase in order to execute only setup script
                    def async = false
                    TC.run (context.getProperties(), async)
        
                    // restore TD script
                    TC.setTearDownScript(td_script)
        
                    // restore initial setupScript
                    // re-enable setup init
                    TC.setSetupScript(s_script)
                    
                    // restore testSteps state
                    TC.getTestStepList().each{
                        //log.info "${it.config.id} / " + it.isDisabled()
                        // restore state
                        if (ts_state_map[it.config.id] != true)
                            it.setDisabled(false)
                    }
                }
            }
        }