Ask a Question

In SoapUI how to create multiple REST Requests via groovy script?

SOLVED
Sarita
New Contributor

In SoapUI how to create multiple REST Requests via groovy script?

Hi Experts,

 

I have a bunch of REST request messages in text format. Is there a way to import them to a SoapUI project?

I want to import them and add as "Test Request" Test Step to an existing Test Case.

6 REPLIES 6
richie
Community Hero

Hey @Sarita 

 

you cant just import a bunch of REST requests in text form - BUT - if your REST apis are described in a .wadl file (equivalent to a SOAP Services .wsdl), you can create a project from the .wadl and this will do what you need.

 

Cheers,

 

rich

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Sarita
New Contributor

Hi @richie,

Thank you for your reply. 

I am using REST API URI to create a Project in SoapUI. our REST API's are not described in a .wadl format. I am looking for a groovy script to create multiple requests. Trying the below groovy script, but I am getting error in my code: Looks like I am missing something in my code.

 

import com.eviware.soapui.impl.rest.actions.method.NewRestRequestAction ;

// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the REST TestStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Request 1");
// create the test step factory to use later
def testStepFactory = new NewRestRequestAction();

// now get all the request from a specific location...

def directory = new File("D:/.............................../")
// for each file in the directory
directory.eachFile{ file ->
// use file name as test step name
def testStepName = file.getName()
// create the config
def testStepConfig = testStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName)
// add the new testStep to TestCase
def newTestStep = tc.insertTestStep( testStepConfig, -1 )
// set the request from the file content
newTestStep.getTestRequest().setRequestContent(file.getText())
}

appliedMagic
Occasional Contributor

Hi,
so I used http://petstore.swagger.io to create a new rest project (don't have anything else to test against), be sure to change the GET request to a POST request, otherwise you will be unable to add anything to the request body.


The project looked like this (note the [POST] Swagger.json 1, before it was [GET]):

screenshot.png

 

Add the following script to the groovy script test step, change <path> and <url> to what you need.

 

import com.eviware.soapui.config.TestStepConfig;
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory;
import com.eviware.soapui.impl.rest.RestRequest;
import com.eviware.soapui.impl.rest.RestResource;
import com.eviware.soapui.impl.rest.RestService;

log.info('start')

// the test case where we want to add stuff to
def testCases = context.testCase.testSuite.project.getTestSuiteByName('testSuite').getTestCaseByName('testCases')

// see also here: https://community.smartbear.com/t5/SoapUI-Open-Source/How-do-I-automatically-insert-a-REST-test-step-into-a-SoapUI/m-p/102437#M17957
// the rest template, first he interface, we only have 1 so index 0
def iface = context.testCase.testSuite.project.getInterfaceList()[0];
// get the operation, again we only have 1 so 0
def opr = iface.getOperationList()[0]
// get the appropriate request, also only 1 available so 0
def res = opr.getRequestAt(0)


// iterate over all files we want to add, txt files were used in my test
new File('<path>').eachFile { file ->
	if(file.getName().toLowerCase().endsWith('.txt')) {
		def testStepName = file.getName();

		//create the test step
		TestStepConfig testStepConfig = RestRequestStepFactory.createConfig( res, testStepName);
		def testStep = testCases.addTestStep(testStepConfig);
		// set the url
		testStep.getHttpRequest().setEndpoint("<url>");
		// set content
		testStep.setPropertyValue('Request', file.text)
	}
}

log.info('done')

 

Hi @Sarita,

 

You were really close 😉.

You should use "RestRequestStepFactory" instead of "NewRestRequestAction".

Here is a script that worked for me:

 

import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory

// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the REST TestStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("REST Request");
// create the test step factory to use later
//def testStepFactory = new NewRestRequestAction();

// now get all the request from a specific location...

def directory = new File("C:\\Users\\xxxxxxxxx\\Documents\\xxxxxxxx")
// for each file in the directory
directory.eachFile{ file ->
	// use file name as test step name
	def testStepName = file.getName()
	// create the config
	def testStepConfig = RestRequestStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName)
	// add the new testStep to TestCase
	def newTestStep = tc.insertTestStep( testStepConfig, -1 )
	// set the request from the file content
	newTestStep.getTestRequest().setRequestContent(file.getText())
}

 

Please tell us if the script is working for your case.

 

David.

Sarita
New Contributor

Hi David,

Thank you for your reply. 🙂

Yes it is working..!!

-Sari

Sarita
New Contributor

Hi David,

 

Here is my final code, which generates requests with .txt extension.

def testStepName = file.getName()

 just to trim the .txt extension

def testStepName = file.name.take(file.name.lastIndexOf('.'))

//Final Code

 

import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory;

// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the REST TestStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Request 1");

// now get all the request from a specific location...

// your location
def directory = new File("D:/.../")
// for each file in the directory
directory.eachFile{ file ->
// use file name as test step name
def testStepName = file.name.take(file.name.lastIndexOf('.')) // to trim the .txt extension
// create the config
def testStepConfig = RestRequestStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName)

// add the new testStep to TestCase
def newTestStep = tc.insertTestStep( testStepConfig, -1 )
// set the request from the file content
newTestStep.getTestRequest().setRequestContent(file.getText())
}

cancel
Showing results for 
Search instead for 
Did you mean: