7

Through a groovy teststep in soapUI i want all request and response files to be stored in a local directory with system date.

The groovy teststep in soapUI:

def name = context.expand( '${Input#TG}' )

def locatie = context.expand( '${#TestCase#locatie}' )

def createFolder() {
  date = new Date()
  dateFormat = new java.text.SimpleDateFormat('ddMMyyyy')
  shortDate = dateFormat.format(date)
  outputFolder = locatie+shortDate
  createFolder = new File(outputFolder)
  createFolder.mkdir()  
}

def getResponseFilename(name) {
  respFilename = createFolder()+"_"+name+"_response.xml"
}

def getRequestFilename(locatie,name) {
  reqFilename = createFolder()+"_"+ name+"_request.xml"
}

def file = new PrintWriter (getResponseFilename(name))

def response = testRunner.testCase.testSteps
["CheckAdres"].testRequest.response.contentAsString

file.println(response)
file.flush()
file.close()

def file2 = new PrintWriter (getRequestFilename(name))
def request = context.expand('${CheckAdres#Request}')

file2.println(request)
file2.flush()
file2.close()

I get the following error:

Tue Jun 14 12:47:24 CEST 2011:**ERROR:groovy.lang.MissingPropertyException: No such property: locatie for class: Script78**
groovy.lang.MissingPropertyException: No such property: locatie for class: Script78
  at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:49)
  at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:241)
  at Script78.createFolder(Script78.groovy:8)
  at Script78$createFolder.callCurrent(Unknown Source)
  at Script78.getResponseFilename(Script78.groovy:14)
  at Script78$getResponseFilename.callCurrent(Unknown Source)
  at Script78.run(Script78.groovy:21)
  at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
  at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SourceFile:51)
  at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:148)
  at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
makhlo
  • 346
  • 1
  • 5
  • 16

5 Answers5

14

There are several ways of doing this. One would be creating a Groovy test step with the following script:

def myOutFile = "C:/Temp/MyOutDir/response.xml"
def response = context.expand( '${MyTestRequest#Response}' )
def f = new File(myOutFile)
f.write(response, "UTF-8")
Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
3

There is a shorter syntax. Logic is similar what @robert Shared

def response=context.expand('${TestRequest#RawRequest}')
new File("c:/testpath/input.xml").write(response)

if you want to reduce it to just one line

 new File("c:/testpath/input.xml").write(context.expand('${TestRequest#RawRequest}')

You can replace RawRequest with whatever you want to save

Request or Response

RawRequest is used when you want data replacing the variables used in request

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
  • Need some help please. can you tell me if it is possible to pick up different request messages from the file system, Trigger the request message sequentially if SOAP UI groovy script can pick up and call the SOAP endpoint.? – user3384231 Jul 27 '18 at 09:59
  • Yes. THrough ReadyApi its quite easy. For soapui you can simply write a groovy logic to pick files from a path and run it in a loop. Or alternatively you can pick up values from a property file and replace the values in your request and run it in a loop. There is something called.. testRunner.runTeststepByname("Request Step") <- YOu can have this statament in your groovy untill all the files are read from the system – Gaurav Khurana Jul 30 '18 at 02:42
  • Thanks. I wrote Curl and batch script to do this task. – user3384231 Jul 31 '18 at 08:59
2

Try to use SoapUI's tools to select the value of whatever you want. Right click on the groovy editing area, choose Get Data --> your test suite --> your test case --> your test step --> response. This will get you the entire response. You can also dive deeper into the response with this method.

RonK
  • 9,472
  • 8
  • 51
  • 87
1

Check out the answer by McDonald. Best way to save and shoot request.

http://www.loadui.org/forum/viewtopic.php?f=5&t=16354#p38935

amol-c
  • 435
  • 4
  • 10
0

More useful if we should save an error in Response:

import com.eviware.soapui.support.XmlHolder
import java.text.MessageFormat
import org.apache.commons.lang.ObjectUtils

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def retrieve = groovyUtils.getXmlHolder("MyTestRequest#Response" )

if (!ObjectUtils.equals(retrieve.getNodeValue("//*:xpath"), "string")){
 def currentTime = System.currentTimeMillis()
 def fullFilePath = context.expand('${projectDir}') + File.separator + "Fail-"+currentTime+".xml"
 def reportFile = new File(fullFilePath)
 if (!reportFile.exists())
 {
  reportFile.createNewFile()    
  reportFile.append((Object)retrieve.getPrettyXml(), 'UTF-8')
 }
}
user3215161
  • 81
  • 1
  • 1