Alex Cooke
Alex Cooke

Reputation: 3

Import Multiple XML files into SoapUI

Started a new job couple of months ago, and requires basic day-to-day use of SoapUI for executing batches of tests that require some visual verification (we haven't automated the visual part yet).

However, I now have a whole bunch of new xmls file I need to import into a project, and I was wondering whether somebody had a groovy script or something, to import the files, and use the content within the file as the xml text to pass,. via HTTP requests.

Basically, I want what albciff has done here. But turn my xml files into HTTP steps.

I have tried modifying his script to include the correct classes for HTTP, but I am getting an exception which I cant fix

UPDATE 28/01

I am just using the free/standard edition of SoapUI. I have limited codding/scripting knowledge as I'm just a functional tester :(

My current groovy script is

import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory
import groovy.io.FileType

// get the current testCase to add testSteps later
def tc = testRunner.testCase
// get the testStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Template")
// create the factory to create testSteps
def testStepFactory = new HttpRequestStepFactory()

def requestDir = new File("C://directory//..//final_dir")
// for each xml file in the directory
requestDir.eachFileRecurse (FileType.FILES) { file ->
  def newTestStepName = file.getName()
  // create the config
  def testStepConfig = testStepFactory.createConfig( tsTemplate.getOperation(), newTestStepName )
  // add the new testStep to current testCase
  def newTestStep = tc.insertTestStep( testStepConfig, -1 )
  // set the request which just create with the file content
  newTestStep.getTestRequest().setRequestContent(file.getText())
}

And the exception I get when running is

groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep.getOperation() is applicable for argument types: () values: [] Possible solutions: getAssertions() error at line: 14

Upvotes: 0

Views: 6048

Answers (1)

albciff
albciff

Reputation: 18507

You're almost there :), try this way (use HttpRequestStepFactory.createNewTestStep instead of HttpRequestStepFactory.createConfig):

import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory
import groovy.io.FileType

// get the current testCase to add testSteps later
def tc = testRunner.testCase
// get the testStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Template")
// create the factory to create testSteps
def testStepFactory = new HttpRequestStepFactory()
def requestDir = new File("C://directory//..//final_dir")
// for each xml file in the directory
requestDir.eachFileRecurse(FileType.FILES){ file ->
  def newTestStepName = file.getName()
  // get the template endpoint
  def endpoint = tsTemplate.getPropertyValue('Endpoint')
  // create the config using endpoint and your method (post in your case)
  def testStepConfig  = testStepFactory.createNewTestStep(tc,newTestStepName,endpoint,'POST')
  // add the new testStep to current testCase
  def newTestStep = tc.insertTestStep( testStepConfig, -1 )
  // set the request which just create with the file content
  def testRequest = newTestStep.getTestRequest()
  testRequest.setRequestContent(file.getText())

  // UPDATED CODE BELOW BASED ON COMMENT

  // you can use in this case HttpRequestConfig (due the type of your
  // testStep you can use this class) to set some additional properties 
  // in your TestStep
  def httpReqCfg = testRequest.getConfig();
  // for example for mediaType as you said in your comment
  httpReqCfg.setMediaType('application/json');

}

Note that looks more convenient to use the HttpTestRequestStep.clone method. However when you invoke this in the groovy, the testStep is created but groovy throws the follow exception (I think that here there is something wrong with the SOAPUI API):

java.lang.ClassCastException: com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep cannot be cast to com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

However since I think that you're interesting only in use of file content to set the request the proposed code above it's enough in your case.

EDIT BASED ON COMMENT:

You can also use HttpRequestConfig to set mediaType or to add additional parameters as you require in your comment, take a look at the HttpRequestConfig API, I also update my code above to show you an example on how to get this object and setMediaType take a look on it.

Hope this helps,

Upvotes: 1

Related Questions