cancel
Showing results for 
Search instead for 
Did you mean: 

How to access method getStepTestByName in a teardown script

Highlighted
Occasional Contributor

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!
 
 
9 REPLIES 9
Community Leader

Re: How to access method getStepTestByName in a teardown script

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");

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Occasional Contributor

Re: How to access method getStepTestByName in a teardown script

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")
  }
}

 

 

Community Leader

Re: How to access method getStepTestByName in a teardown script

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"
		}
	}
}

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Moderator

Re: How to access method getStepTestByName in a teardown script

Hi all,

 

@caz88, was the code helpful? If so, could you please accept it as a Solution?

 

We are looking forward to hearing from you.


Olga Terentieva
SmartBear Assistant Community Manager

Occasional Contributor

Re: How to access method getStepTestByName in a teardown script

@HimanshuTayal 

I appreciate the suggestion but ideally I want to use it in my SetUp script to alter the Datasource if there is a global property available

 

When I try to use runner.project in the setup script I keep getting an error?

Community Leader

Re: How to access method getStepTestByName in a teardown script

@caz88 ,

 

runner variable is avaible at setup script level, refer below link 

 

working-with-scripts

 

It will be helpful if you can attach any screenshot.

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Occasional Contributor

Re: How to access logic at project level

@HimanshuTayal I'm trying to get the logic from the groovy script I've written working at the project level. Currently the functionality requires me to include a groovy script within each testcase. I was wanting to be able to use the same logic in a setup script to reduce the amount of code needed to be inserted into the project. Can we use Java libraries in setup scripts? I'm not sure it is even possible to transfer this approach to a setup script at project level. 

 

class SelectedMarkets{
	def static selectMarketsToRunTests(testRunner, context, log){
		def dataPath
		def dataFile
		def headers
		def rowLine

  		//get the specifcic global property 
		def objGlobalProperties = com.eviware.soapui.SoapUI.globalProperties;
    	        def regionToRun = objGlobalProperties.getPropertyValue("region")
	        def regionArray = regionToRun.tokenize();
		log.info regionArray

    	//get original dataPath
    	dataPath = context.expand( '${#TestCase#DataPath}' )

    	//get original dataFile
    	dataFile = context.expand( '${#TestCase#DataFile}' )
    	//read current file 
    	FileInputStream fstream = new FileInputStream(dataPath +"/"+dataFile);
    	DataInputStream data = new DataInputStream(fstream);
    	BufferedReader br = new BufferedReader(new InputStreamReader(data));
    	//find relevant lines and then write to another file
    		while ((rowLine = br.readLine()) != null){
    			if(rowLine.contains("locale")){
    					headers = "$rowLine" 
    					log.info headers;
    				}

				regionArray.each{
					//if finds global property write to new file
    				if(rowLine.contains(it) && !rowLine.contains("locale")){
    					//create temporary file
    					def tempFile = File.createTempFile("temp" + dataFile, ".csv") 
    					//write revalent locales to temp file
    					headers += "\n$rowLine"
    					tempFile.write headers
    					log.info headers
    					//set the datasource to the temp file
    					def dataSourceStep = testRunner.testCase.getTestStepByName("DataSource")
    					dataSourceStep.getDataSource().setFileName(tempFile.absolutePath)
    	  			}
				}
    		}
    	data.close();
  	}
}
Community Hero

Re: How to access logic at project level

Moderator

Re: How to access logic at project level

Thanks everyone for your participation.

 

@caz88, what stage are you on now?


Olga Terentieva
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Watch the New Interview
APITestingMistake#2
Top Kudoed Authors