Forum Discussion

caz88's avatar
caz88
Occasional Contributor
6 years ago

How to access method getStepTestByName in a teardown script

Hi Folks, 

 

Is there a reason that this:

 
def dataSourceStep = testRunner.testCase.getTestStepByName("DataSource")
 
Doesn't work in a teardown script? 
 
Using a Groovy script I have modified my datasource path to point to a temporary file if a particular global property exists which I execute in a setup script. 
 
I am wanting to revert the datasource back to it's original version after it has been run. I am doing it at the testcase level but is there a way to do it at the project level? 
 
Any input would be appreciated!
 
 
  • Hi caz88 ,

     

    testRunner variable is not available at Project level.

    If you wanted to access testStep you can use below syntax by using project variable.

     

    project.getTestSuiteByName("Test Suite Name").getTestCaseByName("Test Case Name").getTestStepByName("Test Step Name");

     

    • caz88's avatar
      caz88
      Occasional Contributor

      Hi HimanshuTayal 

       

      Thanks for the reply. If testrunner isn't available at the project level is there a way to reset every datasource of every testcase at the end of the run. I have it working at the testcase level but would like it to be a bit of script that sits in the teardown script of the project. The scripts I've included currently sit at the TestCase level

       

      class TearDownForSelectedMarkets{
      
      	def static resetTestCaseSettings(testRunner, context, log){
            //reset the data to original path
      	DataSource.setDataPath(testRunner,context,log)
      
      		//reset the datasource path
      		def dataSourceStep = testRunner.testCase.getTestStepByName("DataSource")
      		dataSourceStep.getDataSource().setFileName('${#TestCase#DataPath}/${#TestCase#DataFile}')
      
      		//delete global property
      		com.eviware.soapui.SoapUI.globalProperties.removeProperty("region") 
      
      		//delete temporary folder/file
      
        	}
      
      }

       

      class StartUpForSelectedMarkets{
      
      	def static setRegionAsGlobalProperty(testRunner, context, log){
          	   //set global property - SETUP SCRIPT
      		def objGlobalProperties = com.eviware.soapui.SoapUI.globalProperties
      		objGlobalProperties.setPropertyValue("region", "ca ie")
        }
      }

       

       

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        Hi caz88 ,

         

        Try below code hope it will help.

         

        def suiteCount = project.testSuiteCount
        def testCaseCount
        for(i=0;i<suiteCount;i++){
        	testCaseCount = runner.project.getTestSuiteAt(i).getTestCaseCount();
        	for(j=0;j<testCaseCount;j++){
        		//work when DataSource Step name is "DataSource"
        		def dStep = runner.project.getTestSuiteAt(i).getTestCaseAt(j).getTestStepByName("DataSource");
        		if(dStep != null ){
        			//reset here
        			log.info "contains datasource step"
        		}
        		else{
        			//these are the cases where DataSource step doesn't exist
        			log.info "doesn't contains datasource step"
        		}
        	}
        }